一个小游戏

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public  class StartGame extends JFrame implements ActionListener
{
protected JButton start,pause,renew;
Gameing panel;
People people;
boolean stop=false;
final int W=100,H=30;
public StartGame()
{
panel=new Gameing(this);
people=new People(panel);


start =new JButton("开始游戏");
pause =new JButton("暂停游戏");
renew =new JButton("重新开始");


Font fnt=new Font("Serief",Font.ITALIC+Font.BOLD,15);   
this.setLayout(null);


start.setLocation(10,425);
start.setSize(W,H);
start.setFont(fnt);
start.setBackground(Color.GRAY);


pause.setLocation(250,425);
pause.setSize(W,H);
pause.setFont(fnt);
pause.setBackground(Color.GRAY);


renew.setLocation(476,425);
renew.setSize(W,H);
renew.setFont(fnt);
renew.setBackground(Color.GRAY);


this.setLocation(500,200);
this.setSize(600,500);


this.add(start);              //添加按钮
this.add(pause);
this.add(renew);


panel.setBounds(0, 0, 600, 420);//将panel上的东西添加到窗口上
this.add(panel);


start.addActionListener(this);
pause.addActionListener(this);//添加事件监听
renew.addActionListener(this);


this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setResizable(false);
panel.requestFocusInWindow();
}


public static void main(String[] args)
{
new StartGame();
}


public void actionPerformed(ActionEvent e)
{
JButton button = (JButton) e.getSource();
if(button==start)
{
panel.begin=true;
stop=true;
}
else if(button==renew)

if (panel.begin) {
panel.gamejudge(true);//按钮的判断
}
}
else if(button==pause)
{   

if(stop){
if (panel.begin)                                     //设置暂停
panel.begin=false;
panel.people.pause();
int n=JOptionPane.showConfirmDialog(this,"继续","",JOptionPane.DEFAULT_OPTION);
if(n==JOptionPane.OK_OPTION )
{
panel.begin=true;
}
else 
System.exit(0);
}
}
panel.requestFocusInWindow();   //设置焦点
}

}

以上是第一个类,Startgame类名



import java.awt.Font;
import java.awt.Graphics;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;


