用java实现的简易贪吃蛇游戏
程序员文章站
2022-04-07 18:04:51
1.封装贪吃蛇身体,设置贪吃蛇结点类,结点用ArrayList存储import java.awt.*;public class Node { private int x; private int y; public Node(int x, int y) { this.x = x; this.y = y; } public Node(){ } public int getX() { return...
1.封装贪吃蛇身体,抽象出贪吃蛇结点类Node,结点用ArrayList存储
import java.awt.*;
public class Node {
private int x;
private int y;
public Node(int x, int y) {
this.x = x;
this.y = y;
}
public Node(){
}
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 void drawNode(int i, Graphics g){
if(i==0){//头绘制成圆
g.fillOval(this.x,this.y,20-1,20-1);
}else{//身体绘制成矩形
g.fillRect(this.x,this.y,20-1,20-1);
}
}
}
2.主类
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
public class Snake extends Frame implements KeyListener , ActionListener {
//初始蛇移动方向
String fx="right";
//碰撞判断
boolean is_attack=false;
private ArrayList<Node> body=new ArrayList<Node>();
{
body.add(new Node(160, 60));
body.add(new Node(140, 60));
body.add(new Node(120, 60));
int x=(int)(Math.random()*580);
int y=(int)(Math.random()*580);
body.add(new Node(x-x%20,y-y%20));
}
/**
* 贪吃蛇
* 1、窗体
* 位置(坐标) * 标题 * 大小 * 背景颜色 * 窗体的大小不可变 * 可见
* 2、绘制蛇
*/
public Snake(){
//窗体标题
super.setTitle("贪吃蛇游戏");
//窗体位置
super.setLocation(100,100);
//窗体大小
super.setSize(600,600);
//背景颜色
super.setBackground(new Color(252,255, 208));
//设置窗体可见
super.setVisible(true);
// 窗体大小不可变
super.setResizable(false);
//给关闭按钮添加事件
super.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
//获取聚焦
super.setFocusable(true);
//添加键盘监听
super.addKeyListener(this);
start();
}
public void start(){
while(!is_attack){
try{
Thread.sleep(200);
}catch (Exception e){
e.printStackTrace();
}
//碰撞检测
attack_check();
//刷新窗体,调用paint方法
repaint();
}
}
//碰撞检测
public void attack_check(){
//撞到身体检测
for(int i=1;i<body.size()-1;i++){
if (body.get(0).getX()==body.get(i).getX()&&body.get(0).getY()==body.get(i).getY()){
is_attack=true;
}
}
//撞到边界
if(body.get(0).getX()==0||body.get(0).getX()==600||body.get(0).getY()==0||body.get(0).getY()==600){
is_attack=true;
}
}
//移动贪吃蛇
public void move(){
//吃到食物判断,头结点坐标和食物坐标相等
if ((body.get(0).getX()==body.get(body.size()-1).getX())&&(body.get(0).getY()==body.get(body.size()-1).getY())) {
//出现下一个食物
int x=(int)(Math.random() * 580);
int y=(int)(Math.random() * 580);
body.get(body.size()-1).setX(x-x%20);
body.get(body.size()-1).setY(y-y%20);
//蛇身加一个结点
body.add(body.size()-1,new Node(0,0));
}
//移动头尾节点,使贪吃蛇移动
Node node=body.get(0);
if (fx.equals("up")){
node=new Node(node.getX(),node.getY()-20);
//加头
body.add(0,node);
//去尾
body.remove(body.size()-2);
}if (fx.equals("down")){
node=new Node(node.getX(),node.getY()+20);
//加头
body.add(0,node);
//去尾
body.remove(body.size()-2);
}if (fx.equals("right")){
node=new Node(node.getX()+20,node.getY());
//加头
body.add(0,node);
//去尾
body.remove(body.size()-2);
}if (fx.equals("left")){
node=new Node(node.getX()-20,node.getY());
//加头
body.add(0,node);
//去尾
body.remove(body.size()-2);
}
}
/**
* 展示窗体|每一次刷新窗体都会执行的方法
* @param g 画笔
*/
@Override
public void paint(Graphics g) {
g.setColor(new Color(105, 78,255));
for(int i=0; i<body.size(); i++){
body.get(i).drawNode(i,g);
}
move();
//游戏结束绘制
if(is_attack){
g.setColor(new Color(255, 67, 40));
Font ft = new Font("宋体", Font.PLAIN, 40);
g.setFont(ft);
g.drawString("很遗憾,游戏结束",150,300);
}
}
//事件监听
@Override
public void actionPerformed(ActionEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
int keyCode=e.getKeyCode();
if(keyCode==KeyEvent.VK_UP){
fx="up";
}if (keyCode==KeyEvent.VK_DOWN){
fx="down";
}if (keyCode==KeyEvent.VK_LEFT){
fx="left";
}if (keyCode==KeyEvent.VK_RIGHT){
fx="right";
}
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
/**
* main方法
* @param args
*/
public static void main(String[] args) {
new Snake();
}
}
运行图片
游戏结束
本文地址:https://blog.csdn.net/qq_44047446/article/details/111892120