Java 使用eclipse编程Pong
我目前正在尝试教自己用Java编写代码,我正在使用eclipse,我有一个创建pong的教程,但其中缺少了一些方法。我唯一有困难的是完成球类课程。我已经让它渲染并正确显示在窗口中,但实际上没有做任何事情,它只是保持静止。这是因为我不知道我需要什么样的代码,所有的谷歌搜索结果都是对不起作用的代码感到沮丧 到目前为止,这是我在棒球课上所学的全部内容Java 使用eclipse编程Pong,java,eclipse,pong,Java,Eclipse,Pong,我目前正在尝试教自己用Java编写代码,我正在使用eclipse,我有一个创建pong的教程,但其中缺少了一些方法。我唯一有困难的是完成球类课程。我已经让它渲染并正确显示在窗口中,但实际上没有做任何事情,它只是保持静止。这是因为我不知道我需要什么样的代码,所有的谷歌搜索结果都是对不起作用的代码感到沮丧 到目前为止,这是我在棒球课上所学的全部内容 import java.awt.Color; import java.awt.Graphics; public class Ball extends
import java.awt.Color;
import java.awt.Graphics;
public class Ball extends Entity {
public Ball(int x, int y, Color color) {
super(x, y, color);
this.width = 15;
this.height = 15;
int yspeed = 1;
int xspeed = 2;
}
public void render( Graphics g ) {
super.render( g );
}
public void update() {
if (y <= 0) {
y = 0;
}
if (y >= window.HEIGHT - height - 32 ) {
y = window.HEIGHT - height -32;
}
}
导入java.awt.Color;
导入java.awt.Graphics;
公共类Ball扩展实体{
公共球(整数x,整数y,彩色){
超级(x,y,颜色);
这个宽度=15;
这个高度=15;
int y速度=1;
int xspeed=2;
}
公共空间渲染(图形g){
超级渲染(g);
}
公共无效更新(){
如果(y=window.HEIGHT-HEIGHT-32){
y=窗高-高度-32;
}
}
如果您有任何建议,我们将不胜感激。您的球类课程到目前为止看起来不错,您所写的(或从教程中复制的)内容。您缺少将“生命”放在其中的代码,例如,使球移动的代码。您有一些字段,如
xspeed
和yspeed
,但没有代码实际将增量从一个时间单位应用到另一个时间单位。(我希望)定期调用update()
方法应将这两个值应用于x
和y
字段:
public void update() {
x += xspeed;
y += yspeed;
if (y <= 0) {
y = 0;
}
if (y >= window.HEIGHT - height - 32 ) {
y = window.HEIGHT - height -32;
}
}
public void update(){
x+=x速度;
y+=y速度;
如果(y=window.HEIGHT-HEIGHT-32){
y=窗高-高度-32;
}
}
这就是更新部分所需的内容。
下一件大事是实现当球撞到墙或桨时发生的逻辑。对于这样的事件,您需要操纵xspeed
和yspeed
变量。您的ball类看起来很好,到目前为止,您所编写的(或从教程中复制的)内容。您缺少将“生命”放在其中的代码,例如,使球移动的代码。您有一些字段,如xspeed
和yspeed
,但没有代码实际将增量从一个时间单位应用到另一个时间单位。(我希望)定期调用update()
方法应将这两个值应用于x
和y
字段:
public void update() {
x += xspeed;
y += yspeed;
if (y <= 0) {
y = 0;
}
if (y >= window.HEIGHT - height - 32 ) {
y = window.HEIGHT - height -32;
}
}
public void update(){
x+=x速度;
y+=y速度;
如果(y=window.HEIGHT-HEIGHT-32){
y=窗高-高度-32;
}
}
这就是更新部分所需的内容。
下一件大事是实现当球击中墙壁或桨叶时发生的逻辑。对于此类事件,您需要操纵xspeed
和yspeed
变量。显而易见的步骤是:
public void update() {
x += xspeed;
y += yspeed;
if (y <= 0) {
y = 0;
}
if (y >= window.HEIGHT - height - 32 ) {
y = window.HEIGHT - height -32;
}
// TODO: Code to change the direction of the ball (i.e. xspeed and yspeed)
// when it hits a wall or paddle.
}
public void update(){
x+=x速度;
y+=y速度;
如果(y=window.HEIGHT-HEIGHT-32){
y=窗高-高度-32;
}
//TODO:更改球的方向(即xspeed和yspeed)的代码
//当它碰到墙或桨时。
}
在调用render()
之前,不要忘记调用update()
,显而易见的步骤是:
public void update() {
x += xspeed;
y += yspeed;
if (y <= 0) {
y = 0;
}
if (y >= window.HEIGHT - height - 32 ) {
y = window.HEIGHT - height -32;
}
// TODO: Code to change the direction of the ball (i.e. xspeed and yspeed)
// when it hits a wall or paddle.
}
public void update(){
x+=x速度;
y+=y速度;
如果(y=window.HEIGHT-HEIGHT-32){
y=窗高-高度-32;
}
//TODO:更改球的方向(即xspeed和yspeed)的代码
//当它碰到墙或桨时。
}
在调用render()
之前,千万不要忘记调用update()
在更新函数中需要一些代码来更改球的位置(例如基于其速度),在更新函数中需要一些代码来更改球的位置(例如基于其速度)