Advertisement
Guest User

Untitled

a guest
Oct 30th, 2014
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. //Oblig9Runner
  2.  
  3. public class Oblig9Runner {
  4.  
  5. public static void main(String[] args) {
  6. // TODO Auto-generated method stub
  7.  
  8. Oblig9Runner snake = new Oblig9Runner();
  9. }
  10.  
  11. }
  12.  
  13. //Oblig9SnakeGame
  14.  
  15. import java.awt.Color;
  16. import java.awt.Graphics;
  17. import java.awt.event.ActionEvent;
  18. import java.awt.event.ActionListener;
  19. import java.awt.event.KeyEvent;
  20. import java.awt.event.KeyListener;
  21.  
  22. import javax.swing.JFrame;
  23. import javax.swing.Timer;
  24.  
  25.  
  26. public class Oblig9SnakeGame extends JFrame implements KeyListener, ActionListener {
  27.  
  28. Oblig9SnakeElement se = new Oblig9SnakeElement();
  29.  
  30. public Oblig9SnakeGame(){
  31. this.setVisible(true);
  32. this.setSize(1000,750);
  33. this.setTitle("SNAKE");
  34. Color farge2 = new Color(0,0,0);
  35. this.setBackground(farge2);
  36. Timer t = new Timer(10, this);
  37. t.start();
  38. this.addKeyListener(this);
  39.  
  40. }
  41.  
  42. public void drawStuff(){
  43. Graphics g = this.getGraphics();
  44. g.clearRect(0, 0, this.getWidth(), this.getHeight());
  45. this.se.drawMe(g);
  46. }
  47.  
  48. @Override
  49. public void keyPressed(KeyEvent arg0) {
  50. // TODO Auto-generated method stub
  51. if(arg0.getKeyCode()==arg0.VK_UP){
  52. se.y-=10;
  53. System.out.println("opp");
  54. }
  55. if(arg0.getKeyCode()==arg0.VK_DOWN){
  56. se.x+=10;
  57. System.out.println("ned");
  58. }
  59. if(arg0.getKeyCode()==arg0.VK_LEFT){
  60. se.x+=10;
  61. System.out.println("venstre");
  62. }
  63. if(arg0.getKeyCode()==arg0.VK_RIGHT){
  64. se.x-=10;
  65. System.out.println("høyre");
  66. }
  67.  
  68. }
  69.  
  70. @Override
  71. public void keyReleased(KeyEvent arg0) {
  72. // TODO Auto-generated method stub
  73.  
  74. }
  75.  
  76. @Override
  77. public void keyTyped(KeyEvent arg0) {
  78. // TODO Auto-generated method stub
  79. this.drawStuff();
  80.  
  81. }
  82.  
  83. @Override
  84. public void actionPerformed(ActionEvent e) {
  85. // TODO Auto-generated method stub
  86.  
  87. }
  88.  
  89. }
  90.  
  91. //Oblig9GrafikkElement
  92.  
  93. import java.awt.Color;
  94. import java.awt.Graphics;
  95.  
  96.  
  97. public class Oblig9GrafikkElement {
  98.  
  99. int x;
  100. int y;
  101. int bredde;
  102. int hoyde;
  103. Color farge;
  104. Color farge2;
  105.  
  106. public void drawMe(Graphics g){
  107. g.setColor(farge);
  108. g.fillRect(x, y, bredde, hoyde);
  109. }
  110. }
  111.  
  112. // Oblig9SnakeElement
  113.  
  114. import java.awt.Color;
  115.  
  116.  
  117. public class Oblig9SnakeElement extends Oblig9GrafikkElement {
  118.  
  119. public Oblig9SnakeElement(){
  120. this.x = 150;
  121. this.y = 150;
  122. this.bredde = 40;
  123. this.hoyde = 40;
  124. this.farge = new Color(0,255,0);
  125. }
  126.  
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement