-
Beginning of snake game
I'm trying to make a snake game equivalent to the one found on many cell phones.
I'm having trouble getting my snake to be drawn correctly according to keyboard events. The keyboard events fire and my program picks up on the events but the snake is not drawn correctly or really at all for that case. The keys to control the snake are the four arrow keys. My problem lies in using the Graphics object-- i'm not exactly sure what to do with it.
Thanks for the help!!
HTML Code:
//SnakePane.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class SnakePanel extends JPanel implements KeyListener{
Snake snake;
Graphics g;
public SnakePanel(){
super();
this.setBackground(Color.yellow);
snake = new Snake(Color.black);
addKeyListener(this);
//give SnakePanel class keyboard focus
this.setFocusable(true);
}
//KeyListener interface methods
public void keyPressed(KeyEvent e){
System.out.println("keyPressed");
update(g, e);
}
public void keyReleased(KeyEvent e){
System.out.println("keyReleased");
}
public void keyTyped(KeyEvent e){
System.out.println("keyTyped");
}
public void paintComponent(Graphics graphics){
System.out.println("paint");
g = graphics;
//display snake for the first time
snake.drawSnake(g);
}
//overload update() method
public void update(Graphics g, KeyEvent e){
int keyCode = e.getKeyCode();
//get arrow key
switch(keyCode){
case KeyEvent.VK_UP:
//System.out.println("moveUp");
snake.moveUp(g);
break;
case KeyEvent.VK_DOWN:
snake.moveDown(g);
break;
case KeyEvent.VK_RIGHT:
snake.moveRight(g);
break;
case KeyEvent.VK_LEFT:
snake.moveLeft(g);
break;
}
}
public static void main(String[] args){
JFrame frame = new JFrame("Snake");
frame.getContentPane().add(new SnakePanel());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200,200);
frame.setLocation(100,100);
frame.setVisible(true);
}
}
*****************************
And below is the Snake.java class**
*****************************
HTML Code:
import java.awt.*;
public class Snake{
int xPos, yPos;
int size = 10;
Color color;
public Snake(Color col){
xPos = 50;
yPos = 50;
color = col;
}
public void drawSnake(Graphics g){
g.setColor(Color.black);
g.fillRect(xPos,yPos,size,size);
}
public void moveUp(Graphics g){
g.setColor(Color.red);
//decrement yPos
yPos = yPos - size;
g.fillRect(xPos, yPos, size, size);
}
public void moveRight(Graphics g){
g.setColor(Color.red);
xPos = xPos + size;
g.fillRect(xPos, yPos, size, size);
}
public void moveLeft(Graphics g){
g.setColor(Color.red);
xPos = xPos - size;
g.fillRect(xPos, yPos, size, size);
}
public void moveDown(Graphics g){
g.setColor(Color.red);
yPos = yPos + size;
g.fillRect(xPos, yPos, size, size);
}
}
-
Have you tried adding println() statements to your code to see how the code flows and where it goes?
Are the various draw methods being called? Is your update() method being called?
-
Graphics
Well I don't understand java.awt.Graphics;
In my code SnakePanel.java I have an instance variable Graphics g;
Then in my paintComponent(Graphics graphics) method (SnakePanel extends JPanel) I say: "g = graphics"
Then, in my snake class I have methods to move the snake:
i.e.: void moveUp(Graphics g){
g.setColor(Color.red);
g.fillRect(newXPos, newYPos, snakeSize, snakeSize);
}
So in the update method of my SnakePanel class I get the keyboard events and say if the up arrow key is hit then I call snake.moveUp(g); snake is an instance of the Snake.java class.
This is the g referred to above.
I don't know if this is how to do this or if maybe I should create a graphics object in the snake class?
Also, since this is a simple game I didn't see a need for double-buffering. Do you think that is needed to counter flickering or will there likely be no flickering?
Thanks a lot!
-
I've never tried saving the Graphics object passed to the paintxxx() method to use in other methods. I've always done all the painting from the paint() method (I code only AWT not Swing).
A listener detects a movement, sets some global variables and calls repaint() which sometime later will cause a call to paint() where the global variables are inspected and the necessary painting done. It might be slightly different in Swing.
Similar Threads
-
By f4cepl4nt in forum C++
Replies: 3
Last Post: 03-08-2005, 11:24 PM
-
Replies: 0
Last Post: 08-11-2001, 04:53 PM
-
By Alex in forum web.announcements
Replies: 0
Last Post: 08-09-2001, 03:01 PM
-
By Alex in forum java.announcements
Replies: 0
Last Post: 08-09-2001, 02:55 PM
-
Replies: 0
Last Post: 07-31-2000, 07:35 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|