gpt4 book ai didi

java - 在一行中绘制多个矩形

转载 作者:行者123 更新时间:2023-12-02 11:04:54 25 4
gpt4 key购买 nike

我正在尝试制作一个游戏,如果球击中一个矩形,该矩形的值就会下降,一旦达到 0,它就会消失。我还没有实现计数变量,但遇到了问题,我试图渲染顶部的数组列表,但当我尝试绘制多个矩形并将它们留在那里时,我的循环仅渲染一个对象球可以与之发生 react 。我该怎么做?

package graphics;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

public class Ballz extends LASSPanel
{
//Variables for the circle (Global Variables)

int circleX, circleY, circleSize;
double ballDirection,circleDX, circleDY;


//Colors
Color circleColor;
Color backgroundColor;
Dimension size;

int turn = 1;
int click =0;
int go =0;
int move = 0;
boolean roundEnd;



//Variables for the map
int brickWH;
int[] rect = new int[12];



public Ballz()
{
//Ball Variables
size = new Dimension(0,0);


circleX = 190;
circleY = 545;
circleDX = 0;
circleDY=0;

circleSize = 15;

circleColor = new Color (245,245,245);
backgroundColor = new Color (28,28,28);

//Game Variables
roundEnd = true;

brickWH = 18;
}

public void update()
{
//Get size of screen
getSize(size);




circleX += circleDX;
circleY += circleDY;



//Screen borders
if (circleX >= (size.width) - circleSize)
{
circleDX = -circleDX;
circleX = size.width-circleSize;

}
if (circleX <= 0)
{
circleDX = -circleDX;
circleX = 0;
}
if (circleY >= (size.height -circleSize))
{
circleDX =0;
circleDY = 0;
circleX = 190;
circleY = 545;
roundEnd = true;
}
if (circleY <= 0)
{
circleDY = -circleDY;
circleY = 0;
}


circleX += circleDX;
circleY += circleDY;


click = getMouseButton(0);

if (click ==1)
{
circleDY = -5;
circleDX = -3;
}


//Rectangle Loop


for (int i =0; i<rect.length; i++)
{
rect[i] = rect[i] +50;
}





repaint();

}

public void paint(Graphics g)
{


//Game Colors
g.setColor(circleColor);
g.fillOval(circleX, circleY, circleSize, circleSize);


for (int i=0; i<rect.length; i++)
{
g.fillRect(rect[i], 2, brickWH, brickWH);

}

setBackground(backgroundColor);







}

}

最佳答案

好的,看过更多代码后,让我们开始......

int[] rect = new int[12];

这会创建一个 int 数组,最初初始化为所有 0

接下来,你...

for (int i =0; i<rect.length; i++)
{
rect[i] = rect[i] +50;
}

所有这一切基本上都是将 50 添加到 0 并将其分配回数组元素,所以当您这样做时...

for (int i=0; i<rect.length; i++)
{
g.fillRect(rect[i], 2, brickWH, brickWH);

}

它只是将每个“砖 block ”绘制在最后一个“砖 block ”之上,因为它们都处于相同的水平位置

对我来说真正非常非常突出的是......

for (int i =0; i<rect.length; i++)
{
rect[i] = rect[i] +50;
}

您的意图是让每 block 砖 block 相距 50 像素吗?在这种情况下,你可能应该做一些更像......

int xPos = 0;
for (int i =0; i<rect.length; i++)
{
rect[i] = xPos;
xPos += 50;
}

如果相反,它们应该放在一起,那么您会想要更像......的东西

for (int i =0; i<rect.length; i++)
{
rect[i] = brickWH * i;
}

关于java - 在一行中绘制多个矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51052749/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com