贪吃蛇小游戏
ConfigSnakeFrameSnakePanelButtonPanelFoodMyTextNodeSnake
未完
Config
package com
.hpe
.config
;
public class Config {
public static final int ROWS
= 22;
public static final int COLS
= 35;
public static final int SPAN
= 20;
public static final String U
= "u";
public static final String D
= "d";
public static final String L
= "l";
public static final String R
= "r";
public static boolean islive
= true;
public static int speed
= 400;
public static boolean valid
= false;
public static boolean isgone
= true;
public static int score
= 0;
public static void reload() {
islive
= true;
valid
= false;
score
= 0;
speed
= 400;
isgone
= true;
}
}
SnakeFrame
package com
.hpe
.frame
;
import javax
.swing
.JFrame
;
import com
.hpe
.panel
.ButtonPanel
;
import com
.hpe
.panel
.SnakePanel
;
public class SnakeFrame extends JFrame {
private SnakePanel snakePanel
= new SnakePanel();
private ButtonPanel buttonPanel
= new ButtonPanel(snakePanel
);
public SnakeFrame() {
initFrame();
addComponent();
this.setVisible(true);
}
private void initFrame() {
this.setTitle("贪吃蛇");
this.setBounds(70, 40, 706, 500);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame
.EXIT_ON_CLOSE
);
this.setLayout(null
);
this.snakePanel
.setFocusable(true);
this.snakePanel
.requestFocus();
}
private void addComponent() {
this.add(snakePanel
);
this.add(buttonPanel
);
}
public static void main(String
[] args
) {
new SnakeFrame();
}
}
SnakePanel
package com
.hpe
.panel
;
import java
.awt
.Color
;
import java
.awt
.Graphics
;
import java
.awt
.event
.KeyEvent
;
import java
.awt
.event
.KeyListener
;
import javax
.swing
.JPanel
;
import com
.hpe
.config
.Config
;
import com
.hpe
.po
.Food
;
import com
.hpe
.po
.Snake
;
public class SnakePanel extends JPanel implements KeyListener{
private Food food
= new Food();
private Snake snake
= new Snake(food
);
private SnakeThread snakeThread
= new SnakeThread();
public SnakePanel() {
initPanel();
addComponent();
snakeThread
.start();
}
private void initPanel() {
this.setSize(700, 440);
this.setBackground(Color
.GREEN
);
this.addKeyListener(this);
}
@Override
public void paint(Graphics g
) {
super.paint(g
);
g
.setColor(Color
.GRAY
);
for(int i
= 0; i
< Config
.ROWS
; i
++) {
g
.drawLine(0, Config
.SPAN
* i
, Config
.SPAN
* Config
.COLS
, Config
.SPAN
* i
);
}
for(int i
= 0; i
< Config
.COLS
; i
++) {
g
.drawLine(Config
.SPAN
* i
, 0, Config
.SPAN
* i
, Config
.SPAN
* Config
.ROWS
);
}
food
.draw(g
);
snake
.draw(g
);
snake
.move();
snake
.eatOrNot();
snake
.deadCheck();
}
private void addComponent() {
}
class SnakeThread extends Thread {
@Override
public void run() {
super.run();
while(Config
.islive
) {
try {
Thread
.sleep(300);
} catch (InterruptedException e
) {
e
.printStackTrace();
}
if(Config
.isgone
){
repaint();
}
}
}
}
@Override
public void keyTyped(KeyEvent e
) {
}
@Override
public void keyPressed(KeyEvent e
) {
this.snake
.dirControl(e
);
}
@Override
public void keyReleased(KeyEvent e
) {
}
}
ButtonPanel
package com
.hpe
.panel
;
import java
.awt
.event
.ActionEvent
;
import java
.awt
.event
.ActionListener
;
import javax
.swing
.JButton
;
import javax
.swing
.JPanel
;
import com
.hpe
.config
.Config
;
public class ButtonPanel extends JPanel implements ActionListener {
private JButton btn_pause
= new JButton("暂停游戏");
private JButton btn_continue
= new JButton("继续游戏");
private JButton btn_restart
= new JButton("重新开始");
private JButton btn_rank
= new JButton("游戏排行");
private SnakePanel snakePanel
;
public ButtonPanel(SnakePanel snakePanel
) {
this.snakePanel
= snakePanel
;
init();
addComponent();
}
public void init() {
this.setBounds(0, 440, 706, 60);
btn_continue
.addActionListener(this);
btn_pause
.addActionListener(this);
btn_restart
.addActionListener(this);
btn_rank
.addActionListener(this);
}
public void addComponent() {
this.add(btn_pause
);
this.add(btn_continue
);
this.add(btn_restart
);
this.add(btn_rank
);
}
@Override
public void actionPerformed(ActionEvent e
) {
if(e
.getSource() == btn_pause
) {
pauseGame();
}
if(e
.getSource() == btn_continue
) {
continueGame();
}
}
public void pauseGame() {
Config
.isgone
= false;
}
public void continueGame() {
Config
.isgone
= true;
this.snakePanel
.setFocusable(true);
this.snakePanel
.requestFocus();
}
}
Food
package com
.hpe
.po
;
import java
.awt
.Color
;
import java
.awt
.Graphics
;
import java
.awt
.Rectangle
;
import java
.util
.Random
;
import com
.hpe
.config
.Config
;
public class Food {
private int col
;
private int row
;
public Food(){
this.reGen();
}
public void reGen(){
this.col
= new Random().nextInt(Config
.COLS
);
this.row
= new Random().nextInt(Config
.ROWS
);
}
public void draw(Graphics g
) {
g
.setColor(Color
.RED
);
g
.fillRect(col
*Config
.SPAN
, row
*Config
.SPAN
, Config
.SPAN
, Config
.SPAN
);
}
public Rectangle
getFoodRectangle(){
return new Rectangle(
this.col
*Config
.SPAN
,
this.row
*Config
.SPAN
,
Config
.SPAN
,
Config
.SPAN
);
}
}
MyText
package com
.hpe
.po
;
import java
.util
.Random
;
public class MyTest {
public static void main(String
[] args
) {
for(int i
= 0; i
< 5; i
++) {
System
.out
.println((int)(Math
.random() * 10) + 1);
}
System
.out
.println("------------------------------------------------------------");
for(int i
= 0; i
< 50; i
++) {
System
.out
.println((int)(Math
.random() * 4) + 1);
}
System
.out
.println("------------------------------------------------------------");
for(int i
= 0; i
< 60; i
++) {
System
.out
.println((int)(Math
.random() * 5) + 2);
}
System
.out
.println("*************************************************************");
Random rd
= new Random();
for(int i
= 0; i
< 10; i
++) {
System
.out
.println(rd
.nextInt(10));
}
}
}
Node
package com
.hpe
.po
;
import java
.awt
.Color
;
import java
.awt
.Graphics
;
import com
.hpe
.config
.Config
;
public class Node {
private int col
;
private int row
;
private String dir
;
public Node pre
;
public Node next
;
public Node(int col
, int row
, String dir
) {
super();
this.col
= col
;
this.row
= row
;
this.dir
= dir
;
}
public int getCol() {
return col
;
}
public void setCol(int col
) {
this.col
= col
;
}
public int getRow() {
return row
;
}
public void setRow(int row
) {
this.row
= row
;
}
public String
getDir() {
return dir
;
}
public void setDir(String dir
) {
this.dir
= dir
;
}
public void draw(Graphics g
) {
if(this.pre
== null
) {
g
.setColor(Color
.YELLOW
);
} else {
g
.setColor(Color
.BLUE
);
}
g
.fillOval(col
* Config
.SPAN
, row
* Config
.SPAN
, Config
.SPAN
, Config
.SPAN
);
}
}
Snake
package com
.hpe
.po
;
import java
.awt
.Graphics
;
import java
.awt
.Rectangle
;
import java
.awt
.event
.KeyEvent
;
import com
.hpe
.config
.Config
;
public class Snake {
private Node head
;
private Node body
;
private Node tail
;
private Food food
;
public Snake(Food food
) {
this.food
= food
;
head
= new Node(17, 11, Config
.R
);
body
= new Node(16, 11, Config
.R
);
tail
= new Node(15, 11, Config
.R
);
head
.pre
= null
;
head
.next
= body
;
body
.pre
= head
;
body
.next
= tail
;
tail
.pre
= body
;
tail
.next
= null
;
}
public void draw(Graphics g
) {
for(Node n
= head
; n
!= null
; n
= n
.next
) {
n
.draw(g
);
}
}
public void move() {
addHead();
removeTail();
}
public void addHead() {
Node node
= null
;
switch(this.head
.getDir()) {
case Config
.U
:
node
= new Node(this.head
.getCol(), this.head
.getRow() - 1, this.head
.getDir());
break;
case Config
.D
:
node
= new Node(this.head
.getCol(), this.head
.getRow() + 1, this.head
.getDir());
break;
case Config
.L
:
node
= new Node(this.head
.getCol() - 1, this.head
.getRow(), this.head
.getDir());
break;
case Config
.R
:
node
= new Node(this.head
.getCol() + 1, this.head
.getRow(), this.head
.getDir());
break;
}
node
.pre
= null
;
node
.next
= head
;
head
.pre
= node
;
head
= node
;
}
public void removeTail() {
Node temp
= tail
;
tail
= tail
.pre
;
tail
.next
= null
;
temp
.pre
= null
;
}
public void dirControl(KeyEvent e
) {
switch(e
.getKeyCode()){
case KeyEvent
.VK_UP
:
if(!this.head
.getDir().equals(Config
.D
)){
this.head
.setDir(Config
.U
);
}
break;
case KeyEvent
.VK_DOWN
:
if(!this.head
.getDir().equals(Config
.U
)){
this.head
.setDir(Config
.D
);
}
break;
case KeyEvent
.VK_LEFT
:
if(!this.head
.getDir().equals(Config
.R
)){
this.head
.setDir(Config
.L
);
}
break;
case KeyEvent
.VK_RIGHT
:
if(!this.head
.getDir().equals(Config
.L
)){
this.head
.setDir(Config
.R
);
}
break;
default:
break;
}
}
public void eatOrNot() {
if(this.getHeadRectangle().contains(this.food
.getFoodRectangle())){
this.addHead();
this.food
.reGen();
}
}
public Rectangle
getHeadRectangle(){
return new Rectangle(
this.head
.getCol()*Config
.SPAN
,
this.head
.getRow()*Config
.SPAN
,
Config
.SPAN
,
Config
.SPAN
);
}
public void deadCheck() {
if(this.head
.getCol()<0||
this.head
.getCol()>Config
.COLS
||
this.head
.getRow()<0||
this.head
.getRow()>Config
.ROWS
){
Config
.islive
=false;
return;
}
for(Node node
=head
.next
;node
!=null
;node
= node
.next
){
Rectangle nodeRect
=new Rectangle(
node
.getCol()*Config
.SPAN
,
node
.getRow()*Config
.SPAN
,
Config
.SPAN
,
Config
.SPAN
);
if(this.getHeadRectangle().contains(nodeRect
)){
Config
.islive
=false;
return;
}
}
}
}
未完
学习笔记
转载请注明原文地址:https://tech.qufami.com/read-28830.html