我想重新粉刷我的屏幕。到目前为止,它所做的只是在第一个屏幕上显示一个点,即头部应该在的位置。这很好,但是我在代码中写道,我希望每秒将头部向下移动 10 像素。我正在打印头部应该在的位置,并且在命令提示符中显示 y 值确实在增加。然而在我的屏幕上,头部没有移动。
我尝试过使用 revalidate 方法,尝试扩展 canvas 类而不是 jframe,我尝试过仅针对 Paint 方法使用不同的类,我尝试过用 PaintComponent 方法替换 Paint 方法。正如你可能知道的那样,我对与 Java 绘画相关的任何事情的理解都不太好。我尝试过阅读这些超类,但它们太复杂了,我无法理解。我也尝试过在没有睡眠声明的情况下运行。这并不重要。
主要类别:
该类包含开始运行贪吃蛇游戏的主要方法。
import java.util.concurrent.TimeUnit;
public class Main{
public static void main(String[] args) throws InterruptedException {
Main programma = new Main();
programma.rungame();
}
void rungame() throws InterruptedException {
AllGUIElements gui = new AllGUIElements();
gui.gui();
while (true) {
TimeUnit.SECONDS.sleep(1);
gui.setGameScreen();
}
}
}
AllGUIElements 类:此类创建一个新框架,其中包含一个新面板。该面板正在使用paintComponent 进行绘制。 setGameScreen 更新头部的位置并应该重新绘制屏幕。
import javax.swing.*;
import java.awt.*;
public class AllGUIElements extends JPanel {
private JFrame frame;
private JPanel panel;
private int screen;
Snake hoofd = new Head(new Point(30,30),3,null);
void gui() throws InterruptedException {
frame = new JFrame("Snake Game");
panel = new AllGUIElements();
panel.setBackground(Color.GRAY);
panel.setSize(1000,500);
frame.setSize(1000,500);
frame.add(panel);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
void setGameScreen() {
repaint();
if (hoofd.getDirection() == 1) {
hoofd.setPosition(new Point(hoofd.getPosition().x, hoofd.getPosition().y-10));
}
if (hoofd.getDirection() == 2) {
hoofd.setPosition(new Point(hoofd.getPosition().x+10, hoofd.getPosition().y));
}
if (hoofd.getDirection() == 3) {
hoofd.setPosition(new Point(hoofd.getPosition().x, hoofd.getPosition().y+10));
}
if (hoofd.getDirection() == 4) {
hoofd.setPosition(new Point(hoofd.getPosition().x-10, hoofd.getPosition().y));
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.DARK_GRAY);
g.fillRect(0, 0, 1000, 10);
g.fillRect(0, 0, 10, 500);
g.fillRect(990, 0, 10, 500);
g.fillRect(0, 490, 1000, 10);
g.setColor(Color.GREEN);
g.fillRect(hoofd.getPosition().x, hoofd.getPosition().y, 10, 10);
}
}
Screen Object 类:Snake 和 Food 的父类(因为没有必要,所以没有发布),返回头部、身体部位和食物的位置。
import java.awt.*;
public class Screenobject{
Point pos;
Screenobject(Point pos){
this.pos = pos;
}
void setPosition(Point pos){
this.pos = pos;
}
Point getPosition() {
return pos;
}
}
Snake 类:Screenobject 的子级,Head 和 Body 的父级。此类设置对象的方向。
import java.awt.*;
public class Snake extends Screenobject{
int direction;
//directions:
//1 = up
//2 = right
//3 = down
//4 = left
Snake nextpart;
Snake(Point pos, int direction, Snake nextpart){
super(pos);
this.direction = direction;
this.nextpart = nextpart;
}
int getDirection() {
return direction;
}
}
Head类:snake的子类,应该是snake链表的第一个对象。
import java.awt.*;
public class Head extends Snake{
Head(Point pos, int direction, Snake nextpart){
super(pos, direction, nextpart);
}
}
身体类别:蛇的子级,每吃一次食物就会生长。
import java.awt.*;
public class Body extends Snake{
Body(Point pos, int direction, Snake nextpart){
super(pos, direction, nextpart);
}
}