Java Snake游戏。(Java Snake game. Why is this image being drawn in completely the wrong location)

我正在使用Java制作一个简单的Snake游戏,我想在游戏板上使用图像而不是g.drawOval作为樱桃。 樱桃的位置是每次游戏加载时随机生成的,或樱桃被吃掉。

如果我使用g.fillOval方法,樱桃每次都会被绘制在正确的位置,但如果我使用图像,每次都会在不正确的位置绘制樱桃。

这是一个绘制樱桃的图像与完全相同的坐标(我用一个点代表樱桃)但你可以看到位置的明显差异。

樱桃位置的随机化按预期工作,这是绘制图像的问题

这是我的代码的结果:

红点是g.fillOval,是樱桃的正确位置樱桃图形是我的g.drawImage放樱桃的地方。

上图的X / Y为Cherry X/Y: 49 : 27

这是呈现游戏的代码:

import java.awt.Color; import java.awt.Graphics; import java.awt.Point; import javax.swing.JPanel; public class Render extends JPanel { private static final Color BG = new Color(0); private static final Color SNAKEBODY = new Color(3380564); private static final Color SNAKEHEAD = new Color(16724736); @Override protected void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(BG); g.fillRect(0, 0, 800, 700); Snake snake = Snake.snake; g.setColor(SNAKEBODY); for (Point point : snake.snakeParts) { g.fillOval(point.x * Snake.SCALE, point.y * Snake.SCALE, Snake.SCALE, Snake.SCALE); } g.setColor(SNAKEHEAD); g.fillOval(snake.head.x * Snake.SCALE, snake.head.y * Snake.SCALE, Snake.SCALE, Snake.SCALE); g.setColor(Color.RED); g.drawImage(snake.cherryImg, snake.cherry.x, snake.cherry.y, snake.cherryImg.getWidth(null), snake.cherryImg.getHeight(null), null); g.fillOval(snake.cherry.x * 10, snake.cherry.y * 10,10, 10); //debug line System.out.println("Cherry X/Y: " + snake.cherry.x + " : " + snake.cherry.y); String string = "Score: " + snake.score + ", Length: " + snake.snakeSize; g.setColor(Color.white); //simple math to center ish text. g.drawString(string, (int) (getWidth() / 2 - string.length() * 2.5f), 10); string = "Game Over!"; if (snake.over){ g.drawString(string, (int) (getWidth() / 2 - string.length() * 2.5f), (int) snake.dim.getHeight() / 4); } string = "Paused!"; { if (snake.paused && !snake.over){ g.drawString(string, (int) (getWidth() / 2 - string.length() * 2.5f), (int) snake.dim.getHeight() / 4); } } } }

安迪身体可以看出为什么这些线上的图画有差异:

g.drawImage(snake.cherryImg, snake.cherry.x, snake.cherry.y,snake.cherryImg.getWidth(null), snake.cherryImg.getHeight(null), null); g.fillOval(snake.cherry.x * 10, snake.cherry.y * 10, 10, 10); //debug line System.out.println("Cherry X/Y: " + snake.cherry.x + " : " + snake.cherry.y);

I am making a simple Snake game in Java, and i want to use an image rather than a g.drawOval for a cherry on my game board. The cherry's location in randomly generated each time the game loads, or the cherry is eaten.

If i use the g.fillOval method, the cherry gets drawn in the correct location every time, but if i use an image the cherry is being drawn in the incorrect location every time.

Here is an image of the drawn cherry with the EXACT same coordinates (i am using a Point to represent the cherry) but you can see the clear differences in location.

The randomisation of the cherry's location is working as intended, it is a problem with drawing the image

Here is the outcome of my code:

The red dot is the g.fillOval and is the Correct location for the cherry The Cherry graphic is where my g.drawImage puts the cherry.

The X/Y for the above image is Cherry X/Y: 49 : 27

This is the code which renders the game:

import java.awt.Color; import java.awt.Graphics; import java.awt.Point; import javax.swing.JPanel; public class Render extends JPanel { private static final Color BG = new Color(0); private static final Color SNAKEBODY = new Color(3380564); private static final Color SNAKEHEAD = new Color(16724736); @Override protected void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(BG); g.fillRect(0, 0, 800, 700); Snake snake = Snake.snake; g.setColor(SNAKEBODY); for (Point point : snake.snakeParts) { g.fillOval(point.x * Snake.SCALE, point.y * Snake.SCALE, Snake.SCALE, Snake.SCALE); } g.setColor(SNAKEHEAD); g.fillOval(snake.head.x * Snake.SCALE, snake.head.y * Snake.SCALE, Snake.SCALE, Snake.SCALE); g.setColor(Color.RED); g.drawImage(snake.cherryImg, snake.cherry.x, snake.cherry.y, snake.cherryImg.getWidth(null), snake.cherryImg.getHeight(null), null); g.fillOval(snake.cherry.x * 10, snake.cherry.y * 10,10, 10); //debug line System.out.println("Cherry X/Y: " + snake.cherry.x + " : " + snake.cherry.y); String string = "Score: " + snake.score + ", Length: " + snake.snakeSize; g.setColor(Color.white); //simple math to center ish text. g.drawString(string, (int) (getWidth() / 2 - string.length() * 2.5f), 10); string = "Game Over!"; if (snake.over){ g.drawString(string, (int) (getWidth() / 2 - string.length() * 2.5f), (int) snake.dim.getHeight() / 4); } string = "Paused!"; { if (snake.paused && !snake.over){ g.drawString(string, (int) (getWidth() / 2 - string.length() * 2.5f), (int) snake.dim.getHeight() / 4); } } } }

Can andy body see why there is differences in the drawing on these lines:

g.drawImage(snake.cherryImg, snake.cherry.x, snake.cherry.y,snake.cherryImg.getWidth(null), snake.cherryImg.getHeight(null), null); g.fillOval(snake.cherry.x * 10, snake.cherry.y * 10, 10, 10); //debug line System.out.println("Cherry X/Y: " + snake.cherry.x + " : " + snake.cherry.y);

最满意答案

看看你的两个职位。

你的填充椭圆形发生在snake.cherry.x*10而图像是在snake.cherry.x上绘制的(同样为Y)

这就是为什么图像是49,27,点是490,270

Look at your two positions.

Your fill oval is happening at snake.cherry.x*10 while the image is drawn at snake.cherry.x (SAME FOR Y)

Thats why the image is at 49,27 and the dot is at 490,270

更多推荐