我正在尝试在没有 youtube 视频帮助的情况下制作我的第一款游戏。我看过很多关于制作 java 游戏的 youtube 系列,但它们从来都不是我想做的,所以在学得足够多之后,我开始制作自己的游戏。我知道足够多的 Java 来解决问题,到目前为止一切都没有问题。
我曾尝试寻求一些帮助,但我只能找到人们说尝试以下内容的地方:
setFocusable(true);
requestFocus();
然而,这对我不起作用。我知道大多数人不想只看我所有的代码,但我认为它是易于管理的(它还没有那么多代码)。
我计划让它成为一种类似蛇的游戏,你变得更大而不是更长,你是一只豚鼠(现在用橙色矩形表示)而不是蛇。
游戏.java:
package com.kaperly.game;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Game extends JFrame {
private static final long serialVersionUID = 1L;
public static JFrame frame;
public static JPanel panel;
public static int width = 1000;
public static int height = 600;
public Game() {
frame = new JFrame();
panel = new Panel();
frame.setPreferredSize(new Dimension(width, height));
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Guinea Pig Eat");
frame.setLayout(new BorderLayout());
frame.setVisible(true);
panel.setFocusable(true);
panel.requestFocus(true);
frame.add(panel, BorderLayout.CENTER);
frame.pack();
}
public static void init() {
}
public static void loop() throws InterruptedException { while(true) {
Character.move();
Thread.sleep(10);
}}
public static void main(String[] args) throws InterruptedException, IOException {
Game game = new Game();
init();
loop();
}
}
面板.java:
package com.kaperly.game;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JPanel;
public class Panel extends JPanel implements KeyListener {
private static final long serialVersionUID = 1L;
public static Image startButton;
public static boolean goingRight = false;
public static boolean goingLeft = false;
public static boolean goingUp = false;
public static boolean goingDown = false;
private static int changeDirection = 0;
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_W) {
goingUp = true;
changeDirection = 1;
System.out.println("Moved Up!");
}else if(e.getKeyCode() == KeyEvent.VK_A) {
goingLeft = true;
changeDirection = 2;
}else if(e.getKeyCode() == KeyEvent.VK_S) {
goingDown = true;
changeDirection = 3;
}else if(e.getKeyCode() == KeyEvent.VK_D) {
goingRight = true;
changeDirection = 4;
}else if(changeDirection == (1 | 2 | 3 | 4)) {
changeDirection = 0;
if(changeDirection == 1) {
goingLeft = false;
goingDown = false;
goingRight = false;
}else if(changeDirection == 2) {
goingUp = false;
goingDown = false;
goingRight = false;
}else if(changeDirection == 3) {
goingUp = false;
goingLeft = false;
goingRight = false;
}else if(changeDirection == 4) {
goingUp = false;
goingLeft = false;
goingDown = false;
}
}
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
//Set background color
g.setColor(Color.GRAY);
g.fillRect(0, 0, Game.width, Game.height);
//Make Walls
g.setColor(Color.BLACK);
g.fillRect(0, 0, Game.width, 25);
g.fillRect(0, 550, Game.width, 25);
g.fillRect(0, 0, 25, Game.height);
g.fillRect(970, 0, 25, Game.height);
//Show Character
g.setColor(Color.ORANGE);
g.fillRect(Character.x, Character.y, Character.width, Character.height);
repaint();
}
}
字符.java:
package com.kaperly.game;
public class Character {
public static int x = 475;
public static int y = 250;
public static int width = 50;
public static int height = 75;
public static void move() {
if(Panel.goingUp == true) {
y = y ++;
}
if(Panel.goingLeft == true) {
x = x --;
}
if(Panel.goingDown == true) {
y = y --;
}
if(Panel.goingRight == true) {
x = x ++;
}
}
}