- 生肖
- 马
- 星座
- 摩羯座
- 情感状态
- 光棍
- 性别
- 男
- 积分
- 373
- 积分
- 425
- 精华
- 2
- 阅读权限
- 30
- 注册时间
- 2012-4-29
- 最后登录
- 2013-10-11
- 帖子
- 62
- 生肖
- 马
- 星座
- 摩羯座
- 性别
- 男
|
本帖最后由 sky_yx 于 2015-12-30 14:09 编辑
Enemy.java
- import java.awt.Graphics;
- import java.awt.Image;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.IOException;
- import java.util.Random;
- import javax.imageio.ImageIO;
- public class Enemy {
- private int x;
- public int getX() {
- return x;
- }
- public void setX(int x) {
- this.x = x;
- }
- private int y;
- public int getY() {
- return y;
- }
- public void setY(int y) {
- this.y = y;
- }
- Random rdm_x=new Random(); //只需让x坐标为随机的
- Random rdm_car=new Random(); //车的类型也是随机的
- //四辆阻挡汽车,随机产生
- File file1=new File("D:/图片/enemy1.jpg");
- File file2=new File("D:/图片/enemy2.jpg");
- File file3=new File("D:/图片/enemy3.jpg");
- File file4=new File("D:/图片/enemy4.jpg");
- private BufferedImage imgEnemy;
- public Enemy()
- {
- y=80;
- x=rdm_x.nextInt(500)+201;
- //随机产生一种车
- switch (rdm_car.nextInt(4))
- {
- case 0:
- try {
- imgEnemy=ImageIO.read(file1);
- } catch (IOException e) {
- e.printStackTrace();
- }
- break;
- case 1:
- try {
- imgEnemy=ImageIO.read(file2);
- } catch (IOException e) {
- e.printStackTrace();
- }
- break;
- case 2:
- try {
- imgEnemy=ImageIO.read(file3);
- } catch (IOException e) {
- e.printStackTrace();
- }
- break;
- case 3:
- try {
- imgEnemy=ImageIO.read(file4);
- } catch (IOException e) {
- e.printStackTrace();
- }
- break;
- default:
- break;
- }
- }
- public void Draw(Graphics g)
- {
- g.drawImage(imgEnemy,x,this.y,null);
- y+=10;
- }
- }
复制代码 GPXMain.java
- import javax.swing.JFrame;
- public class GPXMain extends JFrame {
- public GPXMain()
- {
- GPXWin win=new GPXWin();
- this.add(win);
- }
- public static void main(String[] args) {
- new GPXMain();
- }
- }
复制代码 GPXWin.java
- import java.awt.Graphics;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.KeyEvent;
- import java.awt.event.KeyListener;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.List;
- import javax.imageio.ImageIO;
- import javax.swing.JButton;
- import javax.swing.JPanel;
- public class GPXWin extends JPanel implements ActionListener,KeyListener,Runnable{
- //玩家分数
- private int score;
- //这个变量是用来轮流显示两张马路图片的
- private int Road_Flag=1;
- Road road=new Road();
- JButton startGame=new JButton("开始");
- JButton stopGame=new JButton("结束");
- Player player;
- //游戏线程
- Thread nThread;
- Enemy enemy=new Enemy();
- BufferedImage gameover;
- File gameover_file=new File("D:/图片/gameover.jpg");
- public GPXWin()
- {
- this.score=0;
- try {
- gameover=ImageIO.read(gameover_file);
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- player=new Player();
- this.add(startGame);
- this.add(stopGame);
- startGame.addActionListener(this);
- stopGame.addActionListener(this);
- this.addKeyListener(this);
- }
- public void paintComponent(Graphics g)
- {
- super.paintComponent(g);
- g.drawString("您的分数是:"+score,30,30);
- if(!Check_Hit())
- {
- repaint();
- g.drawImage(gameover,200,80,null);
- return;
- }
- g.drawRect(30, 80, 900, 600);
- road.Draw(g,Road_Flag);
- player.Draw(g);
- Check_Limit();
- enemy.Draw(g);
- }
- @Override
- public void keyPressed(KeyEvent e) {
- switch (e.getKeyCode()) {
- case KeyEvent.VK_UP:
- this.player.Move(0, -5);
- break;
- case KeyEvent.VK_DOWN:
- this.player.Move(0, 5);
- break;
- case KeyEvent.VK_LEFT:
- this.player.Move(-5, 0);
- break;
- case KeyEvent.VK_RIGHT:
- this.player.Move(5, 0);
- break;
- default:
- break;
- }
- }
- @Override
- public void keyReleased(KeyEvent arg0) {
- // TODO Auto-generated method stub
- }
- @Override
- public void keyTyped(KeyEvent arg0) {
- // TODO Auto-generated method stub
- }
- @Override
- public void actionPerformed(ActionEvent e) {
- if(e.getSource()==startGame)
- {
- nThread=new Thread(this);
- nThread.start();
- this.requestFocus();
- repaint();
- }
- if(e.getSource()==stopGame)
- {
- System.exit(0);
- }
- }
- //这个方法用来检测敌人是否出屏幕
- private void Check_Limit()
- {
- if(this.enemy.getY()+100>=680)
- {
- this.enemy=null;
- this.enemy=new Enemy();
- }
- }
- //判定相撞
- private boolean Check_Hit()
- {
- //四种情况判定
- boolean left_up=enemy.getY()+120>player.getY()&&enemy.getX()+55>player.getX()&&enemy.getY()<player.getY()&&enemy.getX()<player.getX();
- boolean right_up=enemy.getX()>player.getX()&&enemy.getX()<player.getX()+53&&enemy.getY()+120>player.getY()&&enemy.getY()+120<player.getY()+109;
- boolean left_down=enemy.getX()+55>player.getX()&&enemy.getX()+55<player.getX()+53&&player.getY()+109>enemy.getY()&&player.getY()+109<enemy.getY()+120;
- boolean right_down=player.getX()+53>enemy.getX()&&player.getX()+53<enemy.getX()+55&&player.getY()+109>enemy.getY()&&player.getY()+109<enemy.getY()+120;
- if(left_up||right_up||left_down||right_down)
- {
- nThread.stop();
- enemy=null;
- player=null;
- road=null;
- return false;
- }
- return true;
- }
- @Override
- public void run() {
- while(true)
- {
- this.score+=10;
- if(Road_Flag==1)
- {
- try {
- Thread.sleep(100);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- Road_Flag=2;
- repaint();
- }
- if(Road_Flag==2)
- {
- try {
- Thread.sleep(100);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- Road_Flag=1;
- repaint();
- }
- }
- }
- }
复制代码 Player.java
- import java.awt.Graphics;
- import java.awt.event.KeyAdapter;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import javax.imageio.ImageIO;
- public class Player extends KeyAdapter {
- private BufferedImage img;
- private int x;
- public int getX() {
- return x;
- }
- public void setX(int x) {
- this.x = x;
- }
- private int y;
- public int getY() {
- return y;
- }
- public void setY(int y) {
- this.y = y;
- }
- public Player()
- {
- try {
- img=ImageIO.read(new File("D:/图片/Player.jpg"));
- } catch (Exception e) {
- // TODO: handle exception
- }
- x=400;y=400;
- }
- //画player的方法
- public void Draw(Graphics g)
- {
- g.drawImage(img, this.x,this.y,null);
- }
- //只能左,右,上移动
- public void Move(int x,int y)
- {
- this.x+=x;
- this.y+=y;
- }
- }
复制代码 Road.java
- import java.awt.Graphics;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.IOException;
- import javax.imageio.ImageIO;
- public class Road {
- File file1=new File("D:/图片/HorseRoad1.jpg");
- File file2=new File("D:/图片/HorseRoad2.jpg");
- private BufferedImage imgRoad;
- private int x;
- public int getX() {
- return x;
- }
- public void setX(int x) {
- this.x = x;
- }
- private int y;
- public int getY() {
- return y;
- }
- public void setY(int y) {
- this.y = y;
- }
- public Road()
- {
- x=200;
- y=80;
- }
-
- public void Draw(Graphics g,int flag)
- {
- if(flag==1)
- {
- try {
- imgRoad=ImageIO.read(file1);
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- g.drawImage(imgRoad,this.x,this.y,null);
- }
- if(flag==2)
- {
- try {
- imgRoad=ImageIO.read(file2);
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- g.drawImage(imgRoad,this.x,this.y,null);
- }
- }
- }
复制代码
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?立即注册
|