gpt4 book ai didi

java - 为什么 "ball"在与数组的某些部分相交时不改变颜色? (java)

转载 作者:太空宇宙 更新时间:2023-11-04 14:11:28 26 4
gpt4 key购买 nike

此代码适用于一款名为“brick-breaker”的游戏。我试图让球在与数组相交时改变颜色。我为自定义颜色的 R、G、B 值创建了一个 int,并在与 1-255 之间的数字相交时生成这些 int

import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;
import java.util.Random;
public class brickbreaker extends Applet implements MouseListener, Runnable, KeyListener
{
Thread main=new Thread(this);
int mouseX, mouseY;
int paddleX = 200, x,y,height,width, ballX = 100, ballY = 400, xChange = 1, yChange = 4, paddleWidth = 150, pageCt = 1, score = 0;
boolean pause = false, gameover=false;
boolean blocks[][] = new boolean [6][10];
int randomR = 255, randomG = 205, randomB = 155;
Random red = new Random();
Random green = new Random();
Random blue = new Random();
Image buffer;
Graphics bufferG;
String input="";
Font font3 = new Font ("Impact",5,50);
Font font4 = new Font ("Impact",5,150);
Font font5 = new Font ("Impact",5,30);
Color randomColor = new Color(randomR,randomG,randomB);
public void init()
{
resize( 1300,700 );
buffer = createImage(this.getWidth(),this.getHeight());
bufferG = buffer.getGraphics();

this.setLayout (null);

this.addKeyListener(this);

this.addMouseListener(this);

this.resize(1400,700);
main.start();



for(int r=0;r<6;r++)
for(int c=0;c<10;c++)
blocks[r][c] = true;
}

public void drawBlocks(Graphics g)
{
for(int r=0;r<6;r++)
for(int c=0;c<10;c++)
if(blocks[r][c])

g.drawRect(100+100*c, 30+30*r, 100, 30);
}

public void run()
{
if (pageCt==1)
{
while(! gameover)
{
if(pause==false)
{
repaint();
ballX = ballX + xChange;
ballY = ballY + yChange;
Rectangle ballRect = new Rectangle (ballX, ballY, 15, 15);
Rectangle paddleRect = new Rectangle (paddleX,650,paddleWidth,10);
Rectangle boundRect = new Rectangle (0,690,1300,10);
for(int r=0;r<6;r++)
for(int c=0;c<10;c++)
if(blocks[r][c])
{
Rectangle blockRect = new Rectangle (100+100*c, 30+30*r, 100, 30);
if(blockRect.intersects(ballRect))
{
blocks[r][c] = false;
yChange *=-1;
score++;
randomR=red.nextInt(255);
randomB=blue.nextInt(255);
randomG=green.nextInt(255);
if (score==60)
pageCt=4;
}
}

if(ballX < 5 || ballX > 1280)
xChange = -1*xChange;
if(ballY < 5 || ballY > 690)
yChange = -1*yChange;
if (ballRect.intersects (paddleRect))
{
yChange = yChange*-1;
//paddleWidth=paddleWidth-3;
}
if (ballRect.intersects (boundRect))
{
pageCt=2;
}
}
repaint();
try
{main.sleep(20);}
catch(Exception e) {}
}
}
}

public void keyPressed(KeyEvent e) // makes applet do something when clicked.
{
int code = e.getKeyCode();
if(code == e.VK_P)
{
if(pause==false)
pause=true;
else
pause=false;
}
if (pause==false)
{
if(paddleX>20)
{
if(code == e.VK_A)
{
paddleX = paddleX-20;
}
}
if(paddleX<1160)
{
if(code == e.VK_D)
{
paddleX = paddleX+20;
}
}
}
}

public void paint(Graphics g) // Outputs on screen
{
if (pageCt==1)
{
bufferG.setColor(Color.black);
bufferG.fillRect(0,0,1300,700);
bufferG.setColor(Color.blue);
bufferG.setColor(Color.black);
bufferG.fillRect(0,0,this.getWidth(),this.getHeight());
bufferG.setColor(randomColor);
bufferG.fillOval(ballX,ballY,15,15);
bufferG.setColor(Color.white);
bufferG.fillRect(paddleX,650,paddleWidth,10);
drawBlocks(bufferG);
bufferG.setFont(font5);
bufferG.drawString("Score : "+score,10,20);
}
if (pageCt==2)
{
bufferG.setColor(Color.black);
bufferG.fillRect(0,0,1300,700);
bufferG.setColor(Color.white);
bufferG.setFont(font4);
bufferG.drawString("You Lose!", 350,400);
}
if (pause==true)
{
bufferG.setColor(Color.white);
bufferG.setFont(font4);
bufferG.drawString("PAUSED",400,350);
}
if (pageCt==0)
{
bufferG.drawString("- Click to begin -",350,350);
}
g.drawImage(buffer,0,0,this);
}

public void update(Graphics g)
{
paint(g);
}

public void mouseClicked (MouseEvent e) {}

public void keyReleased( KeyEvent e){}

public void keyTyped (KeyEvent e){}

public void mousePressed (MouseEvent e)
{
if(pageCt==0)
{
pageCt++;
repaint();
}
}

public void mouseReleased (MouseEvent e) {}

public void mouseEntered (MouseEvent e){}

public void mouseExited (MouseEvent e) {}

}

最佳答案

颜色不会改变,因为您没有使用随机生成的值更新它。

randomR=red.nextInt(255);
randomB=blue.nextInt(255);
randomG=green.nextInt(255);
randomColor = new Color(randomR,randomG,randomB); // <- add this line

关于java - 为什么 "ball"在与数组的某些部分相交时不改变颜色? (java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28272935/

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