学完线程的时候做了一个精灵吃豆的小游戏。
窗体类:
package www.csdn.net.zuoye;
//用窗体实现精灵吃豆子的小游戏
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class GameJFrame extends JFrame implements KeyListener {
// 定义起始位置
private int x = 30, y = 30;
// 定义中间变量
private boolean flag = true;
private static final int DIR_UP = 1;
private static final int DIR_DOWN = 2;
private static final int DIR_LEFT = 3;
private static final int DIR_RIGHT = 4;
// 定义初始化弧度
private int arc1 = 20;
private int arc2 = 320;
// 定义计数器(通过计数器让上下两张嘴都动起来)
private int count = 0;
// 定义豆的对象
private Bean bean;
// 定义方向
private int dir = -1;
// 生成随机数
private Random random;
//定义分数
private int score=0;
//定义速度
private int k=100;
public GameJFrame() {
this.setBounds(300, 200, 200, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
this.setResizable(false);
/*
* 1.this.setBackground(Color.gray);
* this.getContentPane().setVisible(false);
*/
this.getContentPane().setBackground(Color.pink);
new Thread() {
public void run() {
while (flag) {
try {
boolean temp = checkHit();
if (temp) {
initBean();
score++;
if(score%10==0){
k-=5;
}
}
Thread.sleep(k);
count++;
if (count % 2 == 0) {// 嘴张开以及闭住的情况
arc1 = 40;
arc2 = 280;
} else {
arc1 = 20;
arc2 = 320;
}
switch (dir) {
case DIR_UP:
y -= 5;
break;
case DIR_DOWN:
y += 5;
break;
case DIR_LEFT:
x -= 5;
break;
case DIR_RIGHT:
x += 5;
break;
default:
break;
}
repaint();
if (y > 275 || y < 30) {
flag = false;
JOptionPane.showMessageDialog(null, "游戏结束,你的分数为:"+score);
dispose();
}else if(x>176||x==0){
flag=false;
JOptionPane.showMessageDialog(null, "游戏结束,你的分数为:"+score);
dispose();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
// 添加监听
this.addKeyListener(this);
// 创建随机的对象
random = new Random();
initBean();
}
public void initBean() {
// 随机产生坐标
int rx = random.nextInt(5)+10;
int ry = random.nextInt(15)+7;
// 产生豆
bean = new Bean(rx * Bean.r * 2, ry * Bean.r * 2);// 坐标必须是随机产生的
}
@Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.red);// 设置绘制的颜色
switch (dir) {
case DIR_UP:
g.fillArc(x, y, 20, 20, arc1 + 90, arc2);
break;
case DIR_DOWN:
g.fillArc(x, y, 20, 20, arc1 + 270, arc2);
break;
case DIR_LEFT:
g.fillArc(x, y, 20, 20, arc1 + 180, arc2);
break;
case DIR_RIGHT:
g.fillArc(x, y, 20, 20, arc1, arc2);
break;
default:// 为了能够显示,需要有个显示的状态
g.fillArc(x, y, 20, 20, arc1, arc2);
}
// 绘制豆
g.setColor(Color.yellow);
g.fillArc(bean.getX(), bean.getY(), Bean.r * 2, Bean.r * 2, 0, 360);
// g.fillArc(x, y, 20, 20, arc1, arc2);//圆形
// g.fillRect(50, 30, 20, 20);//矩形
}
public static void main(String[] args) {
GameJFrame g = new GameJFrame();
g.setVisible(true);
}
/**
* 输入某个键的时候执行
*/
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
System.out.println("键入");
}
/**
* 按下的时候执行
*/
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
// System.out.println("按键");
// 获取按键的值
int key = e.getKeyCode();// 查询键值(用这个方法给上下左右键加事件)
// 根据上下左右箭头改变方法
switch (key) {
case 38:
dir = DIR_UP;
break;
case 40:
dir = DIR_DOWN;
break;
case 37:
dir = DIR_LEFT;
break;
case 39:
dir = DIR_RIGHT;
break;
default:
break;
}
}
/**
* 抬起的时候执行
*/
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
// System.out.println("抬起");
}
// 定义碰撞检测
public boolean checkHit() {
if (bean != null) {
if (bean.getX() == x) {
if (Math.abs(bean.getY() - y) < 13) {
return true;// 被碰到了
}
}
if (bean.getY() == y) {
if (Math.abs(bean.getX() - x) < 13) {
return true;
}
}
}
return false;
}
}
豆子类:
package www.csdn.net.zuoye;
//绘制豆
public class Bean {
// 坐标
private int x, y;
// 豆的半径
public static int r=5;
public int getX() {//获取横坐标
return x;
}
public int getY() {//获取纵坐标
return y;
}
public Bean(int x, int y) {
this.x = x;
this.y = y;
}
}
效果: