码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA如何实现一个小游戏

时间:2020-02-18 20:37:54      阅读:101      评论:0      收藏:0      [点我收藏+]

标签:oca   颜色   交流   文件路径   bool   raw   owa   start   phi   

话不多说上代码

package 滑稽快闪;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;

/*
* 游戏物体父类
*/
public class gameObject {
Image img;
double x,y;
int speed;
int width,height;

//画出物体
public void drawSelf(Graphics g) {
g.drawImage(img,(int) x,(int) y, null);
}
//构造
public gameObject(Image img, double x, double y, int speed, int width, int height) {
super();
this.img = img;
this.x = x;
this.y = y;
this.speed = speed;
this.width = width;
this.height = height;
}

public gameObject(Image img, double x, double y) {
super();
this.img = img;
this.x = x;
this.y = y;
}

public gameObject() {

}


/*
* 返回物体矩形
* 现在没用
*/

public Rectangle getRect() {
return new Rectangle((int)x,(int)y,width,height);

}



}

 

 

 

package 滑稽快闪;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.net.URL;
import javax.imageio.ImageIO;

public class GameUtil {
//工具类最好构造私有化
private GameUtil() {

}
/*
* 指定文件路径返回对象
*/
public static Image getImage(String path) {
BufferedImage bi = null;
try {
URL u = GameUtil.class.getClassLoader().getResource(path);
bi = ImageIO.read(u);
} catch (Exception e) {
e.printStackTrace();
}
return bi;

}

}

 

 

package 滑稽快闪;

import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Window;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.net.MalformedURLException;
import java.util.Date;
import java.util.Scanner;

import javax.swing.JFrame;

public class Mygame extends JFrame{

Image planeimg = GameUtil.getImage("images/飞机1.1.png");

Image bg = GameUtil.getImage("images/555.jpg");



//int x=250,y =250;//飞机坐标

Plane plane = new Plane(planeimg,400,400);
//Plane plane2 = new Plane(planeimg,350,350);

Shell shell =new Shell();//子弹类
Shell[]shells = new Shell[10];//声明,初始化

Date startTime = new Date();
Date andTime;

private String period;
@Override
public void paint(Graphics g) { //g相当画笔
// Color c =g.getColor();
// g.setColor(Color.BLACK);
// g.drawLine(100, 100, 300, 300);
// g.drawRect(100, 100, 300, 300);
// g.drawOval(100, 100, 300, 300);
// g.fillRect(100, 100, 200, 200);
// g.setColor(c);
g.drawImage(bg, 0, 0, null);//游戏图片导入

plane.drawSelf(g);//画飞机
// plane2.drawSelf(g);//画飞机
shell.draw(g);

for(int i=0;i<shells.length;i++) {
shells[i].draw(g);
boolean peng = shells[i].getRect().intersects(plane.getRect());
//飞机相撞检查
if(peng) {
plane.live=false;
Date endTime = new Date();
int period = (int)(endTime.getTime()-startTime.getTime())/1000;//时间
if(!plane.live) {//死亡字体
g.setColor(Color.red);
g.drawString("时间"+period+"秒",(int)plane.x ,(int)plane.y);//最后时间


}

}

}

}




/*
* 内部类的好处是,可以直接使用外部类
*/

class PaintThread extends Thread{
@Override
public void run() {
while(true) { //帮我们反复重画窗口
// System.out.println("画一次");
repaint();//重画

try {
Thread.sleep(40); //1s = 1000ms
} catch (InterruptedException e) {

e.printStackTrace();
}

}
}
}
//定义键盘监听

class KeyMonitor extends KeyAdapter{

@Override
public void keyPressed(KeyEvent e) {//按下键的信息
plane.addDirection(e);
}

@Override
public void keyReleased(KeyEvent e) {//抬起
plane.minusaddDirection(e);
}
}

/*
* 初始化窗口
*/
public void lauchFrame() {
this.setTitle("滑稽快闪!");//窗口标题
this.setVisible(true);//窗口是默认不可见 - 需要我们自己给
this.setSize(600, 600);//窗口宽和高
this.setLocation(650, 300);//窗口位置

/*
* 关闭窗口,程序结束
*/

this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.err.println("游戏退出!");
System.exit(0);
}
});

new PaintThread().start();//启动重画线程
addKeyListener(new KeyMonitor());//增加键盘监听

//初始化
for(int i=0;i<shells.length;i++)
{
shells[i] = new Shell();
}
}
/*
* 主函数
*/

public static void main(String[] args) throws MalformedURLException {
Mygame f = new Mygame();
f.lauchFrame();
//Scanner input = new Scanner (System.in);

File sound1 = new File ("sounds/球球大作战背景音效_爱给网_aigei_com.wav");
AudioClip sound_choose = Applet.newAudioClip(sound1.toURL());
sound_choose.play();
System.out.println("运行成功!");

//input.nextLine();

}

}

 

package 滑稽快闪;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;

public class Plane extends gameObject {

int speed=3;

boolean live = true;

public void drawSelf(Graphics g) {
if(live) {
g.drawImage(img,(int) x,(int) y, null);

if(left) {
x -= speed;
}
if(right) {
x += speed;
}
if(up) {
y -= speed;
}
if(down) {
y += speed;
}

}
}

boolean left,up,right,down;

// public void drawSelf(Graphics g) {
//
// }

//封装一下图片和xy轴坐标
public Plane(Image img ,double x,double y) {
this.img = img;
this.x = x;
this.y = y;
this.speed = 3;
this.width = img.getWidth(null);//相撞
this.height =img.getHeight(null);
}
//按下
public void addDirection(KeyEvent e) {
switch(e.getKeyCode()) {
case KeyEvent.VK_LEFT:
left = true;
break;
case KeyEvent.VK_UP:
up = true;
break;
case KeyEvent.VK_RIGHT:
right = true;
break;
case KeyEvent.VK_DOWN:
down = true;
break;
}
}
//抬起按下
public void minusaddDirection(KeyEvent e) {
switch(e.getKeyCode()) {
case KeyEvent.VK_LEFT:
left = false;
break;
case KeyEvent.VK_UP:
up = false;
break;
case KeyEvent.VK_RIGHT:
right = false;
break;
case KeyEvent.VK_DOWN:
down = false;
break;
}
}


}

 

package 滑稽快闪;

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

/*
* 子弹类
*/
public class Shell extends gameObject{

double degree;

public Shell() {
x = 200;
y = 200;
width=10;
height=10;
speed = 5;
//弧度 0 - 2PI之间
degree = Math.random()*Math.PI *2;
}

public void draw(Graphics g) {
g.setColor(Color.RED);//填充颜色

g.fillOval((int)x,(int) y, width, height);//

//角度
//三角函数
//了解x y 变换情况
x += speed*Math.cos(degree);
y += speed*Math.sin(degree);


//让球根据角度动起来

if(x<0||x>600) {
degree = Math.PI - degree;
}
if(y<0||y>700) {
degree = -degree;
}
//g.setColor(c);//填充颜色
}

}

蛮久之前的项目了,现在过来蛮久的,但是想学习的小伙伴还是可以参考参考的

包名后期你们修改一下就可以了

作者:初

QQ:3207950853

QQ邮箱:3207950853@qq.com

学习交流群:710023821

JAVA如何实现一个小游戏

标签:oca   颜色   交流   文件路径   bool   raw   owa   start   phi   

原文地址:https://www.cnblogs.com/yjhjys/p/12327550.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!