我一直致力于我的第一个项目:蛇游戏。我看了一个视频,我做了这个。然而它不起作用,当我运行它我的蛇不动。有人可以帮助我如何让它工作。
var str = 'Twas th0e ni0ght befo100re Xm305as...';
var char = str.split('');
for(var i=0; i<str.length; i++){
if((i==0 || char[i] != " ") && ! isNaN(char[i]) && isNaN(char[i+1]))
char[i] = 'o';
else if(isNaN(char[i-1]) && ! isNaN(char[i]) && (char[i] != " " || i+1 == str.length))
char[i] = 'o';
else if(isNaN(char[i-1]) && ! isNaN(char[i]) && char[i] != " " && isNaN(char[i+1]))
char[i] = 'o';
document.getElementById("demo").innerHTML = document.getElementById("demo").innerHTML + char[i];
}
实体类
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;
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;
//Game Loop
private Thread thread;
private boolean running;
private long targetTime;
//Game Stuff
private final int SIZE = 10;
private Entity head, apple;
private ArrayList<Entity> snake;
private int score;
private int level;
private boolean gameover;
//Movement
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;
}
// Toetsenbord input --> beweging van de snake
@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 keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
//Snelheid van de snake
@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();
}
//Hoe het level eruit ziet
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);
score = 0;
gameover = false;
level = 1;
dx = dy = 0;
setFPS(level*10);
}
//Waar het fruit komt
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();
}
//Hoe dat ding beweegt
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.isCollision(head)) {
gameover = true;
break;
}
}
//Punten erbij en langer worden als de snake fruit eet
if (apple.isCollision(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);
if (head.getY() < 0) head.setY(HEIGHT);
if (head.getX() > 0) head.setX(0);
if (head.getY() > 0) 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);
//Tekst voor en na je doodgaat
g2d.setColor(Color.WHITE);
g2d.drawString("Score :" + score + "Level : " + level, 10, 10);
}
}
答案 0 :(得分:0)
我知道这是关于Stack Overflow的第一个问题,所以欢迎。即使这通常不是这里所需的问题类型,我也想指出代码中的一个小错误导致蛇卡在0,0平方。当你检查墙碰撞时:
if (head.getX() > 0) head.setX(0);
if (head.getY() > 0) head.setY(0);
应该是:
if (head.getX() > WIDTH) head.setX(0);
if (head.getY() > HEIGHT) head.setY(0);
我之所以回答这个问题只是因为我知道对发展世界不熟悉的痛苦。希望你能继续玩这些代码并享受很多乐趣。