刚刚接触java的文本框绘图的知识点,然后就可以按照老师的提醒做一些简单的游戏,对JFrame加深一下,下面就贪吃蛇给出一些源代码,其实正真的实现的代码并不多,只是稍微处理一下就可以明白,下面代码均有注释!
第一个类shake类
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
public class Snake {
public int w;//半径
public int x , y;//球心坐标
Color co;//颜色
public int dir = 4;
public Snake(int w,int x , int y ,Color col){
this.w = w;
this.x = x;
this.y = y;
this.co = col;
}
public void draw(Graphics g){
g.setColor(co);//设置填充的颜色
g.fillArc(SnakeGame.OFF_W+x*w, SnakeGame.OFF_H+y*w, w, w,0,360);//设置矩形,4个参数,xy坐标,高跟宽
}
}
第二个类SnakeGame
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.*;
import javax.swing.JFrame;
public class SnakeGame extends JFrame{
public final static int SCREEN_W = 800,SCREEN_H = 600;//设置屏幕大小
public final static int OFF_W = 200,OFF_H = 100;//设置编辑框距屏幕边缘的宽高
int key;//记录上下左右键的KeyCode
boolean isPress;//判断是否按下按键
Graphics offg;//画笔
Image img;//画布
ArrayList<Snake> Snakes = new ArrayList<Snake>();//储存蛇的容器
private static final SnakeGame g = new SnakeGame();
public static SnakeGame get_Game(){
return g;
}
private SnakeGame(){
setBounds(100,100,SCREEN_W,SCREEN_H);//设置屏幕大小以及起始坐标
setTitle("SnakeGame");//设置标题
setVisible(true);//设置时是否显示
setDefaultCloseOperation(EXIT_ON_CLOSE);//设置小红叉
addKeyListener(new KeyAdapter() {//键盘监听
@Override
public void keyPressed(KeyEvent e) {//按键盘事件
// TODO Auto-generated method stub
isPress = true;
key = e.getKeyCode();
}
@Override
public void keyReleased(KeyEvent e) {//松键盘事件
// TODO Auto-generated method stub
isPress = false;
key = -1;
}
});
}
public int snakew = (SCREEN_W - 2*OFF_W)/40;//设置蛇的一格的大小
Snake food;//食物
Snake head;//蛇头
public Random rd = new Random();
public void paint(Graphics g){
if(img == null){
img = createImage(SCREEN_W,SCREEN_H);
offg = img.getGraphics();
int xx = rd.nextInt((SCREEN_W - 2*OFF_W)/snakew);
int yy = rd.nextInt((SCREEN_W - 2*OFF_W)/snakew);
food = new Snake(snakew, xx, yy, Color.red);//随机产生食物
head = new Snake(snakew, 0, 0, Color.yellow);
Snakes.add(head);//添加蛇头
}
offg.setColor(Color.white);
offg.fillRect(0, 0, SCREEN_W, SCREEN_H);
offg.setColor(Color.black);
offg.fillRect(OFF_W,OFF_H,(SCREEN_W - 2*OFF_W),(SCREEN_H - 2*OFF_H));
//判断按键按下去生效
if(isPress){
Snake head = Snakes.get(0);//获得蛇头
switch(key){//蛇头方向变,上下左右
case 38:
if(head.dir != 2){
head.dir = 1;
}
break;
case 40:
if(head.dir != 1){
head.dir = 2;
}
break;
case 37:
if(head.dir != 4){
head.dir = 3;
}
break;
case 39:
if(head.dir != 3){
head.dir = 4;
}
break;
}
}
Snake head = Snakes.get(0);
//吃到食物后,在随机产生一个食物
if(head.x == food.x && head.y == food.y){
food.x = rd.nextInt((SCREEN_W - 2*OFF_W)/snakew);
food.y = rd.nextInt((SCREEN_W - 2*OFF_W)/snakew);
Snakes.add(new Snake(snakew, head.x, head.y, Color.blue));
}//先蛇身再蛇头
for (int i = Snakes.size()-1; i > 0 ; i--) {
Snakes.get(i).x = Snakes.get(i-1).x;
Snakes.get(i).y = Snakes.get(i-1).y;
}
switch(head.dir){
case 1:
head.y--;
break;
case 2:
head.y++;
break;
case 3:
head.x--;
break;
case 4:
head.x++;
break;
}
if(head.x > 39){
head.x = 0;
}else if(head.x < 0){
head.x = 39;
}
if(head.y > 39){
head.y = 0;
}else if(head.y < 0){
head.y = 39;
}
food.draw(offg);
for (int i = 0; i < Snakes.size(); i++) {
Snakes.get(i).draw(offg);
}
g.drawImage(img,0,0,null);
repaint();//重绘
try {
Thread.sleep(150);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
第三个类测试类MainTest
public class MainTest {
public static void main(String[] args) {
SnakeGame g = SnakeGame.get_Game();
}
}