JAVA如何實現一個小游戲


話不多說上代碼

 

 

 

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM