//MyGameFrame类
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Random;
/**
* 游戏的主窗口
* @author zzh
*
*/
public class MyGameFrame extends Frame {
Snake snake = new Snake(250,250,10);
boolean peng = false; //碰撞检测
Random randx = new Random();
Random randy = new Random();
Food food = new Food(100,100); //初始化食物
public void paint(Graphics g) {
Color c = g.getColor();
g.setColor(Color.WHITE); //填充白色背景
g.fillRect(0, 0, 500, 500);
g.setColor(c);
snake.drawSelf(g);
food.draw(g);
//碰撞检测,判断蛇是否吃到了食物
//吃到食物后,改变食物出现的位置
peng = food.getRect().intersects(snake.getRect());
if(peng) {
System.out.println("吃到了");
snake.snakelen +=1;
food.setX(randx.nextInt(400)+40);
food.setY(randy.nextInt(400)+40);
System.out.println("food: "+food.getRect().x+" & "+food.getRect().y);
}
}
class PaintThread extends Thread {
public void run() {
while (true) {
//System.out.println("重画窗口");
repaint();
try {
Thread.sleep(150);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
//键盘监听
class KeyMonitor extends KeyAdapter{
public void keyPressed(KeyEvent e) {
snake.addDirection(e);
}
}
/**
* 初始化窗口
*/
public void launchFrame(){
this.setTitle("贪吃蛇");
this.setVisible(true);
this.setSize(Constant.GAME_WIDTH, Constant.GAME_HEIGHT);
this.setLocation(500, 180);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
new PaintThread().start();
addKeyListener(new KeyMonitor());
}
//主函数
public static void main(String[] args) {
MyGameFrame f = new MyGameFrame();
f.launchFrame();
}
private Image offScreenImage = null;
public void update(Graphics g) {
if(offScreenImage == null)
offScreenImage = this.createImage(Constant.GAME_WIDTH,Constant.GAME_HEIGHT);//这是游戏窗口的宽度和高度
Graphics gOff = offScreenImage.getGraphics();
paint(gOff);
g.drawImage(offScreenImage, 0, 0, null);
}
}
//常量类
public class Constant {
public static final int GAME_WIDTH = 500;
public static final int GAME_HEIGHT = 500;
}
//蛇类
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
public class Snake{
boolean left = true;
boolean up,right,down;
double x,y;
int speed;
int width=10;
int height=10;
int len = 100; //定义蛇头保存多少个坐标值
int snakelen = 2; //蛇的初始长度
int[][] pos = new int[100][2]; //蛇头走过的所有坐标
int[][] newpos = new int[1][2]; //保存蛇头每次变化后的最新坐标
public void drawSelf(Graphics g) {
Color c = g.getColor();
g.setColor(Color.RED);
//填充正方形,蛇头为红色
g.fillRect((int)x, (int)y, 10, 10);
g.setColor(Color.BLACK);
for (int i = 1; i < snakelen; i++) {
g.fillRect(pos[i][0], pos[i][1], 10, 10);
}
g.setColor(c);
// 允许穿过边界
if(left) {
if (x <=15 ) {
x=478;
}
x -= speed;
newpos[0][0] = (int)x;
}
if (right) {
if (x >= 478) {
x = 15;
}
x += speed;
newpos[0][0] = (int)x;
}
if (up) {
if (y <= 32) {
y=479;
}
y -= speed;
newpos[0][1] = (int)y;
}
if (down) {
if (y >= 479) {
y=32;
}
y += speed;
newpos[0][1] = (int)y;
}
g.setColor(c);
//中间变量,用来重组pos数组
int[][] tmp = new int[len][2];
System.arraycopy(newpos, 0, tmp, 0, 1);
for (int s = 1; s < len; s++) {
tmp[s][0]=pos[s-1][0];
tmp[s][1]=pos[s-1][1];
}
for (int s = 0; s < len; s++) {
pos[s][0]=tmp[s][0];
pos[s][1]=tmp[s][1];
}
}
//重载
public Snake (double x, double y,int speed) {
this.x = x;
this.y = y;
this.speed = speed;
pos[0][0] = (int)x;
pos[0][1] = (int)y;
newpos[0][0] = (int)x;
newpos[0][1] = (int)y;
}
//用户碰撞检测
public Rectangle getRect() {
return new Rectangle((int)x, (int)y, width, height);
}
// 改变方向, 不允许在同一直线上掉头
public void addDirection(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_LEFT:
if(! right) {
left = true;
up = false;
right = false;
down = false;
}
break;
case KeyEvent.VK_UP:
if(! down) {
left = false;
up = true;
right = false;
down = false;
}
break;
case KeyEvent.VK_RIGHT:
if(! left) {
left = false;
up = false;
right = true;
down = false;
}
break;
case KeyEvent.VK_DOWN:
if(! up) {
left = false;
up = false;
right = false;
down = true;
}
break;
default:
break;
}
}
}
//食物类
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
public class Food {
int x ;
int y ;
int width = 10;
int height = 10;
//重载
public Food(int x, int y) {
this.x = x;
this.y = y;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
//用户碰撞检测
public Rectangle getRect() {
return new Rectangle((int)x, (int)y, width, height);
}
public void draw(Graphics g) {
Color c = g.getColor();
g.setColor(Color.GREEN);
g.fillOval((int)x, (int)y, width, height);
g.setColor(c);
}
}
用Java编写贪吃蛇小游戏
最新推荐文章于 2024-06-18 08:22:08 发布