Snake Java Hilfe

Diskutiere Snake Java Hilfe im Spiele- und Multimedia-Programmierung Bereich.
P

PlzNichtSchlagen

Kann mir einer sagen was mit dem Code falsch ist irgendwie wird mir nur ein Programm mit nichts drinen gezeigt obwoh es gerade noch ging. ich arbeite mit ECLIPSE. Und ich denke es liegt an der GamePanel Klasse. BIN NOCH NICHT GANZ FERTIG GEWESEN NUR IRGENDWAS IST AB DIESEN PUNKT SCHIEF GEGANGEN

package game;

import java.awt.Graphics2D;
import java.awt.Rectangle;

public class Entity {
private int x,y,size;
public Entity(int size) {
this.size = size;

}

public int getX(){

return x;

}

public int getY(){

return y;

}

public void setX(int x){

this.x = x;

}

public void setY(int y){
this.y = y;

}

public void setPosition(int x,int y)

{
this.x = x;
this.y = y;
}

public void move(int dx,int dy){
x += dx;
y += dy;





}

public Rectangle getBound(){
return new Rectangle(x, y, size, size);
}

public boolean isCollsion(Entity o){
if(o == this) return false;
return getBound().intersects(o.getBound());

}
public void render(Graphics2D g2d) {
g2d.fillRect(x + 1, y + 1, size - 2, size - 2);

}
}





MAIN:
package game;

import java.awt.Dimension;
import javax.swing.JFrame;

public class Main {


public static void main(String[] args) {
JFrame frame = new JFrame("Snake Komplexe Leistung");
frame.setContentPane (new GamePanel());
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setPreferredSize(new Dimension(GamePanel.WIDTH, GamePanel.HEIGHT));
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}
}






GamePanel:

package game;

import java.awt.Color;


import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

import javax.swing.JPanel;

@SuppressWarnings("serial")
public class GamePanel extends JPanel implements Runnable, KeyListener {

public static final int WIDTH = 400;
public static final int HEIGHT = 400;
//Render
private Graphics2D g2d;
private BufferedImage image;


//Spiel Loop
private Thread thread;
private boolean running;
private long targetTime;

//Spiel Zeugs
private final int SIZE = 10;
private Entity head,apple;
private ArrayList<Entity> snake;
private int score;
private int level;
private boolean gameover;
//Bewegung
private int dx,dy;

//Key input
private boolean up,down,right,left,start;


public GamePanel () {
setPreferredSize(new Dimension(WIDTH, HEIGHT));
setFocusable(true);
requestFocus();
addKeyListener(this);

}

@Override
public void addNotify() {
super.addNotify();
thread = new Thread(this);
thread.start();
}
private void setFPS(int fps) {
targetTime = 1000 / fps;
}

@Override
public void keyTyped(KeyEvent e) {


}

@Override
public void keyPressed(KeyEvent e) {
int k = e.getKeyCode();

if (k == KeyEvent.VK_W) up = true;
if (k == KeyEvent.VK_S) down = true;
if (k == KeyEvent.VK_A) left = true;
if (k == KeyEvent.VK_D) right = true;
if (k == KeyEvent.VK_ENTER) start = true;


}

@Override
public void keyReleased(KeyEvent e) {
int k = e.getKeyCode();

if (k == KeyEvent.VK_W) up = false;
if (k == KeyEvent.VK_S) down = false;
if (k == KeyEvent.VK_A) left = false;
if (k == KeyEvent.VK_D) right = false;
if (k == KeyEvent.VK_ENTER) start = false;

}

@Override
public void run() {
if(running) return;
init();
long startTime;
long elapsed;
long wait;
while(running) {
startTime = System.nanoTime();

update();
requestRender();


elapsed = System.nanoTime() - startTime;
wait = targetTime - elapsed / 1000000;
if(wait > 0) {
try {
Thread.sleep(wait);
}catch(Exception e) {
e.printStackTrace();
}
}

}
}
private void init() {
image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB);
g2d = image.createGraphics();
running = true;
setFPS(level * 10);
gameover = false;
level = 1;
setUplevel();


}
private void setUplevel() {
snake = new ArrayList<Entity>();
head = new Entity(SIZE);
head.setPosition(WIDTH / 2, HEIGHT/ 2);
snake.add(head);

for(int i = 1;i < 3;i++) {
Entity e = new Entity(SIZE);
e.setPosition(head.getX() + (i * SIZE) , head.getY());
snake.add(e);
}
apple = new Entity(SIZE);
setApple();
score = 0;



}
public void setApple() {
int x = (int) (Math.random() *(WIDTH - SIZE));
int y = (int) (Math.random() *(HEIGHT - SIZE));
x = x - (x % SIZE);
y = y - (y % SIZE);
apple.setPosition(x, y);

}
private void requestRender() {
render(g2d);
Graphics g = getGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();


}

