Guest User

Untitled

a guest
Jul 22nd, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. import java.awt.*;
  2. import javax.swing.*;
  3. import java.awt.event.*; // needed for event handling
  4. import java.awt.Toolkit;
  5. import java.util.Timer;
  6. import java.util.TimerTask;
  7. import java.util.Date;
  8. import java.awt.BorderLayout;
  9.  
  10. public class DrawGame extends JPanel implements KeyListener, ActionListener {
  11. // This attribute keeps track of the number of "time steps" the
  12. // program has taken so far. At each time step, the snakes should
  13. // move, we should check for intersections, and check for keyboard events,
  14. // etc. This attribute is really just for convenience in debugging,
  15. // and is not needed for the functioning of the game.
  16. int totalTimeSteps=0;
  17.  
  18. Toolkit toolkit;
  19. Timer timer;
  20.  
  21. Snake snake = new Snake();
  22.  
  23. JButton quit = new JButton("Quit");
  24. JTextField typingArea;
  25.  
  26. boolean oval = true;
  27.  
  28. public DrawGame() {
  29. // First thing to do: Start up the periodic task:
  30. System.out.println("About to start the snake.");
  31. startSnake(200); // Argument is number of milliseconds per snake move.
  32. System.out.println("Snake started.");
  33.  
  34. setBackground(Color.yellow);
  35. this.add(quit); // place button in panel
  36. quit.addActionListener(this); // panel is listener for button
  37.  
  38. typingArea = new JTextField(20);
  39. typingArea.addKeyListener(this);
  40. add(typingArea, BorderLayout.PAGE_START);
  41.  
  42. }
  43.  
  44. public void startSnake(int milliseconds) {
  45. toolkit = Toolkit.getDefaultToolkit();
  46. timer = new Timer();
  47. Date firstTime = new Date(); // Start task now.
  48. timer.schedule(new AdvanceTheSnakeTask(), firstTime, milliseconds);
  49. }
  50.  
  51. class AdvanceTheSnakeTask extends TimerTask {
  52. public void run() {
  53.  
  54. // Put stuff here that should happen during every advance.
  55. //toolkit.beep();
  56.  
  57. snake.move();
  58.  
  59. repaint();
  60. }
  61. }
  62.  
  63. public void paintComponent(Graphics g) {
  64. super.paintComponent(g);
  65.  
  66. snake.paint(g);
  67. }
  68.  
  69. public void keyTyped(KeyEvent e) {
  70. if (e.getKeyChar()=='a') {
  71. snake.dirX=-1;
  72. snake.dirY=0;
  73. }
  74. if (e.getKeyChar()=='s') {
  75. snake.dirX=1;
  76. snake.dirY=0;
  77. }
  78. if (e.getKeyChar()=='w') {
  79. snake.dirX=0;
  80. snake.dirY=-1;
  81. }
  82. if (e.getKeyChar()=='z') {
  83. snake.dirX=0;
  84. snake.dirY=1;
  85. }
  86. }
  87.  
  88. public void keyPressed(KeyEvent e) {
  89. }
  90.  
  91. /** Handle the key released event from the text field. */
  92. public void keyReleased(KeyEvent e) {
  93. }
  94.  
  95. public void actionPerformed(ActionEvent e){
  96. if (e.getSource() == quit)
  97. System.exit(0);
  98. }
  99. }
Add Comment
Please, Sign In to add comment