Java程序解决的问题,希望对你有帮助,玩砖块游戏不能用键盘控制。你不能开始或移动。它是否与应用程序一起运行?
导入Java . awt . color;
导入Java . awt . dimension;
导入Java . awt . event;
导入Java . awt . font;
导入Java . awt . font metrics;
导入Java . awt . frame;
导入Java . awt . graphics;
导入Java . awt . image;
导入Java . awt . event . key event;
导入Java . awt . event . key listener;
导入Java . awt . event . window adapter;
导入Java . awt . event . window event;
公共类ballApplet扩展Applet实现Runnable {
维度d;
字体bFont =新字体(“Helvetica”,字体。粗体,24);
字体sFont =新字体(“Helvetica”,字体。加粗,14);
FontMetrics fmSmall,fmBig
图形goff
图像img
线程mThread
布尔ingame = false
int ballx,bally,batpos
int batd pos = 0;
int balldx = 0,balldy = 0,dxval
int iScroe,ballNum
boolean[]show brick;
int brickNumOfLine
final int bWidth = 5;
final int batW = 20
final int ballsize = 5;
最终int batH = 5;
最终int scoreH = 25
final int brickW = 15;
final int brickH = 8;
final int space = 1;
final int back col = 0x 102040;
最终int line = 4;
final int startline = 32
public void init() {
图形g;
d = size();
setBackground(新颜色(back col));
brickNumOfLine =(d . width-2 * b width)/(brickW+space);
d . width = brickNumOfLine *(brickW+space)+(2 * bWidth);
g = get graphics();
g . set font(sFont);
FM small = g . getfont metrics();
g . set font(bFont);
FM big = g . getfont metrics();
show brick = new boolean[brickNumOfLine * line];
this . addkey listener(new ballgamekey listener());
game init();
}
public void GameInit() {
batpos =(d . width-batW)/2;
ballx =(d . width-ballsize)/2;
bally =(d . height-ballsize-scoreH-2 * b width);
is croe = 0;
ball num = 3;
dxval = 2;
if(math . random()& lt;0.5)
balldx = dxval
其他
balldx =-dxval;
balldy =-dxval;
batd pos = 0;
init bricks();
}
public void InitBricks() {
int I;
for(I = 0;我& ltline * brickNumOfLinei++)
show brick[I]= true;
}
//public boolean keyDown(Event e,int key) {
//if(ingame){
//if(key = = Event。左)
//batd pos =-4;
//if(key = = Event。右)
//batd pos = 4;
//if(key = = Event。逃)
//ingame = false;
//}其他{
//if(key = = ' S ' | | key = = ' S '){
//ingame = true;
//game init();
// }
// }
//返回true
// }
//
//public boolean keyUp(Event e,int key) {
//if(key = = Event。LEFT || key ==事件。右)
//batd pos = 0;
//返回true
// }
公共空白油漆(图形g) {
if(Goff = = null & amp;& ampd.width & gt0 & amp& ampd.height & gt0) {
img = createImage(d.width,d . height);
Goff = img . get graphics();
}
if (goff == null || img == null)
返回;
goff.setColor(新颜色(back col));
goff.fillRect(0,0,d.width,d . height);
如果(ingame)
play game();
其他
ShowIntroScreen();
g.drawImage(img,0,0,this);
}
public void PlayGame() {
move ball();
check bat();
check bricks();
draw playfield();
draw brick();
show score();
}
public void ShowIntroScreen() {
move ball();
check bat();
check bricks();
batdummymmove();
draw playfield();
draw brick();
show score();
Goff . set font(bFont);
goff.setColor(新颜色(96,128,255));
String s = "弹球游戏";
goff.drawString(s,(d . width-FM big . string width(s))/2,(d.height
-scoreH-bWidth)/2-20);
Goff . set font(sFont);
goff.setColor(新颜色(128,255,32));
S = "请按' s '键开始游戏";
goff.drawString(s,(d . width-FM small . string width(s))/2,(d.height
-scoreH-bWidth)/2+30);
goff.setColor(新颜色(255,160,64));
S = "用方向键控制球拍的移动";
goff.drawString(s,(d . width-FM small . string width(s))/2,(d.height
-scoreH-bWidth)/2+50);
}
公共空画砖(){
int i,j;
boolean nobricks = true
int color delta = 255/(line-1);
for(j = 0;j & lt线;j++) {
for(I = 0;我& ltbrickNumOfLinei++) {
if(show brick[j * brickNumOfLine+I]){
nobricks = false
goff.setColor(新颜色(255,j * colorDelta,255 - j
* color delta));
Goff . fill rect(bWidth+I *(brickW+space),startline + j
* (brickH + space),brickW,brickH);
}
}
}
如果(nobricks) {
init bricks();
如果(ingame)
is croe+= 100;
}
}
public void DrawPlayField() {
Goff . set color(color . white);
goff.fillRect(0,0,d.width,bWidth);
goff.fillRect(0,0,bWidth,d . height);
goff.fillRect(d.width - bWidth,0,bWidth,d . height);
goff.fillRect(0,d.height - bWidth,d.width,b width);
goff.fillRect(batpos,d.height - 2 * bWidth - scoreH,batW,batH);
goff.fillRect(ballx,bally,ballsize,ballsize);
}
public void ShowScore() {
Goff . set font(sFont);
Goff . set color(color . white);
Goff . drawstring(" score:"+is core,40,d . height-10);
string s = " Life:"+ball num;
戈夫
。拉绳(s,d . width-40-FM small . string width(s),
d . height-10);
}
public void MoveBall() {
ballx+= balldx;
bally+= balldy;
如果(bally & lt= bWidth) {
鲍尔迪=-鲍尔迪;
bally = bWidth
}
如果(bally & gt= (d.height - ballsize - scoreH)) {
if (ingame) {
ball num-;
if(ball num & lt;= 0)
ingame = false
}
ballx = batpos+(batW-ballsize)/2;
bally = start line+line *(brickH+space);
balldy = dxval
balldx = 0;
}
if(ballx & gt;= (d.width - bWidth - ballsize)) {
balldx =-balldx;
ballx = d . width-bWidth-ballsize;
}
if(ballx & lt;= bWidth) {
balldx =-balldx;
ballx = bWidth
}
}
public void batdummymmove(){
if(ballx & lt;(batpos + 2))
bat pos-= 3;
else if(ballx & gt;(batpos + batW - 3))
bat pos+= 3;
}
公共void CheckBat() {
bat pos+= bat pos;
if(batpos & lt;bWidth)
batpos = bWidth
else if(bat pos & gt;(d.width - bWidth - batW))
batpos =(d . width-bWidth-batW);
如果(bally & gt=(d . height-scoreH-2 * b width-ballsize)
& amp& ampbally & lt(d .高度-得分H - 2 *宽度)
& amp& amp(ballx + ballsize)>batpos & amp& ampballx & lt= (batpos + batW)) {
bally = d . height-scoreH-ballsize-b width * 2;
balldy =-dxval;
balldx = CheckBatBounce(balldx,ballx-batpos);
}
}
public int CheckBatBounce(int dy,int delta) {
int符号;
int stepsize,i = -ballsize,j = 0;
stepsize =(ballsize+batW)/8;
if(dy & gt;0)
sign = 1;
其他
sign =-1;
while(我& ltbatW & amp& ampdelta & gti) {
i +=步长;
j++;
}
开关(j) {
案例0:
案例1:
return-4;
案例二:
return-3;
案例7:
返回3;
案例三:
案例6:
返回符号* 2;
案例4:
案例5:
返回标志* 1;
默认值:
返回4;
}
}
公共void CheckBricks() {
int i,j,x,y;
int xspeed = balldx
if(x speed & lt;0)
x speed =-x speed;
int ydir = balldy
如果(bally & lt(起始线-球大小)
| | bally & gt(startline + line * (space + brickH)))
返回;
for(j = 0;j & lt线;j++) {
for(I = 0;我& ltbrickNumOfLinei++) {
if(show brick[j * brickNumOfLine+I]){
y = start line+j *(space+brickH);
x = bWidth+I *(space+brickW);
如果(bally & gt=(y-ballsize)& amp;& ampbally & lt(y + brickH)
& amp& ampballx & gt=(x-ballsize)& amp;& ampballx & lt(x + brickW)) {
show brick[j * brickNumOfLine+I]= false;
如果(ingame)
is croe+=(line-j);
if(ballx & gt;= (x球尺寸)
& amp& ampballx & lt= (x球大小+ 3)) {
balldx =-x speed;
} else if(ballx & lt;= (x + brickW - 1)
& amp& ampballx & gt= (x + brickW - 4)) {
balldx = xspeed
}
balldy =-ydir;
}
}
}
}
}
公共无效运行(){
启动时间长;
graphics g = get graphics();
Thread.currentThread()。setPriority(线程。MAX _ PRIORITY);
while (true) {
start time = system . current time millis();
尝试{
油漆(克);
start time+= 20;
线程.睡眠(数学
。max(0,start time-system . current time millis());
} catch (InterruptedException e) {
打破;
}
}
}
public void start() {
if (mThread == null) {
mThread =新线程(this);
mthread . start();
}
}
公共无效站点(){
if (mThread!= null) {
mthread . stop();
mThread = null
}
}
公共静态void main(String[] args) {
框架frame =新框架(“弹球游戏”);
ballApplet app = new ballApplet();
frame.add("中心",app);
frame.setSize(270,350);
frame.validate()。
frame . set visible(true);
frame . addwindowlistener(new window control(app));
app . init();
app . start();
}
BallGameKeyListener类实现KeyListener{
@覆盖
公共void按键(按键事件e) {
int key = e . get key code();
// TODO自动生成的方法存根
if (ingame) {
开关(钥匙){
案例关键事件。VK _逃避:
ingame = false
打破;
案例关键事件。VK右:
batd pos = 4;
打破;
案例关键事件。VK _左:
batd pos =-4;
打破;
}
}否则{
if (key == KeyEvent。VK)
ingame = true
game init();
}
}
}
@覆盖
public void key released(key event e){
// TODO自动生成的方法存根
int key = e . get key code();
if (key == KeyEvent。VK _左|| key == KeyEvent。VK _右)
batd pos = 0;
}
@覆盖
公共void键入的(KeyEvent e) {
// TODO自动生成的方法存根
}
}
}
WindowControl类扩展WindowAdapter {
小程序app
公共窗口控件(Applet应用程序){
this.app = app
}
public void window closing(window event e){
app . stop();
app . destroy();
system . exit(0);
}
}
我给了你一个keyListener,然后根据你的想法给你加了一个key decision。按S键开始游戏,但是方向键不能移动。应该是你的方法不对,你可以自己修改,懒得看了。。太长了。一般直接根据坐标画出可移动物体,然后移动。+=好的,这个-=控制坐标。我发现你的错误在于你使用了变量batdpos来控制运动。。但是你根本没有用这个变量来画。。所以你不能动。。自己改吧。。