import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import java.util.*;
import java.io.*;
public class HCanvas extends GameCanvas implements Runnable {
private Display display;
private boolean sleeping;
private long frameDelay;
private int inputDelay;
private Random rand;
private Image background;
private Image chickenHead;
private Sprite chickenSprite;
private Sprite[] carSprite = new Sprite[4];
private int[] carYSpeed = new int[4];
private boolean gameOver;
private int numLives;
private int score;
public HCanvas(Display d) {
super(true);
display = d;
// Set the frame rate (30 fps)
frameDelay = 33;
// Clear the input delay
inputDelay = 0;
}
public void start() {
// Set the canvas as the current screen
display.setCurrent(this);
// Initialize the random number generator
rand = new Random();
// Initialize the game variables
gameOver = false;
numLives = 3;
score = 0;
// Initialize the background image and chicken and car sprites
try {
background = Image.createImage("/Highway.png");
chickenHead = Image.createImage("/ChickenHead.png");
chickenSprite = new Sprite(Image.createImage("/Chicken.png"), 22, 22);
chickenSprite.setPosition(2, 77);
carSprite[0] = new Sprite(Image.createImage("/Car1.png"));
carSprite[0].setPosition(27, 0);
carYSpeed[0] = 3;
carSprite[1] = new Sprite(Image.createImage("/Car2.png"));
carSprite[1].setPosition(62, 0);
carYSpeed[1] = 1;
carSprite[2] = new Sprite(Image.createImage("/Car3.png"));
carSprite[2].setPosition(93, 67);
carYSpeed[2] = -2;
carSprite[3] = new Sprite(Image.createImage("/Car4.png"));
carSprite[3].setPosition(128, 64);
carYSpeed[3] = -5;
}
catch (IOException e) {
System.err.println("Failed loading images!");
}
// Start the animation thread
sleeping = false;
Thread t = new Thread(this);
t.start();
}
public void stop() {
// Stop the animation
sleeping = true;
}
public void run() {
Graphics g = getGraphics();
// The main game loop
while (!sleeping) {
update();
draw(g);
try {
Thread.sleep(frameDelay);
}
catch (InterruptedException ie) {}
}
}
private void update() {
// Check to see whether the game is being restarted
if (gameOver) {
int keyState = getKeyStates();
if ((keyState & FIRE_PRESSED) != 0) {
// Start a new game
chickenSprite.setPosition(2, 77);
gameOver = false;
score = 0;
numLives = 3;
}
// The game is over, so don't update anything
return;
}
// Process user input to move the chicken
if (++inputDelay > 2) {
int keyState = getKeyStates();
if ((keyState & LEFT_PRESSED) != 0) {
chickenSprite.move(-6, 0);
chickenSprite.nextFrame();
}
else if ((keyState & RIGHT_PRESSED) != 0) {
chickenSprite.move(6, 0);
chickenSprite.nextFrame();
}
if ((keyState & UP_PRESSED) != 0) {
chickenSprite.move(0, -6);
chickenSprite.nextFrame();
}
else if ((keyState & DOWN_PRESSED) != 0) {
chickenSprite.move(0, 6);
chickenSprite.nextFrame();
}
checkBounds(chickenSprite, false);
// Reset the input delay
inputDelay = 0;
}
// See whether the chicken made it across
if (chickenSprite.getX() > 154) {
// Play a sound for making it safely across
AlertType.WARNING.playSound(display);
// Reset the chicken position and increment the score
chickenSprite.setPosition(2, 77);
score += 25;
}
// Update the car sprites
for (int i = 0; i < 4; i++) {
// Move the car sprites
carSprite[i].move(0, carYSpeed[i]);
checkBounds(carSprite[i], true);
// Check for a collision between the chicken and cars
if (chickenSprite.collidesWith(carSprite[i], true)) {
// Play a sound for losing a chicken
AlertType.ERROR.playSound(display);
// Check for a game over
if (--numLives == 0) {
gameOver = true;
} else {
// Reset the chicken position
chickenSprite.setPosition(2, 77);
}
// No need to continue updating the car sprites
break;
}
}
}
private void draw(Graphics g) {
// Draw the highway background
g.drawImage(background, 0, 0, Graphics.TOP | Graphics.LEFT);
// Draw the number of remaining lives
for (int i = 0; i < numLives; i++)
g.drawImage(chickenHead, 180 - ((i + 1) * 8), 170, Graphics.TOP | Graphics.LEFT);
// Draw the chicken sprite
chickenSprite.paint(g);
// Draw the car sprites
for (int i = 0; i < 4; i++)
carSprite[i].paint(g);
if (gameOver) {
// Draw the game over message and score
g.setColor(255, 255, 255); // white
g.setFont(Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_BOLD, Font.SIZE_LARGE));
g.drawString("GAME OVER", 90, 40, Graphics.TOP | Graphics.HCENTER);
g.setFont(Font.getFont(Font.FACE_MONOSPACE, Font.STYLE_BOLD, Font.SIZE_MEDIUM));
g.drawString("You scored " + score + " points.", 90, 70, Graphics.TOP |
Graphics.HCENTER);
}
// Flush the offscreen graphics buffer
flushGraphics();
}
private void checkBounds(Sprite sprite, boolean wrap) {
// Wrap/stop the sprite if necessary
if (wrap) {
// Wrap the sprite around the edges of the screen
if (sprite.getX() < -sprite.getWidth())
sprite.setPosition(getWidth(), sprite.getY());
else if (sprite.getX() > getWidth())
sprite.setPosition(-sprite.getWidth(), sprite.getY());
if (sprite.getY() < -sprite.getHeight())
sprite.setPosition(sprite.getX(), getHeight());
else if (sprite.getY() > getHeight())
sprite.setPosition(sprite.getX(), -sprite.getHeight());
}
else {
// Stop the sprite at the edges of the screen
if (sprite.getX() < 0)
sprite.setPosition(0, sprite.getY());
else if (sprite.getX() > (getWidth() - sprite.getWidth()))
sprite.setPosition(getWidth() - sprite.getWidth(), sprite.getY());
if (sprite.getY() < 0)
sprite.setPosition(sprite.getX(), 0);
else if (sprite.getY() > (getHeight() - sprite.getHeight()))
sprite.setPosition(sprite.getX(), getHeight() - sprite.getHeight());
}
}
}
Henway3.rar_Henway
版权申诉
156 浏览量
2022-09-23
22:54:34
上传
评论
收藏 50KB RAR 举报
![avatar](https://profile-avatar.csdnimg.cn/dc78d2406d17417ca42db3bd43b9c72a_weixin_42652674.jpg!1)
御道御小黑
- 粉丝: 62
- 资源: 1万+
最新资源
- 关于如何快速入手Kotlin
- ALTIUM库 常用电阻电容芯片类2D3D PCB封装库AD库(2D3D封装库).zip
- keil5安装教程keil5安装教程
- 所得到的多多多多多多多
- GNSS芯片 AG3335M芯片的,EVB 硬件设计图
- 阿云主机网络连接详细操作.docx
- 网络爬虫软件研究与开发pdf
- Java项目-基于SSM+JSP的母婴用品网站的设计与实现(源码+数据库脚本+部署视频+代码讲解视频+全套软件)
- 基于微信小程序的购物商城app设计带Java后端+源代码+文档说明+数据库.zip
- 基于51单片机外设应用设计.DSN后缀PROTEUS仿真仿真源文件及C语言实例源码例程合集(300个).zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)