resources_for_snakegame
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
resources_for_snakegame [2022/05/11 12:47] – created frchris | resources_for_snakegame [2022/05/14 09:17] (current) – frchris | ||
---|---|---|---|
Line 1: | Line 1: | ||
**Snake Game** | **Snake Game** | ||
+ | {{:: | ||
- | < | + | * {{ ::turn.java |Turn.java}} |
- | import | + | * {{ ::segment.java |Segment.java}} |
- | import java.awt.Font; | + | * {{ ::snake.java |Snake.java}} |
- | import | + | * {{ ::snakegame.java |SnakeGame.java}} |
- | import | + | * {{ ::snakegame.jar }} |
- | import java.awt.event.ActionListener; | + | |
- | import | + | |
- | import | + | |
- | import javax.swing.JFrame; | + | |
- | import javax.swing.JPanel; | + | |
- | import javax.swing.Timer; | + | |
- | public class SnakeGame extends JPanel implements KeyListener, | + | This is some starter |
- | { | + | - check for collisions |
- | public static int WIDTH=800; | + | |
- | public static int HEIGHT=600; | + | - keep score |
- | public static int SIZE=30; | + | - spawn fruit to eat |
- | + | ||
- | private Font titleFont, regularFont; | + | |
- | private int x,y; | + | |
- | private Timer timer; | + | |
- | private Snake snake; | + | |
- | + | ||
- | public SnakeGame() | + | |
- | { | + | |
- | + | ||
- | snake = new Snake(WIDTH/ | + | |
- | titleFont = new Font(" | + | |
- | regularFont = new Font(" | + | |
- | y=0; | + | |
- | x=200; | + | |
- | timer = new Timer(10, this); //1000=1 seconds | + | |
- | timer.start(); | + | |
- | + | ||
- | } | + | |
- | + | ||
- | public static void main(String[] args) { | + | |
- | SnakeGame app= new SnakeGame(); | + | |
- | JFrame window = new JFrame(" | + | |
- | window.setSize(WIDTH, | + | |
- | window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | + | |
- | window.getContentPane().add(app); | + | |
- | window.addKeyListener(app); | + | |
- | // | + | |
- | window.setVisible(true); | + | |
- | + | ||
- | } | + | |
- | + | ||
- | public void paintComponent(Graphics g){ | + | |
- | super.paintComponent(g); | + | |
- | g.setColor(Color.WHITE); | + | |
- | g.fillRect(0, | + | |
- | g.setColor(Color.BLUE); | + | |
- | g.setFont(titleFont); | + | |
- | g.drawString(" | + | |
- | g.setColor(Color.BLACK); | + | |
- | g.setFont(regularFont); | + | |
- | snake.draw(g); | + | |
- | + | ||
- | } | + | |
- | + | ||
- | // These 3 methods need to be declares to implement the KeyListener Interface | + | |
- | @Override | + | |
- | public void keyTyped(KeyEvent e) {} | + | |
- | + | ||
- | @Override | + | |
- | public void keyPressed(KeyEvent e) { | + | |
- | int code=e.getKeyCode(); | + | |
- | + | ||
- | if (code ==39) // right | + | |
- | snake.changeDirection('e'); | + | |
- | else if (code == 37 ) //left | + | |
- | snake.changeDirection(' | + | |
- | else if (code == 38 ) //left | + | |
- | snake.changeDirection(' | + | |
- | else if (code == 40 ) //left | + | |
- | snake.changeDirection(' | + | |
- | + | ||
- | repaint(); | + | |
- | } | + | |
- | + | ||
- | @Override | + | |
- | public void keyReleased(KeyEvent e) {} | + | |
- | + | ||
- | @Override | + | |
- | public void actionPerformed(ActionEvent e) { | + | |
- | + | ||
- | if (e.getSource()==timer){ | + | |
- | snake.move(); | + | |
- | } | + | |
- | repaint(); | + | |
- | } | + | |
- | + | ||
- | } | + | |
- | </ | + | |
- | < | + | |
- | import java.awt.*; | + | |
- | + | ||
- | public class Snake | + | |
- | { | + | |
- | // instance variables | + | |
- | private int x, y, size; | + | |
- | + | ||
- | private char direction; | + | |
- | + | ||
- | /** | + | |
- | * Constructor | + | |
- | */ | + | |
- | public Snake(int x, int y, int size) | + | |
- | { | + | |
- | // initialise instance variables | + | |
- | this.x = x; | + | |
- | this.y = y; | + | |
- | this.size = size; | + | |
- | direction = ' | + | |
- | } | + | |
- | public void draw(Graphics g) | + | |
- | { | + | |
- | + | ||
- | | + | |
- | if (direction == ' | + | |
- | | + | |
- | else | + | |
- | | + | |
- | } | + | |
- | /** | + | |
- | * An example of a method - replace this comment | + | |
- | * | + | |
- | * @param | + | |
- | * @return | + | |
- | */ | + | |
- | public void move() | + | |
- | { | + | |
- | if (direction == ' | + | |
- | x++; | + | |
- | else if (direction == ' | + | |
- | x--; | + | |
- | else if (direction == ' | + | |
- | y--; | + | |
- | else if (direction == ' | + | |
- | y++; | + | |
- | + | ||
- | } | + | |
- | public void changeDirection(char ch) | + | |
- | { | + | |
- | direction = ch; | + | |
- | } | + | |
- | } | + | |
- | + | ||
- | </ | + | |
resources_for_snakegame.1652287665.txt.gz · Last modified: 2022/05/11 12:47 by frchris