public class Gameing extends JPanel
{   
Block rect[]=new Block[4];
public boolean begin=false;
public boolean judge=false;
public int time=1;
protected int score;
People people;


private StartGame renew;


public Gameing(StartGame renew)
{
this.renew=renew;
people = new People(this);
ImageIcon background = new ImageIcon("./image/葡萄.jpg");    //添加背景图片
JLabel label = new JLabel(background);  
label.setLocation(0, 0);
label.setSize(800, 620);
this.add(label);




for(int i=0;i<rect.length;i++)
{
rect[i]=new Block(450-i*100);
}
people.set_rect(rect);
people.y=-50;
people.x=(int)((500)*Math.random());
people.y++;
people.x = rect[0].x;
new Thread(new paintThread()).start();       //线程的启动
}


public void paint(Graphics g)
{
super.paint(g);


for(int i=0;i<rect.length;i++)
{
rect[i].drawblock(g); //画小人及移动
rect[i].blockmove(g);
}
people.draw(g);
people.move();
speed_up();//加速方法
g.setFont(new Font("Serief",Font.ITALIC,20));
g.drawString("分数"+score, 0, 20);
}


class paintThread implements Runnable{
public void run() {
while(true) {
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(begin) {
// System.out.println(1);
repaint();
}

}
}
}


public void speed_up()
{
if(begin) 
time++;
if (time%100==0)                                 //设置分数
score++;
if(time%700==0)                                  //设置速度
{
time=1;
for(int i=0;i<rect.length;i++)
rect[i].speed++;
}
}
public void dialog(String message,int type) {
begin = false;
people.pause();
System.out.println("SHOW");
int m=JOptionPane.showConfirmDialog(this,message,message,JOptionPane.YES_NO_OPTION);
if(m==JOptionPane.YES_OPTION)
{         
reset();
}
else if(m==JOptionPane.NO_OPTION)
{
if(type == 0)
begin=true;
else if(type == 1)
System.exit(0);
}
else
System.exit(0);
}
public void reset() {
System.out.println(1);
people.x = rect[0].x;
score = 0;
time=1;
for(int i=0;i<rect.length;i++) {
rect[i].reset(450-100*i);
}
people.renew();
System.out.println("BEGIN:"+begin);
try {
Thread.sleep(20);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
begin = true;


}
public void gamejudge(Boolean judge)
{
if(judge)
{
this.judge=false;
dialog("是否重新开始游戏",0);
}
}

第二个类 gaming类名

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;


import javax.swing.ImageIcon;


public class Block {
public int x;
public int y=400;
final public int WIDTH=120;
final public int HEIGHT=20;
public int speed=1;
public int time;
Boolean began =false;
ImageIcon picture;
Block rect[]=new Block[4];
People people;
Gameing panel;
public int type;
public Block(int y)
{
x=(int)((500)*Math.random());
this.y=y;  
}


public void setType() {
if(type==1 ){
picture =new  ImageIcon("./Image/jc.png");//加图片
}
else if(type == 2){
picture =new  ImageIcon("./Image/xs.png");
}
}

public void drawblock(Graphics g)
{
g.setColor(Color.PINK);
if(type == 0) {
g.fillRect(x,y,WIDTH,HEIGHT);
}else if (type == 1){
g.drawImage(picture.getImage(), x, y,WIDTH,HEIGHT, null);
}else if (type == 2){
g.drawImage(picture.getImage(), x, y,WIDTH,HEIGHT, null);
}
}

public void reset(int y) {
this.y = y;
this.type = 0;
x=(int)((500)*Math.random());
speed = 1;
}

public void setBackImg() {
int probability = (int) (Math.random()*11);
if(probability<4) {
type =0;
}else if(probability<8){
type =1;
}else {
type =2;
}
setType();
}


public void blockmove(Graphics g)
{
if(y>0)
{
y-=speed;
}
if(y<=0)                              //设小块的速度
{
y=400;
x=(int)((500)*Math.random());
setBackImg();
y-=speed;
}
}
Rectangle Clood(){                       //设置矩形范围
return new Rectangle(x,y,WIDTH,HEIGHT);
}
}

第三个类,block类名



import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;


import javax.swing.ImageIcon;
import javax.swing.JButton;


public class People 
{
final int p=50,q=50;
public int speed = 3;
public int x=(int)((500)*Math.random()),y=400;
Block rect[];
boolean key_left=false ,key_right=false,rb_move = true,lb_move = true;
ImageIcon left,right;
ImageIcon temp;
Gameing game;
public People(Gameing game)
{
left =new  ImageIcon("./Image/11111.png");//加图片
right = new  ImageIcon("./Image/22222.png");
temp = left;


this.game = game;
this.game.addKeyListener(new MyKey());
}


public void draw(Graphics g) 
{
if(x>560) x=560;                                     //加边界
if(x<=0) x=0;
if(y<0) y=0;
g.drawImage(temp.getImage(), x, y,p,q, null);
this.peopleMove();
}
public void set_rect(Block [] rect)
{
this.rect = rect;
}
public void move()
{
rb_move = true;
lb_move = true;
for(int i=0;i<rect.length;i++)
{
if(rect[i].Clood().intersectsLine(x-5,y+p,x+q-5,y+q))
{
if(rect[i].type==1) {
game.dialog("是否重新开始游戏",1);
}
if(rect[i].type==2)
x++;
if(set_move(rect[i]))
up(rect[i].speed);                                   //上下碰撞检测
return;
}
else if(!set_move(rect[i]))
{
below(rect[i].speed+1);
return;
}
}
below(rect[0].speed+1);
}


public void renew() {
x=(int)((500)*Math.random());
y=1;
temp = left;
rb_move = true;
lb_move = true;
key_left = false;
key_right = false;
}
private boolean set_move(Block rect)
{
if(rect.Clood().intersectsLine(x+p,y,x+p,y+q-5))
{
rb_move = false;
return false;                                        //左右边界的碰撞
}
if(rect.Clood().intersectsLine(x,y,x,y+q-5))
{
lb_move = false;
return false;
}
return true;
}
public void below(int speed)
{
this.y += speed;
if(this.y>450)
{
game.begin=false;
game.dialog("是否重新开始游戏",1);
}                                                       //碰撞弹出对话框
}
public void up(int speed)
{
this.y -= speed;
if(this.y<0)
{
game.begin=false;
game.dialog("是否重新开始游戏",1);
}
}
public void peopleMove()
{
if(key_left &&!key_right && lb_move && game.begin){
x-=5;
temp = left;
}                                                                        //小人的在左右移动
if(!key_left && key_right  && rb_move && game.begin){
x+=5;
temp = right;
}
}

public void pause() {
key_left = false;
key_right = false;
}


class MyKey extends KeyAdapter
{
public void keyPressed(KeyEvent e)
{
int code = e.getKeyCode();
if(code==KeyEvent.VK_LEFT && game.begin)
{
key_left =true ;


}                                                               //按下键可以移动
else if(code==KeyEvent.VK_RIGHT && game.begin)

key_right =true ;


}
}
public void keyReleased(KeyEvent e) 
{
int code = e.getKeyCode();
if(code==KeyEvent.VK_LEFT && game.begin)
{
key_left =false ;
}                                                               //松开时不能移动
else if(code==KeyEvent.VK_RIGHT && game.begin)

key_right =false ;
}
}


}

}

第四个类  People类名


有兴趣的可以看看哦!






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值