我正在为一个项目创建 pac man,但在我的角色移动时遇到了麻烦。特别是实现墙壁碰撞,我可以很好地移动角色,只是当我尝试进行墙壁碰撞时......显然我做错了什么
当我试图四处移动时,它会检测到箭头键输入但不会移动角色,因此我认为我的 if 语句不正确,所以我尝试将其设置为在上升时处理墙壁碰撞。这导致了看似看不见的墙壁或地方,我只是无法移动我已经看了一段时间,但还没有找到解决方案。
当前代码
package main;
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
public class game extends Canvas implements Runnable {
final static String NL = System.getProperty("line.separator");
public static int tile;
public boolean running = false;
public String name = "PacMan";
public static final int WIDTH = 23;//578;
public static final int HEIGHT = 29;//720;
public static final int SCALE = 25;
private JFrame frame;
private Image bImage;
private Graphics db;
private input input;
public static int playerPosX = 0;
public static int playerPosY = 0;
public static int playerHeight = 16;
public static int playerWidth = 16;
public static int speed = 3;
static BufferedImage background = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);;
static BufferedImage pacman = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);;
static BufferedImage settingsBackground = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);;
static BufferedImage level1 = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);;
static BufferedImage level2 = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);;
static BufferedImage points = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);;
static BufferedImage point = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);;
static BufferedImage wall = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);;
static BufferedImage blackBack = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);;
static BufferedImage pointBack = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);;
public static boolean key_down = false;
public static boolean key_up = false;
public static boolean key_right = false;
public static boolean key_left = false;
public boolean wallDrawn = false;
public boolean loaded = false;
public static int tc = 0;
public int tileX, tileY, pTileX, pTileY;
public game() {
setMinimumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
setMaximumSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE)); // keeps
// the
// canvas
// same
// size
setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE));
frame = new JFrame(name); // creates the frame for our game
input = new input();
//if(MAIN_MENU == true && GAME == false){
// buttons = new buttons(frame.getContentPane()); //draws the buttons based on the code in our graphics/buttons.java
//}
frame.setLayout(new BorderLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // ends program on click of the Exit button in the top right corner
frame.setLocationByPlatform(true);
frame.addKeyListener(new input() );
frame.add(this, BorderLayout.CENTER);
frame.pack(); // keeps size correct
frame.setResizable(false); //keep this false, if set to true whenever someone resizes it our code will not function correctly
frame.setVisible(true);
this.addKeyListener(input);
this.createBufferStrategy(2); //I think this is double buffering, honestly not too sure
}
public static void main(String[] args) {
new game().start();
}
public void resLoader() {
try {
background = ImageIO.read(new File("res\\Background.png"));
wall = ImageIO.read(new File("res\\maptile.png"));
pacman = ImageIO.read(new File("res\\pacman.png"));
settingsBackground = ImageIO.read(new File("res\\Background.png"));
level1 = ImageIO.read(new File("res\\level1.png"));
point = ImageIO.read(new File("res\\Points for pacman.png"));
blackBack = ImageIO.read(new File("res\\blackBack.png"));
pointBack = ImageIO.read(new File("res\\points.png"));
} catch (IOException e) {
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~ WARNING! ~~~~~~~~~~~~~~~~~~~~~~~~~~");
System.out.println("There was am Image unsuccessfully loaded!" + NL + "The game may not work properly, check the load images method for spelling errors!");
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~ WARNING! ~~~~~~~~~~~~~~~~~~~~~~~~~~" + NL + NL);
}
}
public void run()
{
long lastTime = System.nanoTime();
double nsPerTick = 1000000000 / 60D;
long lastTimer = System.currentTimeMillis();
double delta = 0;
int frames = 0;
int ticks = 0;
while (running == true) {
long now = System.nanoTime();
delta += (now - lastTime) / nsPerTick;
lastTime = now;
boolean render = false;
while (delta >= 1) {
ticks++;
tick();
delta -= 1;
render = true;
}
try {
Thread.sleep(3); //keep the Frames from going to high
} catch (InterruptedException e) {
e.printStackTrace();
}
if(render == true){
frames++;
render();
}
if (System.currentTimeMillis() - lastTimer >= 1000) {
lastTimer +=1000;
//System.out.println("Frames: " + frames + " Ticks: " + ticks);
frames = 0;
ticks = 0;
}
}
try {
Thread.sleep(3); //keep the Frames from going to high
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public synchronized void start() {
resLoader();
new Thread(this).start();
running = true;
}
public synchronized void stop() {
running = false;
}
public void render () {
Graphics g = getGraphics();
if (bImage == null) {
bImage = createImage (this.getSize().width, this.getSize().height);
db = bImage.getGraphics ();
}
db.setColor (getBackground ());
db.fillRect (0, 0, this.getSize().width, this.getSize().height);
paint (db);
g.drawImage (bImage, 0, 0, this);
}
public void paint (Graphics g) {
for (int x = 0; x < main.map.width; x++)
{
for (int y = 0; y < main.map.height; y++)
{
tile = main.map.level1[y][x];
tileX = x * 21;
tileY = y * 26;
if(tile == 0){
g.drawImage(wall,tileX,tileY, 21, 26,null);
}
if(tile == 1){
g.drawImage(pointBack,tileX, tileY, (int)21.42857142857143, 26,null);
}
if(tile == 2){
g.drawImage(blackBack,tileX, tileY, (int)21.42857142857143, 26,null);
}
if(tile == 5) {
if(!loaded) {
pTileX = tileX;
pTileY = tileY;
loaded = true;
}
g.drawImage(blackBack,tileX, tileY, (int)21.42857142857143, 26,null);
}
}
}
g.drawImage(pacman, pTileX, pTileY, playerWidth, playerHeight, null);
}
public void tick() {
playerPosX = pTileX / 21;
playerPosY = pTileY / 26;
int wallPosX, wallPosY;
if(key_up && map.level1[playerPosX][playerPosY] == 1){
pTileY = pTileY - speed;
}
if(key_down && map.level1[playerPosX][playerPosY] == 1){
pTileY += speed;
}
if(key_left && map.level1[playerPosX][playerPosY] == 1){
pTileX -= speed;
}
if(key_right && map.level1[playerPosX][playerPosY] == 1){
pTileX += speed;
}
}
}
package main;
import java.awt.event.*;
public class input implements KeyListener
{
public void keyTyped(KeyEvent e) {
}
/** Here we have a rather basic key listener
* It is set that you can only go left right up or down right now
* If more directions are needed delete the "other key = false"
*/
public void keyPressed(KeyEvent e) {
System.out.println(KeyEvent.getKeyText(e.getKeyCode()));
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
game.key_right = true;
game.key_left = false;
game.key_up = false;
game.key_down = false;
}
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
game.key_right = false;
game.key_left = true;
game.key_up = false;
game.key_down = false;
}
if (e.getKeyCode() == KeyEvent.VK_UP) {
game.key_right = false;
game.key_left = false;
game.key_up = true;
game.key_down = false;
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
game.key_right = false;
game.key_left = false;
game.key_up = false;
game.key_down = true;
}
}
public void keyReleased(KeyEvent e) {
System.out.println(e.getKeyCode());
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
game.key_right = false;
}
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
game.key_left = false;
}
if (e.getKeyCode() == KeyEvent.VK_UP) {
game.key_up = false;
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
game.key_down = false;
}
}
}
public class map {
public static int width = 28;
public static int height = 28;
public static int[][] level1 = {
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,0},
{0,1,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,1,0},
{0,1,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,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