我正在遵循 youtube 上的教程来开发贪吃蛇游戏。 https://www.youtube.com/watch?v=VXGwSp28DlE&list=PLJSII25WrAz6d_vfRsMGU2SG_e_x8ajtX&index=2
我在创建蛇体时遇到问题。根据导师的说法,我们需要在模块
g.clearRect(0, 0, WIDTH, HEIGHT);
中为蛇的运动添加 public void paint(Graphics g)
。
但是当我添加这个时,蛇不会出现在我的屏幕上。我照原样遵循了导师的要求,但我遇到了问题。请帮助我。
//Screen.java
package game.graphics;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.ArrayList;
import javax.swing.JPanel;
import entities.BodyParts;
public class Screen extends JPanel implements Runnable{
public static final int WIDTH = 800, HEIGHT = 800;
private Thread thread;
private boolean running = false;
private BodyParts b;
private ArrayList<BodyParts> snake;
private int ticks;
private int X = 10, Y = 10, size = 5;
private boolean right = false, left = true, up=false, down = false;
public Screen()
{
setPreferredSize(new Dimension(WIDTH, HEIGHT));
snake = new ArrayList<BodyParts>();
Start();
}
public void Start()
{
running = true;
thread = new Thread(this, "game loop");
thread.start();
}
public void tick()
{
if(snake.size() == 0)
{
b = new BodyParts(X, Y, 10);
snake.add(b);
}
ticks++;
if(ticks > 2500)
{
//check the direction of movement
if(right) {X++;}
if(left) {X--;}
if(up) {Y++;}
if(down) {Y--;}
ticks = 0;
b = new BodyParts(X, Y, 10);
snake.add(b);
if(snake.size() > size)
{
snake.remove(0);
}
}
}
public void paint(Graphics g)
{
//g.clearRect(0, 0, WIDTH, HEIGHT);
g.setColor(Color.BLACK);
for(int i = 0;i<WIDTH/10 ;i++)
{
g.drawLine(i*10, 0, i*10, HEIGHT);
}
for(int i = 0;i<HEIGHT/10 ;i++)
{
g.drawLine( 0, i*10, WIDTH, i*10);
}
for(int i = 0; i < snake.size() ;i++)
{
snake.get(i).draw(g);
}
}
public void Stop()
{
}
@Override
public void run() {
// TODO Auto-generated method stub
while(running)
{
tick();
repaint();
}
}
}
// BodyParts.java
package entities;
import java.awt.Color;
import java.awt.Graphics;
public class BodyParts {
private int X, Y, width, height;
public BodyParts(int X, int Y, int tile)
{
this.X = X;
this.Y = Y;
width = tile;
height = tile;
}
public void tick()
{
}
public void draw(Graphics g)
{
g.setColor(Color.black);
g.fillRect(X*width, Y*height, width, height);
g.setColor(Color.GREEN);
g.fillRect(X*width + 2, Y*height + 2, width-4, height-4);
}
}
使用下面的代码为贪吃蛇游戏构建漂亮的 UI..
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Snake Game</title>
<style>
body {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background: linear-gradient(135deg, #71b7e6, #9b59b6);
color: white;
font-family: 'Roboto', sans-serif;
}
#gameContainer {
display: flex;
flex-direction: column;
align-items: center;
background: rgba(0, 0, 0, 0.7);
padding: 20px;
border-radius: 15px;
box-shadow: 0 0 15px rgba(0, 0, 0, 0.5);
}
canvas {
background-color: #222;
border: 2px solid #555;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
}
#score {
font-size: 24px;
margin: 20px;
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.7);
}
</style>
</head>
<body>
<div id="gameContainer">
<canvas id="gameCanvas"></canvas>
<div id="score">Score: 0</div>
</div>
<script src="snake.js"></script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Snake Game</title>
<style>
body {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background: linear-gradient(135deg, #71b7e6, #9b59b6);
color: white;
font-family: 'Roboto', sans-serif;
}
#gameContainer {
display: flex;
flex-direction: column;
align-items: center;
background: rgba(0, 0, 0, 0.7);
padding: 20px;
border-radius: 15px;
box-shadow: 0 0 15px rgba(0, 0, 0, 0.5);
}
canvas {
background-color: #222;
border: 2px solid #555;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
}
#score {
font-size: 24px;
margin: 20px;
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.7);
}
</style>
</head>
<body>
<div id="gameContainer">
<canvas id="gameCanvas"></canvas>
<div id="score">Score: 0</div>
</div>
<script src="snake.js"></script>
</body>
</html>