private void update() {
if(gameover) {
if(start) {
setUplevel();
}

return;
}
if(up && dy == 0){
dy = -SIZE;
dx = 0;
}
if(down && dy == 0){
dy = SIZE;
dx = 0;
}
if(left && dx == 0){
dy = 0;
dx = -SIZE;
}
if(right && dx == 0 && dy != 0){
dy = 0;
dx = SIZE;
}

if(dx != 0 || dy != 0) {
for (int i = snake.size() - 1;i > 0;i--) {

snake.get(i).setPosition(
snake.get(i - 1).getX(),
snake.get(i - 1).getY()

);


}
head.move(dx, dy);
}

for(Entity e : snake) {
if(e.isCollsion(head)) {
gameover = true;
break;


}



}

if(apple.isCollsion(head)) {
score++;
setApple();

Entity e = new Entity(SIZE);
e.setPosition(-100,-100);
snake.add(e);
if(score % 10 == 0) {
level++;
if(level > 10) level = 10;
setFPS(level * 10);

}



}



if (head.getX() < 0) head.setX(WIDTH - 10);
if (head.getY() < 0) head.setY(HEIGHT - 10);
if (head.getX() > WIDTH - 10) head.setX(0);
if (head.getY() > HEIGHT - 10) head.setY(0);





}
public void render(Graphics2D g2d) {
g2d.clearRect(0, 0, WIDTH, HEIGHT);

g2d.setColor(Color.GREEN);
for(Entity e : snake) {
e.render(g2d);


}
g2d.setColor(Color.RED);
apple.render(g2d);
if(gameover) {
g2d.drawString("GameOver!", 150, 200);

}

g2d.setColor(Color.WHITE);
g2d.drawString("Score : " + score + "Level : " + level, 10, 10);
if(dx == 0 || dy == 0) {
g2d.drawString("Ready!", 150, 200);
}




}



}





ENTITY:


package game;

import java.awt.Graphics2D;
import java.awt.Rectangle;

public class Entity {
private int x,y,size;
public Entity(int size) {
this.size = size;

}

public int getX(){

return x;

}

public int getY(){

return y;

}

public void setX(int x){

this.x = x;

}

public void setY(int y){
this.y = y;

}

public void setPosition(int x,int y)

{
this.x = x;
this.y = y;
}

public void move(int dx,int dy){
x += dx;
y += dy;





}

public Rectangle getBound(){
return new Rectangle(x, y, size, size);
}

public boolean isCollsion(Entity o){
if(o == this) return false;
return getBound().intersects(o.getBound());

}
public void render(Graphics2D g2d) {
g2d.fillRect(x + 1, y + 1, size - 2, size - 2);

}
}
 
mihe7

mihe7

Ersetze den Part
Java:
Graphics g = getGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
durch ein einfaches
Java:
repaint();
und überschreibe paintComponent:
Java:
@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g); 
    if (image != null) {
        g.drawImage(image, 0, 0, null);
    }
}
 
VfL_Freak

VfL_Freak

und poste Code immer in Code-Tags (siehe meine Signatur) !!
So bekommt man ja Augenkrebs!!
VG Klaus
 
P

PlzNichtSchlagen

Ersetze den Part
Java:
Graphics g = getGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
durch ein einfaches
Java:
repaint();
und überschreibe paintComponent:
Java:
@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    if (image != null) {
        g.drawImage(image, 0, 0, null);
    }
}

Du bist mein Retter es geht wieder DANKESCHÖN :D
 
Thema: 

Snake Java Hilfe

Passende Stellenanzeigen aus deiner Region:
Anzeige

Neue Themen

Anzeige

Anzeige
Oben