- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在用java制作乒乓球,但敌人的AI没有动。它应该向球移动的方向移动。请问有人可以帮助我吗?
主类:
import javax.swing.*;
public class Main extends JFrame{
final static int WW = 800;
final static int WH = 600;
public Main(){
setSize(WW,WH);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setTitle("Pong");
add(new GamePanel());
setVisible(true);
}
public static void main(String[] args){
new Main();
}
}
GamePanel 类:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class GamePanel extends JPanel implements ActionListener,KeyListener{
Player player = new Player();
Computer computer = new Computer();
Ball ball = new Ball();
public GamePanel(){
Timer time = new Timer(20, this);
time.start();
this.addKeyListener(this);
this.setFocusable(true);
}
private void update(){
player.update();
ball.update();
ball.Collision(player);
computer.update();
ball.Collision(computer);
}
public void paintComponent(Graphics g){
g.setColor(Color.BLACK);
g.fillRect(0, 0, Main.WW, Main.WH);
player.paint(g);
ball.paint(g);
computer.paint(g);
}
public void actionPerformed(ActionEvent e){
update();
repaint();
}
public void keyPressed(KeyEvent e){
if(e.getKeyCode() == KeyEvent.VK_UP){
player.setyv(-10);
}
else if(e.getKeyCode() == KeyEvent.VK_DOWN){
player.setyv(10);
}
}
public void keyReleased(KeyEvent e){
if(e.getKeyCode()==KeyEvent.VK_UP || e.getKeyCode()==KeyEvent.VK_DOWN)
player.setyv(0);
}
public void keyTyped(KeyEvent e){
}
}
玩家等级:
import java.awt.*;
public class Player {
private int y = 300;
private int w = 20;
private int h = 120;
private int yv = 0;
private int x = 700;
public Player(){
}
public void update(){
y = y + yv;
}
public void paint(Graphics g){
g.setColor(Color.WHITE);
g.fillRect(x,y,w,h);
}
public void setyv(int speed){
yv = speed;
}
public int getX(){
return this.x;
}
public int getY(){
return this.y;
}
public int getW(){
return this.w;
}
public int getH(){
return this.h;
}
}
球类:
import java.awt.*;
public class Ball {
private int x = 400;
private int y = 300;
private int xv = -10;
private int yv = 10;
public void update(){
x = x += xv;
y = y += yv;
if(x < 10){
xv = 10;
}
if(x > 770){
xv = -10;
}
if(y < 10){
yv = 5;
}
if(y > 550){
yv = -5;
}
}
public int getY(){
return this.y;
}
public void paint(Graphics g){
g.setColor(Color.WHITE);
g.fillOval(x, y, 20, 20);
}
public void Collision(Player player){
if(this.x > (player.getX()-10)){
if(this.y > player.getY() && (this.y < player.getY() + player.getH())){
xv = -10;
}
}
}
public void Collision(Computer cpu){
if(this.x < (cpu.getX()+10)){
if(this.y > cpu.getY() && (this.y < cpu.getY() + cpu.getH())){
xv = 10;
}
}
}
}
计算机类:
import java.awt.*;
public class Computer {
private int y = 300;
private int w = 20;
private int h = 120;
private int yv = 0;
private int x = 100;
Ball ballpos = new Ball();
public Computer(){
}
public void update(){
if(ballpos.getY() < this.y){
yv = -1;
}
else if(ballpos.getY() > this.y){
yv = 1;
}
y = y + yv;
}
public void paint(Graphics g){
g.setColor(Color.WHITE);
g.fillRect(x,y,w,h);
}
public int getX(){
return this.x;
}
public int getY(){
return this.y;
}
public int getW(){
return this.w;
}
public int getH(){
return this.h;
}
}
如果代码太多,我很抱歉,但如果只发送AI类,我猜你看不到问题出在哪里。请帮助我,我不知道为什么它不起作用。抱歉我的英语不好。
最佳答案
你的计算机类创建了一个不同的球(ballpos),它永远不会更新,因此人工智能永远不会移动。您需要将 Ball 对象从 GamePanel 类传递到 Computers 更新方法:
计算机:
public void update(Ball ball){
if(ball.getY() < this.y){
yv = -1;
}
else if(ball.getY() > this.y){
yv = 1;
}
y = y + yv;
}
游戏面板:
private void update(){
player.update();
ball.update();
ball.Collision(player);
computer.update(ball);
ball.Collision(computer);
}
关于Java-Pong AI 不动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32190976/
更新:下面提到的stackoverflow给出了另一种解决方案,即导入json并用正确格式的数据替换文本。我现在试图看看如何适应给定的格式,它看起来像这样: { "text" : "use
有谁知道三者之间的具体区别和功能,或者作为开发人员是否有更多功能/更灵活地使用? 最佳答案 wit.ai 与 Dialogflow 与 luis.ai ╔═══════════════════════
我知道 Wit.ai 引擎可以通过手动验证案例来训练,但是有没有办法用一组定义的输入和输出来训练它? 最佳答案 您可能可以查看其中一个应用程序的导出格式并对其进行调整以导入新应用程序。 https:/
来自 wit.ai 团队的一些人请回答这个 我们计划将 wit.ai 用于商业目的。有使用政策吗?请提供一些注意事项指南。此外,这项服务将来是否也是免费的,还是您计划推出企业版? 主要是 API 命中
我需要添加一个确认实体,以便在对话框流 (api.ai) 中的某个操作的参数中得到"is"或“取消”。假设用户正在购买咖啡,我会询问有关咖啡和数量的详细信息,最后我需要确认,我应该向哪个实体申请?任何
谁能帮我解决上面的问题。我们必须在数组 (a1,a2),(a1,a3),(a1,a4).... 等中找到元素的组合,然后选择满足条件 (ai*aj) <= max 的组合(A) 其中 A 是数组并返回
我正在尝试训练我的 Wit.ai 机器人以识别某人的名字。我不太确定我是否完全理解 NLP 的工作原理,所以我会给你一个例子。 我定义了很多表达,比如“我的名字是XXXX”、“大家都叫我XXXX” 在
我想知道是否存在一个网站,人们可以上传他们的 AI 在不同的棋盘游戏中相互竞争:国际象棋、五子棋等。 该站点将接受程序的源代码(以某种通用语言编写)、对其进行编译并相互运行程序。所有程序都必须使用一些
大家好,我是程序员幽鬼。 你想为后代开发一个令人难以置信的应用程序,你想到的第一件事——人工智能!还有什么比模仿人类智能的机器更令人着迷的呢?如果你期待打破刻板印象并准备推出出色的 AI 应用
我在 Application Insights Analytics 中创建了几个查询来获取我想要的图表。 示例: customEvents | where timestamp >= ago(31d)
我似乎无法让这个请求生效: https://wit.ai/docs/http/20160526#delete--entities-:entity-id-values-link 我已经设置了一个值为“C
我正在尝试在 wit.ai 中编写示例应用程序。我使用在 https://wit.ai/docs/quickstart 中显示的 node.js 客户端跟随快速启动应用程序。 .那里显示的示例只有一个
在 api.i(dialogflow) 中,我想获取用户的输入,如姓名、年龄、邮件等,并使用 PHP 将它们存储在我的 MYSQL 数据库中。 如何在对话流中生成我的代理的公共(public) API
我目前正在与 Wit.ai 合作 webpage in CodePen.io .我想知道是否可以使用 HTTP API 检索 Wit.ai 机器人的文本响应(“Bot says”)。 例如:如果用户要
我接到了一项任务,要编写一个由人类玩家和 AI 玩家组成的 NIM 游戏。游戏是“Misere”(最后一个必须拿起一根棍子的人输了)。 AI 应该使用 Minimax 算法,但它正在采取使其输得更快的
为了通过 shell 从端点取消部署模型,我必须指定 deployed-model-id如 gcloud ai endpoints undeploy-model 中所述 我如何获得这个已部署的模型 I
我在谷歌的 Vertex AI 中运行自定义训练作业。执行自定义作业的简单 gcloud 命令将使用类似于以下语法的内容(可以查看命令的完整文档 here ): gcloud beta ai cust
Wit AI project 在他们的 Converse 功能中添加了一个名为 Story 的新概念。有没有办法通过 HTTP API 管理(创建/编辑/验证)这些 Wit AI 故事? 最佳答案 W
我想为 Google 智能助理部署一个应用。但是,我想使用不同的 AI 后端而不是 api.ai。 有人知道这是否可能吗?如何? 或者如果我想使用 Google 智能助理,我会被 api.ai 困住吗
我的项目有一个依赖项,需要 python v3.6+。因此,它会在通过 pip 在 python 3 内核中安装时抛出错误,因为 AI Platform Notebooks 默认附带 v3.5。如何使
我是一名优秀的程序员,十分优秀!