resources_for_snakegame
Differences
This shows you the differences between two versions of the page.
Next revisionBoth sides next revision | |||
resources_for_snakegame [2022/05/11 12:47] – created frchris | resources_for_snakegame [2022/05/11 14:40] – frchris | ||
---|---|---|---|
Line 1: | Line 1: | ||
**Snake Game** | **Snake Game** | ||
+ | * {{ ::turn.java |Turn.java}} | ||
+ | * {{ :: | ||
+ | * {{ :: | ||
+ | * {{ :: | ||
- | < | ||
- | import java.awt.Color; | ||
- | import java.awt.Font; | ||
- | import java.awt.Graphics; | ||
- | import java.awt.event.ActionEvent; | ||
- | import java.awt.event.ActionListener; | ||
- | import java.awt.event.KeyEvent; | ||
- | import java.awt.event.KeyListener; | ||
- | import javax.swing.JFrame; | ||
- | import javax.swing.JPanel; | ||
- | import javax.swing.Timer; | ||
- | |||
- | public class SnakeGame extends JPanel implements KeyListener, | ||
- | { | ||
- | public static int WIDTH=800; | ||
- | public static int HEIGHT=600; | ||
- | public static int SIZE=30; | ||
- | |||
- | 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(' | ||
- | 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 - replace the example below with your own | ||
- | private int x, y, size; | ||
- | | ||
- | private char direction; | ||
- | | ||
- | /** | ||
- | * Constructor for objects of class Snake | ||
- | */ | ||
- | 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 == ' | ||
- | | ||
- | | ||
- | | ||
- | } | ||
- | /** | ||
- | * An example of a method - replace this comment with your own | ||
- | | ||
- | * @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.txt · Last modified: 2022/05/14 09:17 by frchris