gpt4 book ai didi

Java Agario 单机

转载 作者:行者123 更新时间:2023-11-30 08:46:13 27 4
gpt4 key购买 nike

如何制作 agario 的 java 单人游戏版本?我想使用面向对象编程。所以应该有 Dot、Blob 和一个 Controller Object。(我只是想分享我的代码,所以不要投反对票:)

最佳答案

我是这样做的。有随机颜色的点,从 20 大小开始的 Blob 。线程每几毫秒检查一次冲突。

点:

package agario;

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.util.Random;

public class Dot extends Component{
public int x;
public int y;
public Color c;
public int size;
Dot(int x, int y){
this.x = x;
this.y = y;
Random rand = new Random();
int r = rand.nextInt(255);
int green = rand.nextInt(255);
int b = rand.nextInt(255);
this.c = new Color(r,green,b);
this.size = rand.nextInt(50);
}
public void paint(Graphics g){
g.setColor(c);
g.fillOval(this.x, this.y, this.size, this.size);
}
}

Blob :

package agario;

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;

public class Blob extends Component{
public int x;
public int y;
public int size;
public Color color;
public static Blob blob = new Blob(800,100,40,Color.blue);
Blob(int x, int y, int size, Color c){
this.x = x;
this.y = y;
this.size = size;
this.color = c;
}
public void paint(Graphics g){

g.setColor(color);
g.fillOval(x, y, size, size);
}
}

Controller :

package agario;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Label;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;

public class Controller {
ArrayList<Blob> blobs = new ArrayList<Blob>();
public ArrayList<Dot> dots = new ArrayList<Dot>();
Blob b = new Blob(50,50,10,Color.CYAN);
MyFrame mf = new MyFrame("Agario");
Label l = new Label("10");
int mouseX = 0;
int mouseY = 0;
static int score = 30;
static int xDis = 0;
static int yDis = 0;
public static void main(String[] args){
new Controller().startGame();
}
public void startGame(){
mf.addMouseMotionListener(new MyMouseMoveListener());
mf.add(l,BorderLayout.NORTH);
Refresh rf = new Refresh();
Thread t = new Thread(rf);
t.start();

while(true){
try{
Random r = new Random();
Thread.sleep(r.nextInt(40));

double dis = Math.sqrt(xDis*xDis + yDis*yDis);
double easingAmount = 20;
b.x += xDis / easingAmount;
b.y += yDis / easingAmount;


if(r.nextInt(10) == 5){
int randX = r.nextInt(600);
int randY = r.nextInt(600);
Dot d = new Dot(randX,randY);
synchronized(dots){
dots.add(d);
}
mf.add(d);
mf.repaint();
}
}catch(Exception e){

}
}
}
class Refresh implements Runnable{

public void run() {
while(true){
Random ran = new Random();
try{
Thread.sleep(ran.nextInt(20));
}catch(Exception e){
System.out.println("error");
}
Rectangle r = new Rectangle(b.x,b.y,b.size,b.size);
synchronized(dots){
Iterator i = dots.iterator();
while(i.hasNext()){
Dot d = (Dot) i.next();
Rectangle r1 = new Rectangle(d.x,d.y,d.size,d.size);
if(r1.intersects(r)){
i.remove();
b.size += 1;
l.setText(String.valueOf(Integer.parseInt(l.getText())+1));
score += 1;
}
}
}
mf.repaint();
}

}
}
class MyMouseMoveListener extends MouseMotionAdapter{
public void mouseMoved(MouseEvent m){
mouseX = m.getX();
mouseY = m.getY();
xDis = mouseX-b.x;
yDis = mouseY-b.y;
}
}
class MyFrame extends Frame{
MyFrame(String s){
super(s);
setBounds(0,0,900,900);
add(b);
blobs.add(b);
setVisible(true);
}
public void paint(Graphics g){
for(Blob b : blobs)
b.paint(g);
synchronized(dots){
Iterator i = dots.iterator();
while(i.hasNext()){
Dot d = (Dot) i.next();
d.paint(g);
}
}
}
}
}

关于Java Agario 单机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32937857/

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