- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在为一个项目创建 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,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,0,0,0,1,0},
{0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
{0,1,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,1,0},
{0,1,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,1,0},
{0,1,1,1,1,1,1,0,0,1,1,1,1,0,0,1,1,1,1,0,0,1,1,1,1,1,1,0},
{0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0},
{0,0,0,0,0,0,1,0,0,1,1,1,1,1,1,1,1,1,1,0,0,1,0,0,0,0,0,0},
{0,0,0,0,0,0,1,0,0,1,0,0,0,2,2,0,0,0,1,0,0,1,0,0,0,0,0,0},
{0,0,0,0,0,0,1,1,1,1,0,0,2,2,2,2,0,0,1,1,1,1,0,0,0,0,0,0},
{0,0,0,0,0,0,1,0,0,1,0,0,2,2,2,2,0,0,1,0,0,1,0,0,0,0,0,0},
{0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0},
{0,0,0,0,0,0,1,0,0,1,1,1,1,1,1,1,1,1,1,0,0,1,0,0,0,0,0,0},
{0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,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,0,0,0,1,0},
{0,1,1,1,0,0,1,1,1,1,1,1,1,5,1,1,1,1,1,1,1,1,0,0,1,1,1,0},
{0,0,0,1,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,1,0,0,0},
{0,0,0,1,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,1,0,0,0},
{0,1,1,1,1,1,1,0,0,1,1,1,1,0,0,1,1,1,1,0,0,1,1,1,1,1,1,0},
{0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0},
{0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0},
{0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,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}
};
}
最佳答案
除了有一些愚蠢的错误,比如 ||
而不是 &&
.如果不查看更多应用程序,就很难确定此类问题的问题所在。不过我会给出一些提示:
System.out.println("collision!")
或调试器,无论您最喜欢哪种方式。
Input
类检测输入但更改 Game
中的变量类(class)。 OOP 的一个好处是您可以将应用程序的特定区域分解为特定的类,而您不想要的是 god object。这就是您现在拥有的 Game
类(class)。它变得势不可挡且难以管理。继续这样做,很快你就会有一个无法管理的程序。 pTileX
, pTileY
, playerPosX
, playerPosY
, tileX
, tileY
.为什么是 6 个变量?您应该只需要 2,即播放器的 x 和 y 位置。可以使用这些坐标随时找到图 block 位置。 import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import javax.swing.JFrame;
public class Game extends Canvas implements Runnable {
public static int playerHeight = 16;
public static int playerWidth = 16;
public static int movementSpeed = 3;
public boolean running = false;
private JFrame frame;
private Image bufferImage;
private Graphics bufferGraphics;
private Input input;
private Point playerLocation = null;
private GameMap currentLevel;
public Game() {
Resources.loadResources();
loadLevel(new GameMap.Level1());
frame = new JFrame("PacMan"); // 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
}
/**
* Loads the level into the game, also changes the dimensions of the window to fit the game
* @param gameMap
*/
private void loadLevel(GameMap gameMap) {
currentLevel = gameMap;
playerLocation = convertMapLocationToScreenLocation(currentLevel.getPlayerStartLocation());
Dimension canvasSize = new Dimension(currentLevel.getWidth()*Resources.TILE_WIDTH, currentLevel.getHeight()*Resources.TILE_HEIGHT);
setMinimumSize(canvasSize);
setMaximumSize(canvasSize);
setPreferredSize(canvasSize);
}
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(20); // keep the Frames from going to high
} catch (InterruptedException e) {
e.printStackTrace();
}
if (render == true) {
frames++;
render();
}
if (System.currentTimeMillis() - lastTimer >= 1000) {
lastTimer += 1000;
frames = 0;
ticks = 0;
}
}
try {
Thread.sleep(20); // keep the Frames from going to high
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public synchronized void start() {
new Thread(this).start();
running = true;
}
public synchronized void stop() {
running = false;
}
public void render() {
Graphics g = getGraphics();
if (bufferImage == null) {
bufferImage = createImage(this.getSize().width, this.getSize().height);
bufferGraphics = bufferImage.getGraphics();
}
bufferGraphics.setColor(Color.orange);
bufferGraphics.fillRect(0, 0, this.getSize().width, this.getSize().height);
drawGame(bufferGraphics);
g.drawImage(bufferImage, 0, 0, this);
}
// this had to be renamed to drawGame, the paint method is used by AWT objects. This caused a serious bug where you would be constantly repainting.
public void drawGame(Graphics g) {
for (int x = 0; x < currentLevel.getWidth(); x++) {
for (int y = 0; y < currentLevel.getHeight(); y++) {
int tile = currentLevel.getTileAt(x, y);
int tileX = x * Resources.TILE_WIDTH;
int tileY = y * Resources.TILE_HEIGHT;
if (tile == GameMap.TILE_WALL) {
g.drawImage(Resources.WALL, tileX, tileY, Resources.TILE_WIDTH, Resources.TILE_HEIGHT, null);
}
if (tile == GameMap.TILE_NOTHING) {
g.drawImage(Resources.BLACK_BACK, tileX, tileY,
Resources.TILE_WIDTH, Resources.TILE_HEIGHT, null);
}
if (tile == GameMap.TILE_POINT) {
g.drawImage(Resources.POINT, tileX, tileY,
Resources.TILE_WIDTH, Resources.TILE_HEIGHT, null);
}
/* This is not a good way to find the first location for the player, knowing that location belongs to the Map class
if (tile == 5) {
if (!loaded) {
playerPosX = tileX;
playerPosY = tileY;
loaded = true;
}
g.drawImage(blackBack, tileX, tileY,
(int) 21.42857142857143, 26, null);
}*/
}
}
g.drawImage(Resources.PACMAN, playerLocation.x, playerLocation.y, playerWidth, playerHeight, null);
}
public void tick() {
int nextPlayerPosX = playerLocation.x;
int nextPlayerPosY = playerLocation.y;
if (input.key_up) {
nextPlayerPosY -= movementSpeed;
}
if (input.key_down) {
nextPlayerPosY += movementSpeed;
}
if (input.key_left) {
nextPlayerPosX -= movementSpeed;
}
if (input.key_right) {
nextPlayerPosX += movementSpeed;
}
// lets make sure the next location doesnt collide with a wall, if so then dont move the pacman!
if(!doesPlayerCollideWith(nextPlayerPosX, nextPlayerPosY, GameMap.TILE_WALL)) {
playerLocation.setLocation(nextPlayerPosX, nextPlayerPosY);
}
}
/**
* Looks at the players screen location and gets the map tiles for each corner.
* @param screenX
* @param screenY
* @return the 4 map tiles for each corner of the pac man given the screenX and screenY
*/
private int[] getPlayerCornerCollisions(int screenX, int screenY) {
int[] corners = new int[4];
Point tileLocation = convertScreenLocationToMapLocation(screenX, screenY);
corners[0] = currentLevel.getTileAt(tileLocation.x, tileLocation.y);
tileLocation = convertScreenLocationToMapLocation(screenX + playerWidth, screenY);
corners[1] = currentLevel.getTileAt(tileLocation.x, tileLocation.y);
tileLocation = convertScreenLocationToMapLocation(screenX, screenY + playerHeight);
corners[2] = currentLevel.getTileAt(tileLocation.x, tileLocation.y);
tileLocation = convertScreenLocationToMapLocation(screenX + playerWidth, screenY + playerHeight);
corners[3] = currentLevel.getTileAt(tileLocation.x, tileLocation.y);
return corners;
}
/**
* Checks if any corners of the player intersects with the given mapTileType
* @param screenX
* @param screenY
* @param mapTileType
* @return true if the player intersects with the given map tile type
*/
public boolean doesPlayerCollideWith(int screenX, int screenY, int mapTileType) {
for(int tileType : getPlayerCornerCollisions(screenX, screenY)) {
if(tileType == mapTileType) {
return true;
}
}
return false;
}
/**
* Takes the screen location and converts it to a coordinate in the map
* @param location
* @return
*/
public Point convertScreenLocationToMapLocation(Point location) {
return convertScreenLocationToMapLocation(location.x, location.y);
}
public Point convertScreenLocationToMapLocation(int x, int y) {
return new Point(x/Resources.TILE_WIDTH, y/Resources.TILE_HEIGHT);
}
public Point convertMapLocationToScreenLocation(Point location) {
return convertMapLocationToScreenLocation(location.x, location.y);
}
public Point convertMapLocationToScreenLocation(int x, int y) {
return new Point(x*Resources.TILE_WIDTH, y*Resources.TILE_HEIGHT);
}
public static void main(String[] args) {
new Game().start();
}
}
import java.awt.Point;
public abstract class GameMap {
public static final int TILE_WALL = 0;
public static final int TILE_NOTHING = 1;
public static final int TILE_POINT = 2;
public static final int TILE_START_LOCATION = 5;
public abstract int getWidth();
public abstract int getHeight();
public abstract int[][] getLevelData();
public abstract java.awt.Point getPlayerStartLocation();
public int getTileAt(int x, int y) {
return getLevelData()[y][x];
}
public static class Level1 extends GameMap {
@Override
public int getWidth() {
return LEVEL_1_DATA[0].length;
}
@Override
public int getHeight() {
return LEVEL_1_DATA.length;
}
@Override
public int[][] getLevelData() {
return LEVEL_1_DATA;
}
@Override
public Point getPlayerStartLocation() {
for(int y=0;y<LEVEL_1_DATA.length;y++) {
for(int x=0;x<LEVEL_1_DATA[y].length;x++) {
if(LEVEL_1_DATA[y][x] == GameMap.TILE_START_LOCATION) {
return new Point(x, y);
}
}
}
// should never reach this unless we forgot to put a start location.
throw new RuntimeException("No player start location could be found!");
}
}
private static int[][] LEVEL_1_DATA = {
{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,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,0,0,0,1,0},
{0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
{0,1,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,1,0},
{0,1,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,1,0},
{0,1,1,1,1,1,1,0,0,1,1,1,1,0,0,1,1,1,1,0,0,1,1,1,1,1,1,0},
{0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0},
{0,0,0,0,0,0,1,0,0,1,1,1,1,1,1,1,1,1,1,0,0,1,0,0,0,0,0,0},
{0,0,0,0,0,0,1,0,0,1,0,0,0,2,2,0,0,0,1,0,0,1,0,0,0,0,0,0},
{0,0,0,0,0,0,1,1,1,1,0,0,2,2,2,2,0,0,1,1,1,1,0,0,0,0,0,0},
{0,0,0,0,0,0,1,0,0,1,0,0,2,2,2,2,0,0,1,0,0,1,0,0,0,0,0,0},
{0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0},
{0,0,0,0,0,0,1,0,0,1,1,1,1,1,1,1,1,1,1,0,0,1,0,0,0,0,0,0},
{0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,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,0,0,0,1,0},
{0,1,1,1,0,0,1,1,1,1,1,1,1,5,1,1,1,1,1,1,1,1,0,0,1,1,1,0},
{0,0,0,1,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,1,0,0,0},
{0,0,0,1,0,0,1,0,0,1,0,0,0,0,0,0,0,0,1,0,0,1,0,0,1,0,0,0},
{0,1,1,1,1,1,1,0,0,1,1,1,1,0,0,1,1,1,1,0,0,1,1,1,1,1,1,0},
{0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0},
{0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0},
{0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,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}
};
}
import java.awt.event.*;
public class Input implements KeyListener
{
public boolean key_down = false;
public boolean key_up = false;
public boolean key_right = false;
public boolean key_left = false;
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) {
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
key_right = true;
key_left = false;
key_up = false;
key_down = false;
}
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
key_right = false;
key_left = true;
key_up = false;
key_down = false;
}
if (e.getKeyCode() == KeyEvent.VK_UP) {
key_right = false;
key_left = false;
key_up = true;
key_down = false;
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
key_right = false;
key_left = false;
key_up = false;
key_down = true;
}
}
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
key_right = false;
}
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
key_left = false;
}
if (e.getKeyCode() == KeyEvent.VK_UP) {
key_up = false;
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
key_down = false;
}
}
}
import java.awt.Color;
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;
public final class Resources {
private final static String NL = System.getProperty("line.separator");
public static final int TILE_WIDTH = 23;// 578;
public static final int TILE_HEIGHT = 29;// 720;
public static BufferedImage BACKGROUND = new BufferedImage(TILE_WIDTH, TILE_HEIGHT,
BufferedImage.TYPE_INT_RGB);
public static BufferedImage PACMAN = new BufferedImage(TILE_WIDTH, TILE_HEIGHT,
BufferedImage.TYPE_INT_RGB);
public static BufferedImage SETTINGS_BACKGROUND = new BufferedImage(TILE_WIDTH, TILE_HEIGHT,
BufferedImage.TYPE_INT_RGB);
public static BufferedImage LEVEL1 = new BufferedImage(TILE_WIDTH, TILE_HEIGHT,
BufferedImage.TYPE_INT_RGB);
/*public static BufferedImage LEVEL2 = new BufferedImage(TILE_WIDTH, TILE_HEIGHT,
BufferedImage.TYPE_INT_RGB);*/
public static BufferedImage POINTS = new BufferedImage(TILE_WIDTH, TILE_HEIGHT,
BufferedImage.TYPE_INT_RGB);
public static BufferedImage POINT = new BufferedImage(TILE_WIDTH, TILE_HEIGHT,
BufferedImage.TYPE_INT_RGB);
public static BufferedImage WALL = new BufferedImage(TILE_WIDTH, TILE_HEIGHT,
BufferedImage.TYPE_INT_RGB);
public static BufferedImage BLACK_BACK = new BufferedImage(TILE_WIDTH, TILE_HEIGHT,
BufferedImage.TYPE_INT_RGB);
public static BufferedImage POINT_BACK = new BufferedImage(TILE_WIDTH, TILE_HEIGHT,
BufferedImage.TYPE_INT_RGB);
public static void loadResources() {
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"));
SETTINGS_BACKGROUND = 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"));
BLACK_BACK = ImageIO.read(new File("res\\blackBack.png"));
POINT_BACK = 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);
}
}
}
关于Java - 二维数组 map 中的移动和墙壁碰撞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26538154/
我正在尝试从一个 map 的 map 的 map 的 map 的 map 的 map 的 map 的 map 的 map 的 map 的 map 的 map 的 map 的 map 的 map 的 m
我是 Haskell 的新手,我认为函数 map map和 map.map在 Haskell 中是一样的。 我的终端给了我两种不同的类型, (map.map) :: (a -> b) -> [[a]
我的目标是创建一个 map 的 map ,这样我就可以通过它的键检索外部 map 的信息,然后通过它们的键访问它的“内部” map 。 但是,当我得到每个内部映射时,我最初创建的映射变成了一个对象,我
如何使用 Java8 编写以下代码? for (Entry> entry : data.entrySet()) { Map value = entry.getValue(); if (valu
我有覆盖整个南非的图片。它们为Tiff格式,并已将坐标嵌入其中。我正在尝试拍摄这些图像(大约20张图像),并将它们用作我的iPhone应用程序中的地图叠加层。我的问题在于(准确地)将地图切成图块。 我
所以我有 2 std::map s >一个是“旧的”,一个是“新的”,我想知道哪些文件被删除了,这样就能够遍历差异并对 shared_ptr 做一些事情。这样的事情可能吗?如何做到? 最佳答案 虽然
是否可以将当前查看的 google.maps.Map 转换为静态图像链接,以便我可以获取图像并将其嵌入到 PDF 中? 我在 map 上添加了一些带有自定义图标的标记,所以我不确定这是否真的可行。 如
你能帮我吗 Java Streams ? 从标题可以看出我需要合并List>>进入Map> . 列表表示为List>>看起来像: [ { "USER_1":{
对于 idAndTags 的第二个条目,内部映射被打乱,但第一个条目则不然 第一次接近! for (Map.Entry> entryOne : idAndTags.entrySet()) {
我将从我的代码开始,因为它应该更容易理解我想要做什么: @function get-color($color, $lightness) { @return map-get(map-get($col
我过去曾在许多网站上使用过 Google map ,但遇到了以前从未遇到过的问题。 map 窗口正在显示,但它只显示左上角的 map 片段,以及之后的任何内容(即使我在周围导航时),右侧也不会加载任何
众所周知,这些 map ,无论是常规街道 map 还是卫星 map ,在中国的特定地区都无法正确排列。那么哪个 map 排列正确,是卫星 map 还是默认街道 map ?一些网站表明卫星 map 是正
在拖尾事件之后,我面临着获取此处 map 中的 map 边界的问题。我需要新的经纬度来在新更改的视口(viewport)中获取一些项目/点。我只是想在拖动结束时获得谷歌地图map.getBounds(
我想做的是通过 ajax API 显示以英国邮政编码为中心的小型 bing 生成 map 。我相信这是可能的;我在 Bing map 文档中找不到如何将英国邮政编码转换为可以插入 map Ajax 控
我有一个 List我想转换成的 e Map>其中外部字符串应为“Name”,内部字符串应为“Domain”。 Name Id Domain e(0) - Emp1, 1, Insuran
我的第 2 部分:https://stackoverflow.com/questions/21780627/c-map-of-maps-typedef-doubts-queries 然后我继续创建 I
是否可以在 1 行中使用 Java8 编写以下所有 null 和空字符串检查? Map> data = new HashMap<>(holdings.rowMap()); Set>> entrySet
我正在审查一个项目的旧代码,并使用 Map 的 Map 的 Map 获得了如下数据结构(3 层 map ): // data structure Map>>> tagTree
这可能是一种不好的做法,但我还没有找到更好的解决方案来解决我的问题。所以我有这张 map // Map>> private Map>> properties; 我想初始化它,这样我就不会得到 Null
我们在 JDK 1.7 中使用 HashMap,我在使用 SonarQube 进行代码审查时遇到了一些问题。 请考虑以下示例: public class SerializationTest imple
我是一名优秀的程序员,十分优秀!