新华学院Java课程设计报告
程序代码:
package cn;
import java.awt.*; import java.io.*;
import java.util.LinkedList; import java.util.Properties; import javax.swing.JFrame; //游戏界面的主要类
public class GameFrame extends JFrame {
public GamePanel panel = new GamePanel(this); public Snake xiaoqing = new Snake(this); public Control ct = new Control(this); public Food food = new Food(this);
public LinkedList
int width = Constant.COMPANY * Constant.COMPANY_WIDTH; int height = Constant.COMPANY * Constant.COMPANY_HEIGHT; panel.setSize(width , height );
panel.setLayout(new BorderLayout()); panel.addKeyListener(ct);
this.add(panel,BorderLayout.CENTER); this.setSize(width + 5 , height + 28);
this.setLocation((1366-width)/2, (768-height)/2); this.setVisible(true); this.setTitle(\贪吃蛇\ this.setResizable(false); this.intiRocks(); this.inti();
this.addKeyListener(ct);
this.setDefaultCloseOperation(GameFrame.EXIT_ON_CLOSE); new Thread(new SnakeDriver()).start(); }
public void intiRocks(){
for(int i = 0; i < Constant.COMPANY_WIDTH; i++){
for(int j = 0; j < Constant.COMPANY_HEIGHT; j++){ // if(0 == i || 19 == i) rocks.add(new Rocks(i, j, this)); if((0 == j || 19 == j) && (0 != i || 19 != i))
8
新华学院Java课程设计报告
rocks.add(new Rocks(i, j, this)); } } }
public void inti() {
xiaoqing = new Snake(this); food = new Food(this); panel.setPause(true); panel.setScore(0); }
private Snake snake;
Properties dbconfig = new Properties(); static FileInputStream file;
public String msg() throws IOException{
file = new FileInputStream(\ dbconfig.load(file);
String url1 =dbconfig.getProperty(\ if(!xiaoqing.isLife()){
int x = panel.getScore(); if(x>Integer.parseInt(url1)){
dbconfig.setProperty(\ panel.setHigh(panel.getScore()); OutputStream out = FileOutputStream(\ dbconfig.store(out, null); } }
return url1; }
private class SnakeDriver implements Runnable { @Override
public void run() { while(true){
GameFrame.this.panel.repaint(); try {
Thread.sleep(xiaoqing.getSpeed()); } catch (InterruptedException e) { e.printStackTrace();
9
new 新华学院Java课程设计报告
} }
} }
public static void main(String[] args) { GameFrame gf = new GameFrame(); } }
//在游戏界面中画出事物的主要类 import java.awt.*; import java.io.*;
import java.util.Properties; import javax.swing.JPanel;
public class GamePanel extends JPanel{ private GameFrame gf; private Snake s; private int score; private int high;
private boolean pause;
public GamePanel(GameFrame gf) { pause = true; score = 0; this.gf = gf; }
@Override
protected void paintComponent(Graphics g) { g.setColor(Color.WHITE);
g.fillRect(0, 0, Constant.COMPANY * Constant.COMPANY_WIDTH + 20,
Constant.COMPANY * Constant.COMPANY_HEIGHT + 40); drawBackGround(g); gf.xiaoqing.move();
if (gf.xiaoqing.isLife()) {
gf.xiaoqing.eatFood(gf.food); for (Rocks rock : gf.rocks) {
if (gf.xiaoqing.eatRock(rock)) { break; }
10
新华学院Java课程设计报告
gf.xiaoqing.eatBody(); }
gf.xiaoqing.drawMe(g); gf.food.drawMe(g); if(gf.panel.isPause()) drawPause(g); }
for (Rocks y : gf.rocks) { y.drawMe(g); }
if(!gf.xiaoqing.isLife()){ gf.xiaoqing.drawMe(g); gf.food.drawMe(g); drawGameOver(g); }
drawlenth(g); drawfenshu(g); highnum(g); }
public void drawGameOver(Graphics g){ g.setColor(Color.BLUE);
g.drawString(\按f1重新开始?\* Constant.COMPANY_WIDTH/2 - 70,
Constant.COMPANY * Constant.COMPANY_HEIGHT/2); }
public void drawlenth(Graphics g){ g.setColor(Color.BLUE);
g.drawString(\ 10, Constant.COMPANY + 20); }
public void drawfenshu(Graphics g){ g.setColor(Color.BLUE);
g.drawString(\ 10, Constant.COMPANY + 40); }
public void highnum(Graphics g){ g.setColor(Color.BLUE); try {
11