Advertisement
Guest User

Oblig9

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