Crenox

Spaceship Battle

May 5th, 2014
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.49 KB | None | 0 0
  1. package com.samkough.spaceshipbattle.main;
  2.  
  3. import javax.swing.JFrame;
  4.  
  5. @SuppressWarnings("serial")
  6. public class Main extends JFrame
  7. {
  8. public Main()
  9. {
  10. add(new Board());
  11.  
  12. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  13. setSize(400, 400);
  14. setLocationRelativeTo(null);
  15. setTitle("Spaceship Battle");
  16. setResizable(false);
  17. setVisible(true);
  18. }
  19.  
  20. public static void main(String[] args)
  21. {
  22. new Main();
  23. }
  24. }
  25. -------------------------------------------------------------------------------------------------------------------------------------
  26. package com.samkough.spaceshipbattle.main;
  27.  
  28. import java.awt.Color;
  29. import java.awt.Graphics;
  30. import java.awt.Graphics2D;
  31. import java.awt.Toolkit;
  32. import java.awt.event.ActionEvent;
  33. import java.awt.event.ActionListener;
  34. import java.awt.event.KeyAdapter;
  35. import java.awt.event.KeyEvent;
  36. import javax.swing.JPanel;
  37. import javax.swing.Timer;
  38. import com.samkough.spaceshipbattle.entity.Craft;
  39.  
  40.  
  41. @SuppressWarnings("serial")
  42. public class Board extends JPanel implements ActionListener
  43. {
  44. private Timer timer;
  45. private Craft craft;
  46.  
  47. public Board()
  48. {
  49. addKeyListener(new TAdapter());
  50. setFocusable(true);
  51. setBackground(Color.BLACK);
  52. setDoubleBuffered(true);
  53.  
  54. craft = new Craft();
  55.  
  56. timer = new Timer(5, this);
  57. timer.start();
  58. }
  59.  
  60.  
  61. public void paint(Graphics g)
  62. {
  63. super.paint(g);
  64.  
  65. Graphics2D g2d = (Graphics2D)g;
  66. // In the paint() method, we draw the spacecraft. We get the image and the coordinates from the sprite class.
  67. g2d.drawImage(craft.getImage(), craft.getX(), craft.getY(), this);
  68.  
  69. Toolkit.getDefaultToolkit().sync();
  70. g.dispose();
  71. }
  72.  
  73. // The actionPerformed() method is called every 5ms. We move the sprite and repaint the board.
  74. public void actionPerformed(ActionEvent e)
  75. {
  76. craft.move();
  77. repaint();
  78. }
  79.  
  80.  
  81. private class TAdapter extends KeyAdapter
  82. {
  83. public void keyReleased(KeyEvent e)
  84. {
  85. craft.keyReleased(e);
  86. }
  87.  
  88. public void keyPressed(KeyEvent e)
  89. {
  90. craft.keyPressed(e);
  91. }
  92. }
  93.  
  94. }
  95. -------------------------------------------------------------------------------------------------------------------------------------
  96. /* This class represents a spacecraft. In this class we keep the image of the sprite and the coordinates of the sprite.
  97. * The keyPressed() and keyReleased() methods control whether the sprite is moving or is in standstill.
  98. */
  99.  
  100. package com.samkough.spaceshipbattle.entity;
  101.  
  102. import java.awt.Image;
  103. import java.awt.event.KeyEvent;
  104. import javax.swing.ImageIcon;
  105.  
  106. public class Craft
  107. {
  108.  
  109. private String craft = "craft.png";
  110.  
  111. private int dx;
  112. private int dy;
  113. private int x;
  114. private int y;
  115. private Image image;
  116.  
  117. public Craft()
  118. {
  119. ImageIcon ii = new ImageIcon(this.getClass().getResource(craft));
  120. image = ii.getImage();
  121. x = 40;
  122. y = 60;
  123. }
  124.  
  125. // The move() method changes the coordinates of the sprite. These x, y values are used in the paint() method to draw the image of the sprite.
  126. public void move()
  127. {
  128. x += dx;
  129. y += dy;
  130. }
  131.  
  132. public int getX()
  133. {
  134. return x;
  135. }
  136.  
  137. public int getY()
  138. {
  139. return y;
  140. }
  141.  
  142. public Image getImage()
  143. {
  144. return image;
  145. }
  146.  
  147. public void keyPressed(KeyEvent e)
  148. {
  149. int key = e.getKeyCode();
  150.  
  151. if (key == KeyEvent.VK_LEFT)
  152. {
  153. dx = -1;
  154. }
  155.  
  156. if (key == KeyEvent.VK_RIGHT)
  157. {
  158. dx = 1;
  159. }
  160.  
  161. if (key == KeyEvent.VK_UP)
  162. {
  163. dy = -1;
  164. }
  165.  
  166. if (key == KeyEvent.VK_DOWN)
  167. {
  168. dy = 1;
  169. }
  170. }
  171.  
  172. public void keyReleased(KeyEvent e)
  173. {
  174. int key = e.getKeyCode();
  175.  
  176. // When we release the left cursor key, we set the dx variable to zero. The spacecraft will stop moving.
  177. if (key == KeyEvent.VK_LEFT)
  178. {
  179. dx = 0;
  180. }
  181.  
  182. if (key == KeyEvent.VK_RIGHT)
  183. {
  184. dx = 0;
  185. }
  186.  
  187. if (key == KeyEvent.VK_UP)
  188. {
  189. dy = 0;
  190. }
  191.  
  192. if (key == KeyEvent.VK_DOWN)
  193. {
  194. dy = 0;
  195. }
  196. }
  197. }
Advertisement
Add Comment
Please, Sign In to add comment