Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Graphics;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- import javax.swing.JFrame;
- public class Oblig9SnakeGame extends JFrame implements KeyListener {
- Oblig9SnakeElement se = new Oblig9SnakeElement();
- int oppoverpil = 38;
- int nedoverpil = 40;
- int venstrepil = 37;
- int høyrepil = 39;
- public Oblig9SnakeGame(){
- this.setVisible(true);
- this.addKeyListener(this);
- }
- public void drawStuff(){
- Graphics g = this.getGraphics();
- g.clearRect(0, 0, this.se.bredde, this.se.høyde);
- this.se.drawMe(g);
- }
- @Override
- public void keyPressed(KeyEvent arg0) {
- // TODO Auto-generated method stub
- if(arg0.getKeyCode() == oppoverpil ){
- System.out.println("Oppoverpil");
- se.x = se.x-1;
- this.drawStuff();
- }
- if(arg0.getKeyCode() == nedoverpil ){
- System.out.println("Nedoverpil");
- se.x = se.x+1;
- this.drawStuff();
- }
- if(arg0.getKeyCode() == høyrepil ){
- System.out.println("Høyrepilpil");
- se.y = se.y+1;
- this.drawStuff();
- }
- if(arg0.getKeyCode() == venstrepil ){
- System.out.println("Venstrepil");
- se.y = se.y-1;
- this.drawStuff();
- }
- }
- @Override
- public void keyReleased(KeyEvent arg0) {
- // TODO Auto-generated method stub
- }
- @Override
- public void keyTyped(KeyEvent arg0) {
- // TODO Auto-generated method stub
- }
- }
- import java.awt.Color;
- import java.awt.Graphics;
- public class Oblig9GrafikkElement {
- public int x = 50;
- public int y = 0;
- public int bredde = 20;
- public int høyde = 20;
- public Color farge = Color.black;
- public void drawMe(Graphics g){
- g.setColor(this.farge);
- g.fillRect(this.y,this.x,this.bredde,this.høyde);
- }
- }
- public class Oblig9Runner {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Oblig9SnakeGame f = new Oblig9SnakeGame();
- }
- }
- import java.awt.Color;
- import java.awt.Graphics;
- public class Oblig9SnakeElement extends Oblig9GrafikkElement {
- Oblig9SnakeElement(){
- this.x = 40;
- this.y = 0;
- this.bredde = 30;
- this.høyde = 30;
- this.farge = Color.blue;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement