我正在制作一个蛇游戏,很好,完成了,我可以导出它和所有东西。只是一个简单的问题;无论什么原因,每次身体部位增加时,蛇都会留下空间。我没有计算出确切的数量,但是每次身体部位增加时,尾巴都会留下更多的空间。我试图寻找问题。
这是我的第一个游戏项目,所以请不要评判我的 xd。
这些是密码.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.sql.Time;
import java.util.Random;
import javax.swing.Timer;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
public class GamePanel extends JPanel implements ActionListener {
static final int SCREEN_HEIGHT = 600;
static final int SCREEN_WIDTH = 600;
static final int UNIT_SIZE = 25;
static final int GAME_UNITS = (SCREEN_HEIGHT*SCREEN_WIDTH)/UNIT_SIZE;
static final int DELAY = 175;
final int x[] = new int [GAME_UNITS];
final int y[] = new int [GAME_UNITS];
int bodyParts = 1;
int applesEaten;
int appleX;
int appleY;
char direction = 'R';
boolean running = false;
Timer timer;
Random random;
private Image tail;
private Image apple;
private Image head;
GamePanel() {
random = new Random();
this.setPreferredSize(new Dimension(SCREEN_HEIGHT,SCREEN_WIDTH));
this.setBackground(Color.black);
this.setFocusable(true);
this.addKeyListener(new MyKeyAdapter());
startGame();
loadImages();
}
public void startGame() {
running = true;
newApple();
Timer timer = new Timer(DELAY,this);
timer.start();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
draw(g);
}
private void loadImages() {
ImageIcon iid = new ImageIcon("src/res/tail.png");
tail = iid.getImage();
ImageIcon iia = new ImageIcon("src/res/apple.png");
apple = iia.getImage();
ImageIcon iih = new ImageIcon("src/res/head.png");
head = iih.getImage();
}
public void draw(Graphics g) {
g.setColor(Color.red);
for (int i = 0; i < SCREEN_HEIGHT/UNIT_SIZE; i++) {
//g.drawLine(i*UNIT_SIZE, 0 , i*UNIT_SIZE, SCREEN_HEIGHT);
// g.drawLine(0, i*UNIT_SIZE, SCREEN_WIDTH, i*UNIT_SIZE); //grids
}
g.drawImage(apple, appleX, appleY, this); //apple
for (int z = 0; z < bodyParts; z++) {
if (z == 0) {
g.drawImage(head, x[z], y[z], this);
} else {
g.drawImage(tail, x[z], y[z], this);
}
}
Toolkit.getDefaultToolkit().sync();
}
public void newApple() {
appleX = random.nextInt((SCREEN_WIDTH/UNIT_SIZE))*UNIT_SIZE;
appleY = random.nextInt((SCREEN_HEIGHT/UNIT_SIZE))*UNIT_SIZE;
}
public void move() {
for(int z = bodyParts;z>0;z--) {
x[z] = x[(z-1)];
y[z] = y[(z-1)];
switch(direction) {
case 'U':
y[0] -= UNIT_SIZE;
break;
case 'D':
y[0] += UNIT_SIZE;
break;
case 'L':
x[0] -= UNIT_SIZE;
break;
case 'R':
x[0] += UNIT_SIZE;
break;
}
}
}
public void checkApple() {
if((x[0] == appleX) && (y[0] == appleY)) {
bodyParts++;
applesEaten++;
newApple();
}
}
public void checkCollisions() {
//checks if head collides with body
for(int i = bodyParts;i<0;i--) {
if((x[0] == x[i])&& (y[0] == y[i])) {
running = false;
}
}
//checks if head touches left border
if(x[0] < 0) {
running = false;
}
//check if head touches right border
if(x[0] > SCREEN_WIDTH) {
running = false;
}
//check if head touches top border
if(y[0] < 0) {
running = false;
}
//check if head touches bottom border
if(y[0] > SCREEN_HEIGHT) {
running = false;
}
if(!running) {
timer.stop();
}
}
public void gameOver(Graphics g) {
}
@Override
public void actionPerformed(ActionEvent e) {
if(running) {
move();
checkApple();
checkCollisions();
loadImages();
}
repaint();
}
public class MyKeyAdapter extends KeyAdapter {
@Override
public void keyPressed(KeyEvent e) {
switch(e.getKeyCode()) {
case KeyEvent.VK_LEFT:
if(direction != 'R') {
direction = 'L';
}
break;
case KeyEvent.VK_RIGHT:
if(direction != 'L') {
direction = 'R';
}
break;
case KeyEvent.VK_UP:
if(direction != 'D') {
direction = 'U';
}
break;
case KeyEvent.VK_DOWN:
if(direction != 'U') {
direction = 'D';
}
break;
}
}
}
}
这是当 bodyParts =2bodyParts = 2;
并且,这是当 bodyParts =3bodyParts = 3;
本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处
评论列表(17条)