赞
踩
目录
大家好,我是小刘!
本篇文章将向各位介绍一个Java游戏项目,本次项目可以应用与毕业设计以及简历等!!!
运行结果如下
接下来就让我们进入代码的实现吧
首先新建一个项目,然后在项目里面新建一个存放图片的文件夹,将本次项目所需要的图片存放进去,图片资源在文章最后。(按照小刘下面这个方式去创建就可以啦!!一定要在项目中去创建!!)创建包的时候也记得更改一下名字哦!
- package Game;
-
- import Game.utils.GameUtils;
-
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.KeyAdapter;
- import java.awt.event.KeyEvent;
- import java.util.ArrayList;
- import java.util.List;
-
- public class GameWin extends JFrame {
-
-
- public static int state=0;
- public int score=0;//分数
- //定义双缓存图片
- Image offScreeImage=null;
-
- //分数宽高
- int winWidth=800;
- int winHeight=600;
-
-
-
- public HeadObj headObj=new HeadObj(GameUtils.image3,60,570,this);
-
- public List<BodyObj> bodyObjList=new ArrayList<>();
- public FoodObj foodObj=new FoodObj().getFood();
- public void launch(){
- this.setVisible(true);
- this.setSize(winWidth,winHeight);
- this.setLocationRelativeTo(null);
- this.setTitle("好七蛇");
- bodyObjList.add(new BodyObj(GameUtils.imagebody,30,570,this));
- bodyObjList.add(new BodyObj(GameUtils.imagebody,0,570,this));
-
-
- //键盘事件
- this.addKeyListener(new KeyAdapter() {
- @Override
- public void keyPressed(KeyEvent e) {
- if (e.getKeyCode()==KeyEvent.VK_SPACE){
- switch (state){
- //未开始
- case 0:
- state=1;
- break;
- case 1:
- //游戏中
- state=2;
- repaint();
- break;
- case 2:
- //游戏暂停
- state=1;
- break;
- case 3:
- //失败后重新开始
- state=5;
- break;
- default:break;
- }
- }
- }
- });
- while (true){
- if (state==1) {
- repaint();
- }
- //失败重启
- if (state==5){
- state=0;
- resetGame();
- }
- try {
- //1秒1000毫秒
- Thread.sleep(200);
- } catch (InterruptedException e) {
- throw new RuntimeException(e);
- }
- }
- }
-
- @Override
- public void paint(Graphics g) {
- //双缓存图片初始化
- if (offScreeImage==null){
- offScreeImage=this.createImage(winWidth,winHeight);
- }
- Graphics gImage=offScreeImage.getGraphics();
-
- //灰色背景
- gImage.setColor(Color.gray);
- gImage.fillRect(0,0,winWidth,winHeight);
-
- gImage.setColor(Color.black);
- //横线x相同,y不同
- for (int i = 0; i <=20; i++) {
- //横线
- gImage.drawLine(0,i*30,600,i*30);
- //竖线
- gImage.drawLine(i*30,0,i*30,600);
-
- }
- for (int i =bodyObjList.size()-1;i>=0 ; i--) {
- bodyObjList.get(i).paintSelf(gImage);
-
- }
- headObj.paintSelf(gImage);
- foodObj.paintSelf(gImage);
- GameUtils.drawWork(gImage,score+" 分",Color.BLUE,50,650,300);
- gImage.setColor(Color.gray);
- prompt(gImage);
-
- g.drawImage(offScreeImage,0,0,null);
-
- }
- //绘制提示语
- void prompt(Graphics g){
- if (state==0){
- g.fillRect(120,240,400,70);
- GameUtils.drawWork(g,"按下空格健开始游戏",Color.yellow,35,150,290);
- }
- //暂停
- if (state==2){
- g.fillRect(120,240,400,70);
- GameUtils.drawWork(g,"游戏暂停,按空格继续",Color.yellow,35,150,290);
- }
- if (state==3){
- g.fillRect(120,240,400,70);
- GameUtils.drawWork(g,"游戏失败,按空格键重新开始 ",Color.red,35,150,290);
- }
- //通关
- if (state==4){
- g.fillRect(120,240,400,70);
- GameUtils.drawWork(g,"达成条件游戏通关",Color.green,35,150,290);
- }
- }
-
- //未开始
- //游戏重置
- void resetGame(){
- //关闭当前窗口
- this.dispose();
- //开启一个新窗口
- String[] args={};
- main(args);
- }
-
- public static void main(String[] args) {
- GameWin gameWin=new GameWin();
- gameWin.launch();
- }
- }
- package Game.utils;
-
- import java.awt.*;
-
- public class GameUtils {
- public static Image image=Toolkit.getDefaultToolkit().getImage("img/img.png");
- public static Image image1=Toolkit.getDefaultToolkit().getImage("img/img_1.png");
- public static Image image2=Toolkit.getDefaultToolkit().getImage("img/img_2.png");
- public static Image image3=Toolkit.getDefaultToolkit().getImage("img/img_3.png");
- public static Image imagebody=Toolkit.getDefaultToolkit().getImage("img/img_5.png");
- public static Image imagefood=Toolkit.getDefaultToolkit().getImage("img/img_4.png");
-
- //绘制文字
- public static void drawWork(Graphics g,String str,Color color,int size,int x,int y){
- g.setColor(color);
- g.setFont(new Font("仿宋",Font.BOLD,size));
- g.drawString(str,x,y);
- }
-
-
- }
- package Game;
-
- import java.awt.*;
-
- public class GameObject {
-
- //图片
- Image img;
- //坐标
- int x;
- int y;
- //宽高
- int width=30;
- int height=30;
- //窗口类的引用
- GameWin frame;
-
- public Image getImg() {
- return img;
- }
-
- public void setImg(Image img) {
- this.img = img;
- }
-
- public int getX() {
- return x;
- }
-
- public void setX(int x) {
- this.x = x;
- }
-
- public int getY() {
- return y;
- }
-
- public void setY(int y) {
- this.y = y;
- }
-
- public int getWidth() {
- return width;
- }
-
- public void setWidth(int width) {
- this.width = width;
- }
-
- public int getHeight() {
- return height;
- }
-
- public void setHeight(int height) {
- this.height = height;
- }
-
- public GameWin getFrame() {
- return frame;
- }
-
- public void setFrame(GameWin frame) {
- this.frame = frame;
- }
-
- public GameObject(){
- }
-
- public GameObject(Image img, int x, int y, GameWin frame) {
- this.img = img;
- this.x = x;
- this.y = y;
- this.frame = frame;
- }
-
- public GameObject(Image img, int x, int y, int width, int height, GameWin frame) {
- this.img = img;
- this.x = x;
- this.y = y;
- this.width = width;
- this.height = height;
- this.frame = frame;
- }
-
- //绘制自身
- public void paintSelf(Graphics g){
- g.drawImage(img,x,y,null);
- }
- }
- package Game;
-
- import java.awt.*;
-
- public class BodyObj extends GameObject{
- public BodyObj(Image image,int x,int y,GameWin frame){
- super(image,x,y,frame);
- }
-
- @Override
- public void paintSelf(Graphics g) {
- super.paintSelf(g);
- }
- }
- package Game;
-
- import Game.utils.GameUtils;
-
- import java.awt.*;
- import java.util.Random;
-
- public class FoodObj extends GameObject{
-
- //随机函数
- Random r=new Random();
- public FoodObj(){super();}
-
- public FoodObj(Image image,int x,int y,GameWin frame){
- super(image,x,y,frame);
- }
- //获取食物
- public FoodObj getFood(){
- return new FoodObj(GameUtils.imagefood,r.nextInt(20)*30,(r.nextInt(19) +1)*30,this.frame);
- }
-
- @Override
- public void paintSelf(Graphics g) {
- super.paintSelf(g);
- }
- }
- package Game;
-
- import Game.utils.GameUtils;
-
- import java.awt.*;
- import java.awt.event.KeyAdapter;
- import java.awt.event.KeyEvent;
- import java.util.List;
-
- public class HeadObj extends GameObject{
- //方向 up down left right
- private String direction="right";
-
- public String getDirection() {
- return direction;
- }
-
- public void setDirection(String direction) {
- this.direction = direction;
- }
-
- public void move(){
- //蛇身移动
- java.util.List<BodyObj> bodyObjList=this.frame.bodyObjList;
- for (int i = bodyObjList.size()-1;i>=1;i--) {
- bodyObjList.get(i).x=bodyObjList.get(i-1).x;
- bodyObjList.get(i).y=bodyObjList.get(i-1).y;
- //蛇头与身体的碰撞判断
- if (this.x==bodyObjList.get(i).x&&this.y==bodyObjList.get(i).y){
- //失败
- GameWin.state=3;
- }
-
- }
- bodyObjList.get(0).x=this.x;
- bodyObjList.get(0).y=this.y;
-
- //蛇头移动
- switch (direction){
- case "up":
- y-=height;
- break;
- case "down":
- y+=height;
- break;
- case "left":
- x-=width;
- break;
- case "right":
- x+=width;
- break;
- }
- }
- public HeadObj(Image img, int x, int y, GameWin frame) {
- super(img, x, y, frame);
- this.frame.addKeyListener(new KeyAdapter() {
- @Override
- public void keyPressed(KeyEvent e) {
- changeDirection(e);
- }
- });
- }
-
- public void changeDirection(KeyEvent e){
- switch (e.getKeyCode()){
- case KeyEvent.VK_A:
- if (!"right".equals(direction)){
- direction="left";
- img= GameUtils.image2;
- }
- break;
- case KeyEvent.VK_D:
- if (!"left".equals(direction)){
- direction="right";
- img= GameUtils.image3;
- }
- break;
- case KeyEvent.VK_W:
- if (!"down".equals(direction)){
- direction="up";
- img= GameUtils.image;
- }
- break;
- case KeyEvent.VK_S:
- if (!"up".equals(direction)){
- direction="down";
- img= GameUtils.image1;
- }
- break;
- default:
- break;
- }
- }
-
- @Override
- public void paintSelf(Graphics g) {
- super.paintSelf(g);
- //蛇吃食物
- FoodObj food=this.frame.foodObj;
-
- Integer newX=null;
- Integer newY=null;
- if (this.x==food.x&&this.y==food.y){
- this.frame.foodObj=food.getFood();
- BodyObj lastBody=this.frame.bodyObjList.get(this.frame.bodyObjList.size()-1);
- newX= lastBody.x;
- newY= lastBody.y;
- //分数+1
- this.frame.score++;
-
-
- }
- //通关判断
- if (this.frame.score>=15){
- //通过
- GameWin.state=4;
- }
- move();
- if (newX!=null&& newY!=null){
- this.frame.bodyObjList.add(new BodyObj(GameUtils.imagebody,newX,newY,this.frame));
- }
-
- //越界
- if (x<0){
- x=570;
- }else if (x>570){
- x=0;
- }else if (y<30){
- y=570;
- } else if (y>570) {
- y=30;
- }
- }
- }
最后希望大家多多点赞支持小刘
img.png img_1.png-----img_5png
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。