当前位置:网站首页>Snake games
Snake games
2022-07-25 10:16:00 【Look at the bugs】
The main class
package GluttonousSnake;
import javax.swing.*;
// The main startup class of the game
public class StartGame {
public static void main(String[] args) {
JFrame frame =new JFrame();
frame.add(new GamePanel());
frame.setVisible(true);
frame.setResizable(false); // Window size is immutable
frame.setBounds(10,10,920,720);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
}
The main content of the game
1, Defining data
2, Initialization data
3, Draw it with a brush
4, Event monitoring , Keyboard monitor
package GluttonousSnake;
import javafx.scene.input.KeyCode;
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;
// Game panels
public class GamePanel extends JPanel implements KeyListener , ActionListener {
// Drawing panels
Container container=new Container();
// The current state of the game : Start , stop it
boolean start=false; // The default is not to start
boolean Fail=false;// Game failure state
// Timer In Milliseconds
Timer timer=new Timer(100,this);//100ms Do it once
// Constructors
public GamePanel() {
init();
// Get focus and keyboard events
this.setFocusable(true);// Get focus event
this.addKeyListener(this); // Get keyboard monitoring events
timer.start();// The timer starts when the game starts
}
int length; // The length of the snake
int[] snakeX=new int[600]; // Snake x coordinate 25*25
int[] snakeY=new int[500]; // Snake y coordinate
String fx;
// The coordinates of the food
int foodX;
int foodY;
Random random=new Random();
int score;// achievement
// Initialization method
public void init(){
length=3;
snakeX[0]=100;snakeY[0]=100; // Brain coordinates
snakeX[1]=70;snakeY[1]=100; // The first body coordinate
snakeX[2]=45;snakeY[2]=100; // The second body coordinate
fx="R";// The initial direction is right
// Distribute things randomly on the interface
foodX=25+25*random.nextInt(34);
foodY=75+25*random.nextInt(24);
score=0;
}
// Everything in the game is painted by this
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g); // Clear the screen
// Drawing static panels
this.setBackground(Color.white);
Data.header.paintIcon(this,g,25,11);// Head billboard
g.fillRect(25,75,850,600); // Default game interface
// Draw points
g.setColor(Color.WHITE);
g.setFont(new Font(" Microsoft YaHei ",Font.BOLD,18));// Set the font
g.drawString(" length "+length,750,35);
g.drawString(" fraction "+score,750,50);
// Painting food
Data.food.paintIcon(this,g,foodX,foodY);
// Draw the snake
if(fx.equals("R")){
Data.d.paintIcon(this,g,snakeX[0],snakeY[0]); // Snake head , We need to judge by direction
}else if(fx.equals("L")){
Data.a.paintIcon(this,g,snakeX[0],snakeY[0]); // Snake head
}else if(fx.equals("U")){
Data.w.paintIcon(this,g,snakeX[0],snakeY[0]); // Snake head initialization up
}else if(fx.equals("D")){
Data.s.paintIcon(this,g,snakeX[0],snakeY[0]); // Snake head initialization down
}
for (int i = 1; i < length; i++) {
Data.q.paintIcon(this,g,snakeX[i],snakeY[i]); // The first body coordinate
}
// Game status
if(start==false){
g.setColor(Color.white);
g.setFont(new Font(" Microsoft YaHei ",Font.BOLD,40));// Set the font
g.drawString(" Press the space to start the game ",300,300);
}
if(Fail){
g.setColor(Color.RED);
g.setFont(new Font(" Microsoft YaHei ",Font.BOLD,40));// Set the font
g.drawString(" The game failed , Press the space to start over ",300,300);
}
}
@Override
public void keyTyped(KeyEvent e) {
}
// Keyboard monitoring events
@Override
public void keyPressed(KeyEvent e) {
int keyCode = e.getKeyCode();// Which one to get the keys of the keyboard
if(keyCode==KeyEvent.VK_SPACE){
// If you press the space-time grid key
if(Fail){
// restart
Fail=false;
init();
}else{
start=!start;
}
repaint();
}
// Snake moving
if(keyCode==KeyEvent.VK_UP&&fx!="D"){
fx="U";
}else if(keyCode==KeyEvent.VK_DOWN&&fx!="U"){
fx="D";
}else if(keyCode==KeyEvent.VK_LEFT&&fx!="R"){
fx="L";
}else if(keyCode==KeyEvent.VK_RIGHT&&fx!="L"){
fx="R";
}
}
// Event monitoring --- Need to refresh through fixed events ,1s--10 Time
@Override
public void actionPerformed(ActionEvent e) {
if(start&&Fail==false){
// If the game is starting , Let the snake move
// Eat food
if(snakeX[0]==foodX&&snakeY[0]==foodY){
length++;// length +1
score=score+10; // Add... For every score you eat 10
// Regenerate food
foodX=25+25*random.nextInt(34);
foodY=75+25*random.nextInt(24);
}
// Move right
for (int i = length-1; i >0; i--) {
snakeX[i]=snakeX[i-1];// Move one... Forward
snakeY[i]=snakeY[i-1];// The next section moves to the position of the previous one
}
// trend
if(fx.equals("R")){
snakeX[0]=snakeX[0]+25;
// Boundary judgment
if(snakeX[0]>850){
snakeX[0]=25;
}
}else if(fx.equals("L")){
snakeX[0]=snakeX[0]-25;
// Boundary judgment
if(snakeX[0]<25){
snakeX[0]=850;
}
}else if(fx.equals("U")){
snakeY[0]=snakeY[0]-25;
if(snakeY[0]<75){
snakeY[0]=650;
}
}else if(fx.equals("D")){
snakeY[0]=snakeY[0]+25;
if(snakeY[0]>650){
snakeY[0]=75;
}
}
// Failure judgment , Hit yourself
for (int i = 1; i < length; i++) {
if(snakeX[0]==snakeX[i]&&snakeY[0]==snakeY[i]){
Fail=true;
}
}
repaint();// Redraw the interface
}
timer.start();// The timer is on
}
@Override
public void keyReleased(KeyEvent e) {
}
}
Data class
package GluttonousSnake;
import javax.swing.*;
import java.net.URL;
// Data Center
public class Data {
// Relative paths :tx.jpg
// Absolute path :/fj.jpg / Equivalent to the current directory
private static URL headerURL=Data.class.getResource("header.jpg");
public static ImageIcon header=new ImageIcon(headerURL);
private static URL UpURL=Data.class.getResource("Up.jpg");
public static ImageIcon w=new ImageIcon(UpURL);
private static URL downURL=Data.class.getResource("Down.jpg");
public static ImageIcon s=new ImageIcon(downURL);
private static URL leftURL=Data.class.getResource("Left.jpg");
public static ImageIcon a=new ImageIcon(leftURL);
private static URL rightURL=Data.class.getResource("Right.jpg");
public static ImageIcon d=new ImageIcon(rightURL);
private static URL bodyURL=Data.class.getResource("body.png");
public static ImageIcon q=new ImageIcon(bodyURL);
private static URL foodURL=Data.class.getResource("food.jpg");
public static ImageIcon food=new ImageIcon(foodURL);
}
边栏推荐
猜你喜欢

CentOs安装redis

VS Code 连接远程 Jupyter 服务器

Use of dictionary tree

Trojaning Attack on Neural Networks 论文阅读笔记

Configuring ROS development environment with vscode: Causes and solutions to the problem of ineffective code modification

JS uses requestanimationframe to detect the FPS frame rate of the current animation in real time

VLAN的配置及其应用(以华为eNSP为例)

Reflection 反射

mysql 解决不支持中文的问题

Rest使用与原理
随机推荐
Summary of most consistency problems
oh-my-zsh和tmux配置(个人)
JSP details
UE4 碰撞(Collsion)
常用类的小知识
@Import,Conditional和@ImportResourse注解
Round to the nearest
鼠标监听,画笔
UE4 快速找到打包失败的原因
UE4 外部打开exe文件
21. Merge Two Sorted Lists
数论--约数研究
GUI窗口
strut2 表单标签
Exception handling exception
UE4 框架介绍
UE4 窗口控制(最大化 最小化)
基础背包问题
NPM详解
Filter过滤器详解(监听器以及它们的应用)