gpt4 book ai didi

java - 我的程序似乎效率低下

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:06:34 26 4
gpt4 key购买 nike

我是一名初学者,我有一个程序,它是一种从一点到另一点的游戏。你是一个绿色方 block ,你避开红色方 block 。但是我不知道如何在不创建另一个方法 enemyMovement2() 的情况下创建另一个移动的红色 block ,这看起来效率很低,因为如果我想创建许多这样的敌人怎么办。有什么方法可以制作这种行为的 arraylist 吗?

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JPanel;

@SuppressWarnings("serial")
public class Game extends JPanel implements KeyListener, ActionListener {
private static boolean dead = false;
private static boolean won = false;

boolean isPaused=false;
boolean hasStarted=false;
int level = 1;

boolean powerUpIsEaten;
boolean keepPowerUpOffScreen;
int powerupx;
int powerupy;
int powerupwidth=20;
int powerupheight=20;

int winningx;
int winningy;
int winningwidth;
int winningheight;

int losingx;
int losingy;
int losingwidth;
int losingheight;

int losing2x;
int losing2y;
int losing2width;
int losing2height;

int losing3x;
int losing3y;
int losing3width;
int losing3height;

int losing4x;
int losing4y;
int losing4width;
int losing4height;

int losing5x;
int losing5y;
int losing5width;
int losing5height;


int enemywidth;
int enemyheight;

int userwidth;
int userheight;


ArrayList<Integer> badRectangles = new ArrayList<Integer>();
int x = 1, y = 1;

int enemyX=400, enemyY=100;

int velx=0, vely=0;

boolean hasWon = false, hasDied = false;

int movementcounter=0;

private void move() throws InterruptedException {
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);

if (vely > 0){
if(y>700-userheight *2){
}
else{
y = y + 5 + movementcounter/300;
}
}
if (vely < 0){
if(y<0){
}
else{
y = y - 5 - movementcounter/300;
}
}
if (velx > 0){
if(x>1200-userwidth){
}
else{
x = x + 5 + movementcounter/300;
}
}
if (velx < 0){
if(x<0){
}
else{
x = x - 5 - movementcounter/300;
}
}
if(isInARectangle()){
hasDied=true;
}
if(isInWinningArea()){
x=0;
y=0;
hasWon=true;
}
movementcounter++;
}
@Override
public void paint(Graphics g) {
super.paint(g);
getValues(level);
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.setFont(new Font("TimesRoman", Font.PLAIN, 30));
if(level==5){
g2d.drawString("Congratulations", 550, 350);
this.won=true;

}
else if(hasStarted
&& !isPaused){
eatPowerup();
g2d.setColor(Color.RED);
for(int i=1; i<=level; i++){
g2d.fillRect(badRectangles.get(4 * i - 4),badRectangles.get(4 * i - 3),badRectangles.get(4 * i - 2),badRectangles.get(4 * i - 1));
}
g2d.setColor(Color.GREEN);
g2d.fillRect(winningx, winningy, winningwidth, winningheight); //WINNING RECTANGLE
g2d.setColor(Color.RED);
g2d.fillRect(enemyX, enemyY, enemywidth, enemyheight); //ENEMY
enemyMovement();

if(!hasDied){
g2d.setColor(Color.GREEN);
g2d.fillRect(x, y, userwidth, userheight); //USER
}
if(hasWon
&& !hasDied){
level++;
if(level==2){
enemyX=1000;
enemyY=0;
}
if(level==3){
enemyX=700;
enemyY=0;
}
if(level==4){
enemyX=400;
enemyY=100;
}
powerUpIsEaten=false;
hasWon=false;
keepPowerUpOffScreen=false;
}
if(hasDied){
g2d.setColor(Color.RED);
g2d.drawString("You died", 500, 300); //LOSING SEQUENCE
this.dead=hasDied;
}
if(!powerUpIsEaten
&& level>1
&& !keepPowerUpOffScreen){
g2d.setColor(Color.BLUE);
g2d.fillRect(powerupx, powerupy, powerupwidth, powerupheight);
}
else{
keepPowerUpOffScreen=true;
powerUpIsEaten=false;
}
}
if(isPaused){
g2d.drawString("PAUSED", 450, 300);
}
if(!hasStarted){
g2d.drawString("PRESS UP TO START", 450, 300);
}
}
public void eatPowerup(){
if(powerupx<x + userwidth + 5
&& powerupx>x
&& powerupy<y + userheight + 5
&& powerupy>y){
powerUpIsEaten=true;
powerupx=0;
powerupy=1000;
enemyX=0;
enemyY=0;
}
else{

powerUpIsEaten=false;
}
}
public boolean isInWinningArea(){
if(x<winningx + winningwidth - userwidth
&& x>winningx
&& y<winningy + winningheight-userheight
&& y>winningy){
return true;
}
else{
return false;
}
}
public boolean isInARectangle(){
if((x > losingx-userwidth
&& x < losingx + losingwidth
&& y > losingy-userheight
&& y < losingy + losingheight)
|| x > losing2x-userwidth
&& x < losing2x + losing2width
&& y > losing2y-userheight
&& y < losing2y + losing2height
|| x > losing3x-userwidth
&& x < losing3x + losing3width
&& y > losing3y-userheight
&& y < losing3y + losing3height
|| x > losing4x-userwidth
&& x < losing4x + losing4width
&& y > losing4y-userheight
&& y < losing4y + losing4height){
return true;
}
else{
return false;
}

}
public void getValues(int level){
if(level==1){

winningx=550;
winningy=550;
winningwidth=100;
winningheight=100;

losingx=200;
losingy=100;
losingwidth=200;
losingheight=200;

enemywidth=60;
enemyheight=60;

userwidth=50;
userheight=50;

}
if(level==2){

winningx=1000;
winningy=500;
winningwidth=200;
winningheight=200;

losingx=200;
losingy=100;
losingwidth=200;
losingheight=200;

losing2x=700;
losing2y=500;
losing2width=300;
losing2height=200;

enemywidth=60;
enemyheight=60;

userwidth=50;
userheight=50;

powerupx=900;
powerupy=100;

}
if(level==3){

winningx=1000;
winningy=500;
winningwidth=200;
winningheight=200;

losingx=200;
losingy=100;
losingwidth=200;
losingheight=200;

losing2x=700;
losing2y=500;
losing2width=300;
losing2height=200;

losing3x=100;
losing3y=100;
losing3width=100;
losing3height=450;

enemywidth=60;
enemyheight=60;

userwidth=50;
userheight=50;

powerupx=100;
powerupy=600;
}
if(level==4){

winningx=1000;
winningy=500;
winningwidth=200;
winningheight=200;

losingx=200;
losingy=100;
losingwidth=200;
losingheight=200;

losing2x=700;
losing2y=500;
losing2width=300;
losing2height=200;

losing3x=100;
losing3y=100;
losing3width=100;
losing3height=450;

losing4x=900;
losing4y=300;
losing4width=500;
losing4height=100;

enemywidth=60;
enemyheight=60;

userwidth=50;
userheight=50;

powerupx=600;
powerupy=500;
}
if (level==5){

}
badRectangles.clear();
badRectangles.add(losingx);
badRectangles.add(losingy);
badRectangles.add(losingwidth);
badRectangles.add(losingheight);

badRectangles.add(losing2x);
badRectangles.add(losing2y);
badRectangles.add(losing2width);
badRectangles.add(losing2height);

badRectangles.add(losing3x);
badRectangles.add(losing3y);
badRectangles.add(losing3width);
badRectangles.add(losing3height);

badRectangles.add(losing4x);
badRectangles.add(losing4y);
badRectangles.add(losing4width);
badRectangles.add(losing4height);

badRectangles.add(losing5x);
badRectangles.add(losing5y);
badRectangles.add(losing5width);
badRectangles.add(losing5height);
}
public void enemyMovement(){

if(enemyX<x){
enemyX= enemyX+1 + movementcounter/300;
}
if(enemyX>x){
enemyX=enemyX-1 - movementcounter/300;
}
if(enemyY<y){
enemyY=enemyY + 1 + movementcounter/300;
}
if(enemyY>y){
enemyY=enemyY -1 - movementcounter/300;
}
if(x<enemyX+enemywidth
&& x>enemyX-userwidth
&& y<enemyY+ enemyheight
&& y>enemyY-userheight){
hasDied=true;
}

}
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
switch( key ) {
case KeyEvent.VK_UP:
isPaused=false;
hasStarted=true;
vely = -1;
break;
case KeyEvent.VK_DOWN:
vely = 1;
break;
case KeyEvent.VK_LEFT:
velx = -1;
break;
case KeyEvent.VK_RIGHT :
velx = 1;
break;
case KeyEvent.VK_P:
isPaused=true;
}

}
@Override
public void keyReleased(KeyEvent e) {
velx = 0;
vely = 0;

}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub

}
public static void main(String[] args) throws InterruptedException {

JFrame frame = new JFrame("Square Game");
Game game = new Game();
frame.add(game);
frame.setSize(1200, 700);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setBackground( Color.BLUE );

while (!dead
&& !won) {

game.move();
game.repaint();
Thread.sleep(10);
}
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub

}
}

最佳答案

那是的代码!但这里是您未能意识到的快速一瞥。

一些游戏对象是可移动

所以,制作一个Movable interface,每个可移动的GO实现。然后您只需遍历 MovablemovableGameObject.move() 它们。

例子:

public interface Movable {
void move();
}

class Player extends GameObject implements Movable {

@Override
public void move() {
System.out.println("Player moving...");
}
}

class Enemy extends GameObject implements Movable {

@Override
public void move() {
System.out.println("Enemy moving...")
}
}

public static void main(String... args) {
Arrays.asList(
new Movable[] {
new Player(), new Enemy()
}
).forEach(Movable::move);
}

正如预期的那样,这给出了输出...

Player moving...
Enemy moving...

关于java - 我的程序似乎效率低下,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35967154/

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