Hey ich habe schonmal etwas hier gepostet, nun würde ich gerne fragen ob man dieses Spiel noch irgendwie verbessern könnte.
Edit: Ausserdem habe ich probleme das die Tasteneingaben ein wenig spät kommen das heisst wenn ich "UP" drücke kann ich nicht direkt "RIGHT" drücken. Weiss weiner wie man das besser machen könnte? Und ebenfalls passiert es immer das aus irgendwelchen Gründen die Schlange manchmal in sich reinfährt und stirbt. Was habe ich falsch gemacht?
MAIN KLASSE:
GAMEPANEL KLASSE:
ENTITY KLASSE:
Edit: Ausserdem habe ich probleme das die Tasteneingaben ein wenig spät kommen das heisst wenn ich "UP" drücke kann ich nicht direkt "RIGHT" drücken. Weiss weiner wie man das besser machen könnte? Und ebenfalls passiert es immer das aus irgendwelchen Gründen die Schlange manchmal in sich reinfährt und stirbt. Was habe ich falsch gemacht?
MAIN KLASSE:
Javascript:
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 KLASSE:
Javascript:
package game;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Graphics;
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 kopf,apfel;
private ArrayList<Entity> schlange;
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_UP) up = true;
if (k == KeyEvent.VK_DOWN) down = true;
if (k == KeyEvent.VK_LEFT) left = true;
if (k == KeyEvent.VK_RIGHT) right = true;
if (k == KeyEvent.VK_ENTER) start = true;
}
@Override
public void keyReleased(KeyEvent e) {
int k = e.getKeyCode();
if (k == KeyEvent.VK_UP) up = false;
if (k == KeyEvent.VK_DOWN) down = false;
if (k == KeyEvent.VK_LEFT) left = false;
if (k == KeyEvent.VK_RIGHT) 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;
setUplevel();
}
private void setUplevel() {
schlange = new ArrayList<Entity>();
kopf = new Entity(SIZE);
kopf.setPosition(WIDTH / 2, HEIGHT/ 2);
schlange.add(kopf);
for(int i = 1;i < 3;i++) {
Entity e = new Entity(SIZE);
e.setPosition(kopf.getX() + (i * SIZE) , kopf.getY());
schlange.add(e);
}
apfel = new Entity(SIZE);
setApple();
score = 0;
gameover = false;
level = 1;
dx = dy = 0;
setFPS(level * 10);
}
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);
apfel.setPosition(x, y);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (image != null) {
g.drawImage(image, 0, 0, null);
}
}
private void requestRender() {
render(g2d);
repaint();
}
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 = schlange.size() - 1;i > 0;i--) {
schlange.get(i).setPosition(
schlange.get(i - 1).getX(),
schlange.get(i - 1).getY()
);
}
kopf.move(dx, dy);
}
for(Entity e : schlange) {
if(e.isCollsion(kopf)) {
gameover = true;
break;
}
}
if(apfel.isCollsion(kopf)) {
score++;
setApple();
Entity e = new Entity(SIZE);
e.setPosition(-100,-100);
schlange.add(e);
if(score % 10 == 0) {
level++;
if(level > 10) level = 10;
setFPS(level * 10);
}
}
if (kopf.getX() < 0) kopf.setX(WIDTH - 10);
if (kopf.getY() < 0) kopf.setY(HEIGHT - 10);
if (kopf.getX() > WIDTH - 10) kopf.setX(0);
if (kopf.getY() > HEIGHT - 10) kopf.setY(0);
}
public void render(Graphics2D g2d) {
g2d.clearRect(0, 0, WIDTH, HEIGHT);
g2d.setColor(Color.GREEN);
for(Entity e : schlange) {
e.render(g2d);
}
g2d.setColor(Color.RED);
apfel.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 KLASSE:
Javascript:
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);
}
}
Zuletzt bearbeitet: