I'm creating a two player game and I'm having some difficulties with creating two movable player images. should I just make a player one and player two claas or is this amateuristic? here is my current non working code:
package game;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
public class Speler {
private Image image;
private int x = 0, y = 0, velX=0, velY = 0;
private int eerste;
public Speler(int welke){
welke = eerste;
if( welke < 10){ImageIcon ii = new ImageIcon("C:\\Users\\gil\\Pictures\\blauw vierkant.png" );
image = ii.getImage();
x = 40;
y = 160;}
else{ ImageIcon ii = new ImageIcon("C:\\Users\\gil\\Pictures\\paars vierkant.png" );
image = ii.getImage();
x = 520;
y = 10;}
}
public void move(){
if (x<0)
{
velX = 0;
x=0;
}
if (x>545)
{
velX = 0;
x=545;
}
if (y<0)
{
velY = 0;
y=0;
}
if (y>323)
{
velY = 0;
y=323;
}
x = x + velX;
y = y + velY;
}
public void keyPressed(KeyEvent e){
int c = e.getKeyCode();
if (eerste < 10){
if (c == KeyEvent.VK_Q)
{
velX = -1;
velY = 0;
}
if (c == KeyEvent.VK_Z)
{
velX = 0;
velY = -1;
}
if (c == KeyEvent.VK_D)
{
velX = 1;
velY = 0;
}
if (c == KeyEvent.VK_S)
{
velX = 0;
velY = 1;}
else{
if (c == KeyEvent.VK_LEFT)
{
velX = -1;
velY = 0;
}
if (c == KeyEvent.VK_UP)
{
velX = 0;
velY = -1;
}
if (c == KeyEvent.VK_RIGHT)
{
velX = 1;
velY = 0;
}
if (c == KeyEvent.VK_DOWN)
{
velX = 0;
velY = 1;
}
}
}
}
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){
velX = 0;
velY = 0;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public Image getImage() {
return image;
}
public void Drawspeler(Graphics g){
Graphics2D g2d1 = (Graphics2D) g;
g2d1.drawImage(image,x,y,null);
}
}
package game;
import javax.swing.*;
import java.awt.event.KeyAdapter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
public class Board extends JPanel implements ActionListener{
private Timer tm;
private Speler speler1;
private Speler speler2;
public Board(){
addKeyListener(new Indrukken());
setFocusable(true);
setFocusTraversalKeysEnabled(false);
speler1 = new Speler(1);
speler2 = new Speler(50);
tm = new Timer(10, this);
tm.start();
}
public void paintComponent(Graphics g ) {
super.paintComponent(g);
speler1.Drawspeler(g);
speler2.Drawspeler(g);
}
public void actionPerformed(ActionEvent e){
speler1.move();
speler2.move();
repaint();
}
private class Indrukken extends KeyAdapter {
#Override
public void keyPressed(KeyEvent e){
speler1.keyPressed(e);
speler2.keyPressed(e);
}
#Override
public void keyReleased(KeyEvent e){
speler1.keyReleased(e);
speler2.keyReleased(e);
}
}
}
Related
I have been sitting here for 5 hours trying to make my ball bounce off the paddle and the walls when it hits it but it just goes straight through the walls and paddle then re enters through the top and the same cycle continues. I don't know where I am going wrong
My MainWindow class:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
public class MainWindow extends JPanel implements KeyListener, ActionListener {
private boolean gameRunning = true;
private int BOARD_WIDTH = 500;
private int BOARD_HEIGHT = 500;
private Thread animator;
private BufferedImage img;
private Dimension dimension;
private int ballX = 20;
private int ballY = 200;
private int ballSpeedX = 4;
private int ballSpeedY = 5;
private int ballWidth = 20;
private Brick[] bricks = new Brick[24];
private boolean[] showBrick = new boolean[24];
private int paddleX = BOARD_WIDTH/2;
private int paddleY = BOARD_HEIGHT-50;
private int paddleW = 100;
private int paddleH = 10;
boolean paddleLeft = false;
boolean paddleRight = true;
private Timer timer;
private int delay = 50;
public MainWindow(){
addKeyListener(this);
//addMouseListener(this);
setFocusable(true);
dimension = new Dimension(BOARD_WIDTH, BOARD_HEIGHT);
setBackground(Color.BLACK);
int x = 20;
int y = 20;
int count = 1;
for(int i = 0; i < bricks.length; i++){
bricks[i] = new Brick(x, y, 50, 10);
showBrick[i] = true;
x += 55;
if (count%8 == 0){
x = 20;
y += 20;
}
count++;
}
timer = new Timer(delay, this);
timer.start();
//if (animator == null || !gameRunning) {
// animator = new Thread(this);
//animator.start();
//}
setDoubleBuffered(true);
}
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.pink);
g.fillRect(0,0, dimension.width, dimension.height);
g.setColor(Color.black);
// for the bricks
for(int i = 0; i<bricks.length; i++) {
if(showBrick[i])
g.fillRect(bricks[i].getBrickX(), bricks[i].getBrickY(),bricks[i].getBrickW(),bricks[i].getBrickH());
}
// for the paddle
g.fillRect(paddleX, paddleY, paddleW, paddleH);
// for the ball
g.fillOval(ballX, ballY, ballSpeedX,ballSpeedY);
//if (gameRunning){
//ballX+=ballSpeedX;
// movingBall();
//repaint();
//}
g.dispose();
}
private void movingBall(){
}
#Override
public void keyTyped(KeyEvent e) {
//if (e.getKeyCode() == KeyEvent.VK_RIGHT){
// paddleX+=5;
//}
}
#Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (e.getKeyCode() == KeyEvent.VK_RIGHT){
if(paddleX >= 385){
paddleRight = false;
}
else {
//ballX+=ballSpeedX;
paddleRight = true;
paddleX+=5;
repaint();
}
}
if (e.getKeyCode() == KeyEvent.VK_LEFT){
if(paddleX <= 10){
paddleLeft = false;
}
else {
paddleLeft = false;
paddleX-=5;
repaint();
}
}
}
#Override
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
if (e.getKeyCode() == KeyEvent.VK_RIGHT){
paddleRight = false;
}
if (e.getKeyCode() == KeyEvent.VK_LEFT){
paddleLeft = false;
}
}
#Override
public void actionPerformed(ActionEvent e) {
timer.start();
if(ballX < 0 || ballX > BOARD_WIDTH){
ballSpeedX = ballSpeedX * -1;
}
if(ballY < 0){
ballSpeedY = ballSpeedY * -1;
}
if(ballY > BOARD_HEIGHT){
ballX = 20;
ballY = 200;
ballSpeedY *= -1;
}
if(ballX == paddleX && ballY == paddleY){
ballSpeedY *= -1;
}
if(ballX + ballWidth > paddleX && ballX < paddleX + paddleW && ballY + ballWidth > paddleY && ballY < paddleY + paddleH) {
ballSpeedY *= -1;
}
ballX += ballSpeedX;
ballY += ballSpeedY;
repaint();
//System.out.println("its working");
}
}
My Window class:
import javax.swing.*;
public class Window {
public Window() {
JFrame frame = new JFrame("Brick Builder Game");
frame.setDefaultCloseOperation(frame.DISPOSE_ON_CLOSE);
frame.setSize(500,500);
frame.add(new MainWindow());
frame.setVisible(true);
frame.setResizable(false);
}
public static void main(String[] args){
new Window();
}
}
My brick class:
public class Brick {
// X-position of brick
private int brickX;
// Y-position of brick
private int brickY;
// width of brick
private int brickW;
// height of brick
private int brickH;
public Brick(int brickX, int brickY, int brickW, int brickH) {
this.brickX = brickX;
this.brickY = brickY;
this.brickW = brickW;
this.brickH = brickH;
}
public int getBrickX(){
return brickX;
}
public int getBrickY(){
return brickY;
}
public int getBrickW(){
return brickW;
}
public int getBrickH(){
return brickH;
}
}
the signature for fillOval is as follows: (x_position, y_position, width, height). And in your signature, you try to use ballSpeedX and ballSpeedY as the width and height.
So when you do ballSpeedX or ballSpeedY *= -1. Swing tries to draw an oval, with negative measures, which is not possible, and thus fails to show the ball
I am new to java, and after attempting to create a small snake type game, and error occurred
Error: Main method not found in class Game.GamePanel, please define the main method as: public static void main(String[] args)"
Could someone please help me as to fixing the error? It would be a big help as a beginner to java. Im using Eclipse latest version
package Game;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import javax.swing.JPanel;
public class GamePanel extends JPanel implements Runnable, KeyListener {
public static final int WIDTH = 1000;
public static final int HEIGHT = 1000;
//Render
private Graphics2D g2d;
private BufferedImage image;
//GameLoop
private Thread thread;
private boolean running;
private long targetTime;
//Game Stuff
private final int SIZE = 10;
Entity head;
ArrayList<Entity> snake;
//movement
private int dx, dy;
//key input
private boolean up,down,right,left,start;
public GamePanel() {
setPreferredSize(new Dimension(WIDTH, HEIGHT));
setFocusable(true);
requestFocus();
addKeyListener(this);
}
public void addNotify() {
super.addNotify();
thread = new Thread(this);
thread.start();
}
private void setFPS(int fps) {
targetTime = 1000 / fps;
}
#Override
public void keyTyped(KeyEvent e) {
}
#Override
public void keyPressed(KeyEvent e) {
int k = e.getKeyCode();
if(k == KeyEvent.VK_UP) up = true;
if(k == KeyEvent.VK_DOWN) down = true;
if(k == KeyEvent.VK_LEFT) left = true;
if(k == KeyEvent.VK_RIGHT) right = true;
if(k == KeyEvent.VK_ENTER) start = true;
}
#Override
public void keyReleased(KeyEvent e) {
int k = e.getKeyCode();
if(k == KeyEvent.VK_UP) up = false;
if(k == KeyEvent.VK_DOWN) down = false;
if(k == KeyEvent.VK_LEFT) left = false;
if(k == KeyEvent.VK_RIGHT) right = false;
if(k == KeyEvent.VK_ENTER) start = false;
}
#Override
public void run() {
if(running) return;
init();
long startTime;
long elapsed;
long wait;
while(running){
startTime = System.nanoTime();
update();
requestRender();
elapsed = System.nanoTime() - startTime;
wait = targetTime - elapsed / 1000000;
if(wait > 0) {
try {
Thread.sleep(wait);
}catch(Exception e) {
e.printStackTrace();
}
}
}
}
private void init() {
image = new BufferedImage(WIDTH,HEIGHT,BufferedImage.TYPE_INT_ARGB);
g2d = image.createGraphics();
running = true;
setUplevel();
setFPS (10);
}
private void setUplevel() {
snake = new ArrayList<Entity>();
head = new Entity(SIZE);
head.setPosition(WIDTH / 2, HEIGHT / 2);
snake.add(head);
for(int i = 1;i < 10;i++) {
Entity e = new Entity(SIZE);
e.setPosition(head.getX() + (i * SIZE), head.getY());
snake.add(e);
}
}
private void requestRender() {
render(g2d);
Graphics g = getGraphics();
g.drawImage(image, 0,0,null);
g.dispose();
}
private void update() {
if(up && dy == 0) {
dy = -SIZE;
dx = 0;
}
if(down && dy == 0) {
dy = SIZE;
dx = 0;
}
if(left && dx == 0) {
dy = 0;
dx = -SIZE;
}
if(right && dx == 0) {
dy = 0;
dx = SIZE;
}
if(dx != 0 || dy != 0) {
for(int i = snake.size() - 1;i > 0;i--) {
snake.get(i).setPosition(
snake.get(i - 1).getX(),
snake.get(i - 1).getY()
);
}
head.move(dx, dy);
}
if(head.getX() < 0 ) head.getX(WIDTH);
if(head.getY() < 0 ) head.getY(HEIGHT);
if(head.getX() > WIDTH ) head.getX(0);
if(head.getY() > HEIGHT ) head.getY (0);
}
public void render(Graphics2D g2d) {
g2d.clearRect(0, 0, WIDTH, HEIGHT);
g2d.setColor(Color.GREEN);
for(Entity e : snake) {
e.render(g2d);
}
}
}
You can add it to GamePanel, but you need to create a JFrame and you want to add an instance of your GamePanel to it. Something like,
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame();
frame.add(new GamePanel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
I know this question is asked often in stackoverflow. And i know there is a page on oracle about LayoutManagers but i dont understand how to use it? Can someone help me with my code? Im trying to make a game with 2 squares trying to catch each other. It's not finished yet. I will finish it once i solve the graphical issues. Do i need a JPanel to do it? Thanks in advance!
JFrame class
package catchmev2;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
/**
*
* #author MertKarakas
*/
public class CatchMeV2 implements ActionListener{
public static void main(String[] args) {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int width = (int) screenSize.getWidth();
int height = (int) screenSize.getHeight();
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setResizable(true);
frame.setVisible(true);
frame.setSize(width, height);
frame.setTitle("CatchMe.V2");
frame.getContentPane().setLayout(new FlowLayout());
//Adding square 1
RedSquare r = new RedSquare();
frame.getContentPane().add(r);
r.setFocusable(true);
r.requestFocusInWindow();
//Adding square 2
BlueSquare b = new BlueSquare();
frame.getContentPane().add(b);
b.setFocusable(true);
b.requestFocusInWindow();
}
#Override
public void actionPerformed(ActionEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
First square class
package catchmev2;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JPanel;
import javax.swing.Timer;
/**
*
* #author MertKarakas
*/
public class RedSquare extends JPanel implements ActionListener, KeyListener {
int x = 20; int y = 20;
int velX = 0; int velY = 0;
Timer tm = new Timer(5, this);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int width = (int) screenSize.getWidth();
int height = (int) screenSize.getHeight();
public RedSquare(){
addKeyListener(this);
tm.start();
setFocusable(true);
setFocusTraversalKeysEnabled(false);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.red);
g.fillRect(x, y, 25, 25);
repaint();
}
#Override
public void actionPerformed(ActionEvent e) {
x = x + velX;
y = y + velY;
if (x < 0) {
velX = 0;
x = 0;
}
if (x > width - 50) {
velX = 0;
x = width - 50;
}
if (y < 0) {
velY = 0;
y = 0;
}
if (y > height - 40) {
velY = 0;
y = height - 40;
}
repaint();
}
public void keyTyped(KeyEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
#Override
public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();
if (code == KeyEvent.VK_LEFT) {
velX = -4;
velY = 0;
System.out.println("sex1");
}
if (code == KeyEvent.VK_UP) {
velX = 0;
velY = -4;
System.out.println("sex2");
}
if (code == KeyEvent.VK_RIGHT) {
velX = 4;
velY = 0;
System.out.println("sex3");
}
if (code == KeyEvent.VK_DOWN) {
velX = 0;
velY = 4;
System.out.println("sex4");
}
}
#Override
public void keyReleased(KeyEvent e) {
velX = 0;
velY = 0;
}
}
Second square class
package catchmev2;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JPanel;
import javax.swing.Timer;
/**
*
* #author MertKarakas
*/
public class BlueSquare extends JPanel implements ActionListener, KeyListener {
int x = 700;
int y = 600;
int velX = 0;
int velY = 0;
Timer tm = new Timer(5, this);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int width = (int) screenSize.getWidth();
int height = (int) screenSize.getHeight();
public BlueSquare() {
addKeyListener(this);
tm.start();
setFocusable(true);
setFocusTraversalKeysEnabled(false);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.blue);
g.fillRect(x, y, 25, 25);
repaint();
}
#Override
public void actionPerformed(ActionEvent e) {
x = x + velX;
y = y + velY;
if (x < 0) {
velX = 0;
x = 0;
}
if (x > width - 50) {
velX = 0;
x = width - 50;
}
if (y < 0) {
velY = 0;
y = 0;
}
if (y > height - 40) {
velY = 0;
y = height - 40;
}
repaint();
}
public void keyTyped(KeyEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
#Override
public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();
if (code == KeyEvent.VK_A) {
velX = -4;
velY = 0;
System.out.println("sex1");
}
if (code == KeyEvent.VK_W) {
velX = 0;
velY = -4;
System.out.println("sex2");
}
if (code == KeyEvent.VK_D) {
velX = 4;
velY = 0;
System.out.println("sex3");
}
if (code == KeyEvent.VK_S) {
velX = 0;
velY = 4;
System.out.println("sex4");
}
}
#Override
public void keyReleased(KeyEvent e) {
velX = 0;
velY = 0;
}
}
When I run the program and move the circle, it appears as if I'm drawing with a paintbrush in paint. I'm not quite sure what I did to make it to this, or what I can do to make it stop. All help is highly appreciated.
Here is my code:
import java.awt.Graphics;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.Timer;
import javax.swing.JPanel;
import java.awt.event.KeyListener;
public class MovingCar extends JPanel implements ActionListener, KeyListener {
Timer tm = new Timer(5, this);
int x = 0, y = 0, velX = 0, velY = 0;
public MovingCar()
{
tm.start();
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
}
protected void paintComponent (Graphics g) {
super.paintComponents(g);
g.drawOval(x, y, 50, 50);
}
public void actionPerformed(ActionEvent e){
x = x + velX;
y = y + velY;
repaint();
}
public void keyPressed(KeyEvent e){
int c = e.getKeyCode();
if (c == KeyEvent.VK_DOWN) {
velX = -1;
velY = 0;
}
if (c == KeyEvent.VK_UP)
{
velX = 1;
velY = 0;
}
}
public void keyTyped(KeyEvent e){}
public void keyReleased(KeyEvent e){
if (x < 0)
{
velX = 0;
x = 0;
}
if (x > 600)
{
velX = 0;
x = 0;
}
repaint();
velY = 0;
velX = 0;
}
public static void main(String[] args) {
MovingCar o = new MovingCar();
JFrame jf = new JFrame();
jf.setTitle("Circle Move");
jf.setSize(600,400);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.add(o);
jf.setVisible(true);
}
}
You're calling super.paintComponents(g); instead of super.paintComponent(g);
This snake game code. I want to add the system score in this game. So that each time the snake eating his score would be increased. But if the snake does not get food score will not increase.
How do I go about displaying the current score?
this is :
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Board extends JPanel implements ActionListener {
private final int WIDTH = 300;
private final int HEIGHT = 300;
private final int DOT_SIZE = 10;
private final int ALL_DOTS = 900;
private final int RAND_POS = 29;
private final int DELAY = 140;
private int x[] = new int[ALL_DOTS];
private int y[] = new int[ALL_DOTS];
private int dots;
private int apple_x;
private int apple_y;
private boolean left = false;
private boolean right = true;
private boolean up = false;
private boolean down = false;
private boolean inGame = true;
private Timer timer;
private Image ball;
private Image apple;
private Image head;
public Board() {
addKeyListener(new TAdapter());
setBackground(Color.black);
ImageIcon iid = new ImageIcon(this.getClass().getResource("dot.png"));
ball = iid.getImage();
ImageIcon iia = new ImageIcon(this.getClass().getResource("apple.png"));
apple = iia.getImage();
ImageIcon iih = new ImageIcon(this.getClass().getResource("head.png"));
head = iih.getImage();
setFocusable(true);
initGame();
}
public void initGame() {
dots = 3;
for (int z = 0; z < dots; z++) {
x[z] = 50 - z*10;
y[z] = 50;
}
locateApple();
timer = new Timer(DELAY, this);
timer.start();
}
public void paint(Graphics g) {
super.paint(g);
if (inGame) {
g.drawImage(apple, apple_x, apple_y, this);
for (int z = 0; z < dots; z++) {
if (z == 0)
g.drawImage(head, x[z], y[z], this);
else g.drawImage(ball, x[z], y[z], this);
}
Toolkit.getDefaultToolkit().sync();
g.dispose();
} else {
gameOver(g);
}
}
public void gameOver(Graphics g) {
String msg = "Game Over";
Font small = new Font("Helvetica", Font.BOLD, 14);
FontMetrics metr = this.getFontMetrics(small);
g.setColor(Color.white);
g.setFont(small);
g.drawString(msg, (WIDTH - metr.stringWidth(msg)) / 2,
HEIGHT / 2);
}
public void checkApple() {
if ((x[0] == apple_x) && (y[0] == apple_y)) {
dots++;
locateApple();
}
}
public void move() {
for (int z = dots; z > 0; z--) {
x[z] = x[(z - 1)];
y[z] = y[(z - 1)];
}
if (left) {
x[0] -= DOT_SIZE;
}
if (right) {
x[0] += DOT_SIZE;
}
if (up) {
y[0] -= DOT_SIZE;
}
if (down) {
y[0] += DOT_SIZE;
}
}
public void checkCollision() {
for (int z = dots; z > 0; z--) {
if ((z > 4) && (x[0] == x[z]) && (y[0] == y[z])) {
inGame = false;
}
}
if (y[0] > HEIGHT) {
inGame = false;
}
if (y[0] < 0) {
inGame = false;
}
if (x[0] > WIDTH) {
inGame = false;
}
if (x[0] < 0) {
inGame = false;
}
}
public void locateApple() {
int r = (int) (Math.random() * RAND_POS);
apple_x = ((r * DOT_SIZE));
r = (int) (Math.random() * RAND_POS);
apple_y = ((r * DOT_SIZE));
}
public void actionPerformed(ActionEvent e) {
if (inGame) {
checkApple();
checkCollision();
move();
}
repaint();
}
private class TAdapter extends KeyAdapter {
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if ((key == KeyEvent.VK_LEFT) && (!right)) {
left = true;
up = false;
down = false;
}
if ((key == KeyEvent.VK_RIGHT) && (!left)) {
right = true;
up = false;
down = false;
}
if ((key == KeyEvent.VK_UP) && (!down)) {
up = true;
right = false;
left = false;
}
if ((key == KeyEvent.VK_DOWN) && (!up)) {
down = true;
right = false;
left = false;
}
}
}
You need two things:
1) Code to add the score - you should check after the move to see whether the snake's head is at the same coordinates as the apple, if so add to the score.
2) Create a JLabel to store the value of the player's score. On each timer invoked ActionPerformed, update the text of this JLabel. You won't need to worry about Multi-threading, the action event handlers invoked by the Timer are handled in a separate thread.