📄 gamecanvas.java
字号:
package com.supinfo.cargame;
import java.util.Random;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
public class GameCanvas extends Canvas {
private int width=getWidth();
private int height=getHeight();
private int liney=0;
private int linesize=25;
private int roadx=(int) (width*0.3);
private int roadw=width-2*roadx;
private int carx=(int) (width*0.5);
private int cary=height-40;
private int carcol;
private int carspeed=10;
private int n1=liney;
private int n2=liney+linesize*2;
private int n3=liney+linesize*4;
private int n4=liney+linesize*6;
private int n5=liney+linesize*8;
private int n6=liney+linesize*10;
private int n7=liney+linesize*12;
private int n8=liney+linesize*14;
private int ballSize=40;
private int ballX= new Random().nextInt(width-2*roadx - ballSize) + roadx;
private int ballY=linesize;
private int score=0;
public GameCanvas(int carcol) {
this.carcol=carcol;
switch (carcol) {
case 0:
this.carcol=0xff0000;
break;
case 1:
this.carcol=0x00ff00;
break;
case 2:
this.carcol=0x0000ff;
break;
case 3:
this.carcol=0xffff00;
break;
case 4:
this.carcol=0x000000;
break;
default:
break;
}
}
protected void paint(Graphics g) {
//the back ground
g.setColor(0, 150, 150);
g.fillRect(0, 0, width,height);
//the green lines
g.setColor(0, 255, 0);
g.fillRect(0, n1, width, linesize);
g.setColor(0, 255, 0);
g.fillRect(0, n2, width, linesize);
g.setColor(0, 255, 0);
g.fillRect(0, n3, width, linesize);
g.setColor(0, 255, 0);
g.fillRect(0, n4, width, linesize);
g.setColor(0, 255, 0);
g.fillRect(0, n5, width, linesize);
g.setColor(0, 255, 0);
g.fillRect(0, n6, width, linesize);
g.setColor(0, 255, 0);
g.fillRect(0, n7, width, linesize);
g.setColor(0, 255, 0);
g.fillRect(0, n8, width, linesize);
//the road
g.setColor(255,255,255);
g.fillRect(roadx, 0, roadw, height);
//the ball
g.setColor(0,0,0);
g.fillRect(ballX, ballY, ballSize, ballSize);
//the car
g.setColor(carcol);
g.fillRect(carx, cary, 20, 40);
//the top
g.setColor(207,207,207);
g.fillRect(0, 0, width, linesize);
//the words
g.setColor(0,0,0);
g.drawString("Score: " + score ,0, 0, Graphics.LEFT|Graphics.TOP);
g.setColor(255,255,0);
g.drawString("Escape Collisions GoGoGo.... ",roadx, 0, Graphics.LEFT|Graphics.TOP);
}
protected void keyPressed(int keyCode) {
if(getGameAction(keyCode) == RIGHT)
{
if(carx+20 >= roadx+roadw)
{
carx = roadx+roadw-20;
}
else
carx += carspeed;
}
if(getGameAction(keyCode) == LEFT)
{
if(carx <= roadx)
{
carx = roadx;
}
else
carx -= carspeed;
}
if(getGameAction(keyCode) == UP)
{
if(cary <= linesize)
{
cary=linesize;
}
else
cary -= carspeed;
}
if(getGameAction(keyCode) == DOWN)
{
if(cary+40 >= height)
{
cary = height-40;
}
else
cary += carspeed;
}
repaint();
}
protected void keyRepeated(int keyCode) {
if(getGameAction(keyCode) == RIGHT)
{
if(carx+20 >= roadx+roadw)
{
carx = roadx+roadw-20;
}
else
carx += carspeed;
}
if(getGameAction(keyCode) == LEFT)
{
if(carx <= roadx)
{
carx = roadx;
}
else
carx -= carspeed;
}
if(getGameAction(keyCode) == UP)
{
if(cary <= linesize)
{
cary=linesize;
}
else
cary -= carspeed;
}
if(getGameAction(keyCode) == DOWN)
{
if(cary+40 >= height)
{
cary = height-40;
}
else
cary += carspeed;
}
repaint();
}
public void moveback() {
if (n1>=height) { n1=0; } else { n1+=5; }
if (n2>=height) { n2=0; } else { n2+=5; }
if (n3>=height) { n3=0; } else { n3+=5; }
if (n4>=height) { n4=0; } else { n4+=5; }
if (n5>=height) { n5=0; } else { n5+=5; }
if (n6>=height) { n6=0; } else { n6+=5; }
if (n7>=height) { n7=0; } else { n7+=5; }
if (n8>=height) { n8=0; } else { n8+=5; }
if(ballY >= height)
{
score += 4;
ballY = 0;
ballX = new Random().nextInt(width-2*roadx - ballSize) + roadx;
}
else
ballY += 5;
if (ballY+ballSize>=cary&&carx < ballX+ballSize && carx > ballX-20) {
score -= 2;
ballY = 0;
ballX = new Random().nextInt(width-2*roadx - ballSize) + roadx;
}
repaint();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -