自己写的贪吃蛇

package com.atuo.start;

import javax.swing.*;
import java.net.URL;

public class Data {
public static URL headerUrl = Data.class.getResource("imagess/header.png");
public static URL bodyUrl = Data.class.getResource("imagess/body.png");
public static URL upUrl = Data.class.getResource("imagess/up.png");
public static URL downUrl = Data.class.getResource("imagess/down.png");
public static URL rightUrl = Data.class.getResource("imagess/right.png");
public static URL leftUrl = Data.class.getResource("imagess/left.png");
public static URL foodUrl = Data.class.getResource("imagess/food.png");
public static ImageIcon header = new ImageIcon(headerUrl);
public static ImageIcon body = new ImageIcon(bodyUrl);
public static ImageIcon up = new ImageIcon(upUrl);
public static ImageIcon down = new ImageIcon(downUrl);
public static ImageIcon right = new ImageIcon(rightUrl);
public static ImageIcon left = new ImageIcon(leftUrl);
public static ImageIcon food = new ImageIcon(foodUrl);
}

package com.atuo.start;

import com.sun.org.apache.xpath.internal.operations.Bool;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

import static jdk.nashorn.internal.objects.NativeMath.random;

public class GamePanel extends JPanel implements KeyListener, ActionListener {

int length;
int[] snakeX = new int[600];
int[] snakeY = new int[600];
String fx;
Boolean isStart = false;
Boolean isOver = false;
int foodx;
int foody;
Random random = new Random();
//定时器
Timer timer = new Timer(100,this::actionPerformed);


//构造器
public GamePanel(){
init();
this.setFocusable(true);
this.addKeyListener(this);
timer.start();
}

//初始化
public void init(){
length = 3;
snakeX[0] = 200;snakeY[0] = 100;
snakeX[1] = 175 ;snakeY[1] = 100;
snakeX[2] = 150 ;snakeY[2] = 100;
fx = "R";
foodx = 100 + 25*random.nextInt(25);
foody = 200 + 25*random.nextInt(17);

}

//画笔
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
setBackground(Color.white);
Data.header.paintIcon(this,g,100,10);
g.fillRect(100,100,1300,500);

for (int i = 1; i < length; i++) {
Data.body.paintIcon(this,g,snakeX[i],snakeY[i]);
if (fx.equals("R")){
Data.right.paintIcon(this,g,snakeX[0],snakeY[0]);
}if (fx.equals("L")){
Data.left.paintIcon(this,g,snakeX[0],snakeY[0]);
}if (fx.equals("U")){
Data.up.paintIcon(this,g,snakeX[0],snakeY[0]);
}if (fx.equals("D")){
Data.down.paintIcon(this,g,snakeX[0],snakeY[0]);
}
}
if (isStart==false){
g.setColor(Color.white);
g.setFont(new Font("微软雅黑",Font.BOLD,40));
g.drawString("点击空格开始游戏",300,300);
}
//结束
if (isOver==true){
g.setColor(Color.red);
g.setFont(new Font("微软雅黑",Font.BOLD,40));
g.drawString("失败,点击空格重新开始",300,300);
}
Data.food.paintIcon(this,g,foodx,foody);

}

/**
* Invoked when a key has been typed.
* See the class description for {@link KeyEvent} for a definition of
* a key typed event.
*
* @param e
*/
@Override
public void keyTyped(KeyEvent e) {

}

/**
* Invoked when a key has been pressed.
* See the class description for {@link KeyEvent} for a definition of
* a key pressed event.
*
* @param e
*/
@Override
public void keyPressed(KeyEvent e) {
char keyChar = e.getKeyChar();
if (keyChar == KeyEvent.VK_SPACE){
if (isOver == false){
isStart = !isStart;
}else {
isOver = false;
init();
}
}
if (keyChar == KeyEvent.VK_W){
fx = "U";
}else if (keyChar == KeyEvent.VK_A){
fx = "L";
}else if (keyChar == KeyEvent.VK_S){
fx = "D";
}else if (keyChar == KeyEvent.VK_D){
fx = "R";
}
repaint();
}

/**
* Invoked when a key has been released.
* See the class description for {@link KeyEvent} for a definition of
* a key released event.
*
* @param e
*/
@Override
public void keyReleased(KeyEvent e) {

}

/**
* Invoked when an action occurs.
*
* @param e
*/
@Override
public void actionPerformed(ActionEvent e) {
if (isStart == true && isOver == false){
for (int i = length-1; i >0 ; i--) {
snakeX[i] = snakeX[i-1];
snakeY[i] = snakeY[i-1];
}
if (fx == "R"){
if (snakeX[0] > 1400){
snakeX[0] = 100;
}
snakeX[0] = snakeX[0] + 25;
}else if (fx == "L"){
if (snakeX[0] < 100){
snakeX[0] = 1400;
}
snakeX[0] = snakeX[0] - 25;
}else if (fx == "U"){
if (snakeY[0] < 100){
snakeY[0] = 600;
}
snakeY[0] = snakeY[0] - 25;
}else if (fx == "D"){
if (snakeY[0] > 600){
snakeY[0] = 100;
}
snakeY[0] = snakeY[0] + 25;
}
}
//吃食物
if (foodx == snakeX[0] && foody ==snakeY[0]){
length++;
foodx = 100 + 25*random.nextInt(25);
foody = 200 + 25*random.nextInt(17);
}
for (int i = 1; i < length+1; i++) {
if (snakeX[0] == snakeX[i] && snakeY[0] == snakeY[i]){
isOver = true;
}
repaint();
}
timer.start();
}
}

package com.atuo.start;

import javax.swing.*;
import java.awt.*;

public class MyStart{
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setBounds(100,100,1500,800);
frame.add(new GamePanel());

frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

 

posted @ 2022-03-27 21:33  亚托克斯的泯灭  阅读(30)  评论(0)    收藏  举报