java吃豆子游戏设计import java.awt.*;
/** * 定义敌人的类 */public class Enemy{ private int xPos; private int yPos; private int xVPos; private int yVPos; private int direction; //敌人是否失去反抗能力 private boolean alarm; //敌人失去防抗能力的时间 private int alarmTime; private boolean ghostMode; private Rectangle Enemy; private int speed; private int interval; private int Colour; private int AI_time1, AI_time2; private int lefttime, uptime, righttime, downtime; private int gameFlow; private boolean ghost; private boolean canMoveUp, canMoveDown, canMoveLeft,canMoveRight; private boolean cantGoAnyWhere; private int prisonTime; private boolean stop; /** **根据x、y、颜色、是否已经出牢笼、剩余多长时间才可出牢笼 **构造敌人对象 */ public Enemy(int a, int b, int c, boolean cantGoAnyWhere, int prisonTime){ xPos = a; yPos = b; xVPos = xPos; yVPos = yVPos; direction = 3; speed = 3; interval = 63 + 21*((int)(Math.random()*10)); AI_time1 = 0; Colour = c; this.prisonTime = prisonTime; this.cantGoAnyWhere = cantGoAnyWhere; Enemy = new Rectangle(xPos -10, yPos - 10, 21, 21); }
/** **方法:敌人的移动 */ public void move(int a, int b, Wall[] wall){ AI_time1++; AI_time2++; uptime++; downtime++; lefttime++; righttime++; gameFlow++; //如果敌人没有反抗能力 if(alarm) alarmTime++; boolean changeMovement = false;
//如果敌人不能移动,就将囚禁得时间不停增加 if(cantGoAnyWhere) prisonTime++;
//当敌人失去反抗能力后,速度降低 if(alarm) speed = 1; else speed = 2;
//失去反抗能力到指定时间后,恢复原态 if(alarmTime >= 330){ alarmTime = 0; alarm = false; }
canMoveUp = true; canMoveDown = true; canMoveLeft = true; canMoveRight = true;
Rectangle R; Rectangle UP = new Rectangle(xVPos - 10, yVPos - 10 - speed, 21, 21); Rectangle DOWN = new Rectangle(xVPos - 10, yVPos - 10 + speed, 21, 21); Rectangle LEFT = new Rectangle(xVPos - 10 - speed, yVPos - 10, 21, 21); Rectangle RIGHT = new Rectangle(xVPos - 10 + speed, yVPos - 10, 21, 21);
for(int i = 0; i < wall.length; i++){ R = new Rectangle(wall[i].getxPos() - 10, wall[i].getyPos() - 10, 21, 21); if(R.intersects(UP)) canMoveUp = false; if(prisonTime < 231){ //确定此 Rectangle R是否与指定 Rectangle UP 相交。如果两个矩形的交集为非空,则它们是相交的。 //如果R与UP相交,则返回 true;否则返回 false。 if(R.intersects(UP) && direction == 0){ yPos = yVPos; changeMovement = true; } }
if(!(ghost && (yVPos >= 189 && yVPos < 231 && xVPos == 210))){ if(R.intersects(DOWN)) canMoveDown = false; if(R.intersects(DOWN) && direction == 1){ yPos = yVPos; changeMovement = true; } }
if(R.intersects(LEFT)) canMoveLeft = false; if(R.intersects(LEFT) && direction == 2){ xPos = xVPos; changeMovement = true; }
if(R.intersects(RIGHT)) canMoveRight = false; if(R.intersects(RIGHT) && direction == 3){ xPos = xVPos; changeMovement = true; } }
interval-=speed; if(interval <=0) changeMovement = true;
int u = (int)(Math.random()*4); if(canMoveUp && direction != 1 && AI_time1 > 10 && u == 0){ direction = 0; xPos = xVPos; AI_time1 = 0; } if(canMoveDown && direction != 0 && AI_time1 > 10 && u == 1){ direction = 1; xPos = xVPos; AI_time1 = 0; } if(canMoveRight && direction != 2 && AI_time1 > 10 && u == 3){ direction = 3; xPos = xVPos; AI_time1 = 0; }
if(!alarm && !cantGoAnyWhere){ if(AI_time2 > 99){ if(canMoveUp && yPos > b && AI_time2 > 99){ direction = 0; xPos = xVPos; AI_time2 = 0; } if(canMoveDown && yPos < b && AI_time2 > 99){ direction = 1; xPos = xVPos; AI_time2 = 0; } if(canMoveLeft && xPos > a && AI_time2 > 99){ direction = 2; yPos = yVPos; AI_time2 = 0; } if(canMoveRight && xPos < a && AI_time2 > 99){ direction = 3; yPos = yVPos; AI_time2 = 0; } } } if(alarm){ if(AI_time2 > 99){ if(canMoveDown && yPos > b && AI_time2 > 99){ direction = 1; xPos = xVPos; AI_time2 = 0; } if(canMoveUp && yPos < b && AI_time2 > 99){ direction = 0; xPos = xVPos; AI_time2 = 0; } if(canMoveRight && xPos > a && AI_time2 > 99){ direction = 3; yPos = yVPos; AI_time2 = 0; } if(canMoveLeft && xPos < a && AI_time2 > 99){ direction = 2; yPos = yVPos; AI_time2 = 0; } } } if(ghost){ a = 210; b = 189; speed = 4; alarm = false; alarmTime = 0; if(AI_time2 > 99){ if(canMoveUp && yPos > b && AI_time2 > 99){ direction = 0; xPos = xVPos; AI_time2 = 0; } if(canMoveDown && yPos < b && AI_time2 > 99){ direction = 1; xPos = xVPos; AI_time2 = 0; } if(canMoveLeft && xPos > a && AI_time2 > 99){ direction = 2; yPos = yVPos; AI_time2 = 0; } if(canMoveRight && xPos < a && AI_time2 > 99){ direction = 3; yPos = yVPos; AI_time2 = 0; } } }
if(changeMovement){ for(;;){ u = (int)(Math.random()*2); if(direction == 0 || direction == 1){ if(u == 0){ if(canMoveLeft) direction = 2; if(canMoveRight) direction = 3; } if(u == 1){ if(canMoveRight) direction = 3; if(canMoveLeft) direction = 2; } AI_time1 = 0; break; } if(direction == 2 || direction == 3){ if(u == 0){ if(canMoveUp) direction = 0; if(canMoveDown) direction = 1; } if(u == 1){ if(canMoveDown) direction = 1; if(canMoveUp) direction = 0; } if(cantGoAnyWhere){ if(canMoveLeft) direction = 2; if(canMoveRight) direction = 3; } AI_time1 = 0; break; } }
if(!alarm && !ghost && !cantGoAnyWhere){ if(canMoveLeft && xPos > a && lefttime > 66){ direction = 2; yPos = yVPos; AI_time2 = 0; lefttime = 0; } if(canMoveRight && xPos < a && righttime> 66){ direction = 3; yPos = yVPos; AI_time2 = 0; righttime = 0; } if(canMoveUp && yPos > b && uptime > 66){ direction = 0; xPos = xVPos; AI_time2 = 0; uptime = 0; } if(canMoveDown && yPos < b && downtime > 66){ direction = 1; xPos = xVPos; AI_time2 = 0; downtime = 0; } } if(alarm && !ghost){ if(canMoveRight && xPos > a && righttime > 66){ direction = 3; yPos = yVPos; AI_time2 = 0; lefttime = 0; } if(canMoveLeft && xPos < a && lefttime> 66){ direction = 2; yPos = yVPos; AI_time2 = 0; righttime = 0; } if(canMoveDown && yPos > b && downtime > 66){ direction = 1; xPos = xVPos; AI_time2 = 0; uptime = 0; } if(canMoveUp && yPos < b && uptime > 66){ direction = 0; xPos = xVPos; AI_time2 = 0; downtime = 0; } } if(ghost){ a = 210; b = 189; if(canMoveLeft && xPos > a && lefttime > 66){ direction = 2; yPos = yVPos; AI_time2 = 0; lefttime = 0; } if(canMoveRight && xPos < a && righttime> 66){ direction = 3; yPos = yVPos; AI_time2 = 0; righttime = 0; } if(canMoveUp && yPos > b && uptime > 66){ direction = 0; xPos = xVPos; AI_time2 = 0; uptime = 0; } if(canMoveDown && yPos < b && downtime > 66){ direction = 1; xPos = xVPos; AI_time2 = 0; downtime = 0; } } interval = 63 + 21*((int)(Math.random()*10)); }
if(ghost){ if(xVPos == 189 && yVPos == 441) direction = 0; if(xVPos == 231 && yVPos == 441) direction = 0; if(xVPos == 189 && yVPos == 399) direction = 2; if(xVPos == 231 && yVPos == 399) direction = 3; if(xVPos == 189 && yVPos == 357) direction = 0; if(xVPos == 231 && yVPos == 357) direction = 0; if(xVPos == 189 && yVPos == 315) direction = 2; if(xVPos == 231 && yVPos == 315) direction = 3; if(xVPos == 147 && yVPos == 315) direction = 0; if(xVPos == 273 && yVPos == 315) direction = 0; if(xVPos == 105 && yVPos == 231) direction = 3; if(xVPos == 315 && yVPos == 231) direction = 2; if(xVPos == 147 && yVPos == 105) direction = 1; if(xVPos == 273 && yVPos == 105) direction = 1; if(yVPos >= 189 && yVPos < 231 && xVPos == 210) direction = 1; if(xVPos == 210 && yVPos == 231){ ghost = false; speed = 2; cantGoAnyWhere = true; prisonTime = 0; } }
if(prisonTime > 231){ if(yVPos > 189 && yVPos <= 231 && xVPos == 210){ direction = 0; canMoveUp = true; } if(yVPos == 189){ prisonTime = 0; cantGoAnyWhere = false; } }
if(stop) speed = 0; if(direction == 0 && canMoveUp) yPos-=speed; if(direction == 1 && canMoveDown) yPos+=speed; if(direction == 2 && canMoveLeft) xPos-=speed; if(direction == 3 && canMoveRight) xPos+=speed;
if(direction == 2 && xPos < 0) xPos = 420; if(direction == 3 && xPos > 420) xPos = 0;
Enemy = new Rectangle(xPos -10, yPos - 10, 21, 21);
a = (xPos - 10)/21; b = (xPos - 10)%21; if(b < 6) b = 0; if(b > 16) b = 21; if(b < 17 && b > 5) b = 11; xVPos = a*21 + b + 10;
int c = (yPos - 10)/21; int d = (yPos - 10)%21; if(d < 6) d = 0; if(d > 16) d = 21; if(d < 17 && d > 5) d = 11; yVPos = c*21 + d + 10; }
public void draw(Graphics g){ if(Colour == 1) g.setColor(Color.red); if(Colour == 2) g.setColor(Color.cyan); if(Colour == 3) g.setColor(Color.pink); if(Colour == 4) g.setColor(Color.orange); //Polygon 类封装了坐标空间中封闭的二维区域的描述。此区域以任意条线段为边界,每条线段都是多边形的一条边 Polygon Ghost = null; if(gameFlow% 16 > 8){ int[] xPoints = {xPos - 5, xPos - 9, xPos - 9, xPos - 6, xPos - 3, xPos, xPos + 3, xPos + 6, xPos + 10, xPos + 10, xPos + 5, }; int[] yPoints = new int[] {yPos - 9, yPos - 4, yPos + 9, yPos + 4, yPos + 9, yPos + 4, yPos + 9, yPos + 4, yPos + 9, yPos - 4, yPos - 9, }; //初始化新的 Polygon。 //参数:xPoints为x坐标的数组;yPoints为y坐标的数组;xPoints.length为此Polygon中点的总数 Ghost = new Polygon (xPoints, yPoints, xPoints.length); }
if(gameFlow% 16 < 9){ int[] xPoints = {xPos - 5, xPos - 9, xPos - 9, xPos - 6, xPos, xPos + 6, xPos + 10, xPos + 10, xPos + 5, }; int[] yPoints = new int[] {yPos - 9, yPos - 4, yPos + 9, yPos + 4, yPos + 9, yPos + 4, yPos + 9, yPos - 4, yPos - 9, }; Ghost = new Polygon (xPoints, yPoints, xPoints.length); } if(!alarm && !ghost){ //用图形上下文的当前颜色填充由指定的Polygon对象定义的多边形Ghost。 g.fillPolygon(Ghost); }
//如果敌人没有失去反抗能力,则用蓝色进行填充敌人的整体,用白色填充敌人的眼睛。 if(!alarm || ghost){ //填充敌人眼睛的白色部分 g.setColor(Color.white); g.fillOval(xPos - 8, yPos - 5, 7, 8); g.fillOval(xPos + 2, yPos - 5, 7, 8); //填充敌人眼睛中的蓝色十字部分 g.setColor(Color.blue); if(direction == 0){ //使用当前颜色填充外接指定矩形框的椭圆。 g.fillOval(xPos - 6, yPos - 4, 3, 4); g.fillOval(xPos + 4, yPos - 4, 3, 4); } if(direction == 1){ g.fillOval(xPos - 6, yPos - 2, 3, 4); g.fillOval(xPos + 4, yPos - 2, 3, 4); } if(direction == 2){ g.fillOval(xPos - 7, yPos - 3, 3, 4); g.fillOval(xPos + 3, yPos - 3, 3, 4); } if(direction == 3){ g.fillOval(xPos - 5, yPos - 3, 3, 4); g.fillOval(xPos + 5, yPos - 3, 3, 4); } } //如果敌人失去反抗能力 if(alarm){ g.setColor(Color.blue); //当敌人即将恢复攻击能力时,将其颜色显示为灰色 if(alarmTime >= 231 && alarmTime%6 > 2) g.setColor(Color.lightGray); g.fillPolygon(Ghost); g.setColor(Color.white); g.fillOval(xPos - 6, yPos - 6, 5, 6); g.fillOval(xPos + 2, yPos - 6, 5, 6); //在此图形上下文的坐标系统中,使用当前颜色(白色), //在点(xPos - 6, yPos + 3)和(xPos - 4, yPos + 1)之间画一条线。 g.drawLine(xPos - 6, yPos + 3, xPos - 4, yPos + 1); g.drawLine(xPos - 4, yPos + 1, xPos - 2, yPos + 3); g.drawLine(xPos - 2, yPos + 3, xPos, yPos + 1); g.drawLine(xPos, yPos + 1, xPos + 2, yPos + 3); g.drawLine(xPos + 2, yPos + 3, xPos + 4, yPos + 1); g.drawLine(xPos + 4, yPos + 1, xPos + 6, yPos + 3); } } /** **方法:设置敌人是否处于失去反抗能力的状态 */ public void Alarm(int a){ if(a == 1) alarm = true; if(a == 2) alarm = false; alarmTime = 0; }
public void Ghost(){ ghost = true; alarmTime = 0; }
public Rectangle getBorder(){ return Enemy; }
public int status(){ int a = 0; if(alarm) a = 1; if(ghost) a = 2; return a; }
public void stop(){ stop = true; }}146
java吃豆子游戏import java.awt.*;
/** * 定义水果的类 */public class Fruit{ private int xPos, yPos, xVPos, yVPos; private int direction; private Rectangle fruit; private int appearTime; private int AI_time1; private int interval;
public Fruit(){ xPos = 210; yPos = 189; xVPos = 210; yVPos = 189; fruit = new Rectangle(xPos - 6, yPos - 6, 13, 13); direction = 2 + (int)(Math.random()*2); AI_time1 = 0; interval = 63 + 21*((int)(Math.random()*10)); }
/** * 移动水果 */ public void move(Wall[] wall){ appearTime++;
boolean canMoveUp, canMoveDown, canMoveRight, canMoveLeft, changeMovement;
canMoveUp = true; canMoveDown = true; canMoveLeft = true; canMoveRight = true; changeMovement = false;
Rectangle R; Rectangle UP = new Rectangle(xVPos - 10, yVPos - 10 - 1, 21, 21); Rectangle DOWN = new Rectangle(xVPos - 10, yVPos - 10 + 1, 21, 21); Rectangle LEFT = new Rectangle(xVPos - 10 - 1, yVPos - 10, 21, 21); Rectangle RIGHT = new Rectangle(xVPos - 10 + 1, yVPos - 10, 21, 21);
for(int i = 0; i < wall.length; i++){ R = new Rectangle(wall[i].getxPos() - 10, wall[i].getyPos() - 10, 21, 21); if(R.intersects(UP)) canMoveUp = false; if(R.intersects(UP) && direction == 0){ yPos = yVPos; changeMovement = true; }
if(R.intersects(DOWN)) canMoveDown = false; if(R.intersects(DOWN) && direction == 1){ yPos = yVPos; changeMovement = true; }
if(R.intersects(LEFT)) canMoveLeft = false; if(R.intersects(LEFT) && direction == 2){ xPos = xVPos; changeMovement = true; }
if(R.intersects(RIGHT)) canMoveRight = false; if(R.intersects(RIGHT) && direction == 3){ xPos = xVPos; changeMovement = true; } }
interval-=1; if(interval <=0) changeMovement = true;
int u = (int)(Math.random()*4); if(canMoveUp && direction != 1 && AI_time1 > 10 && u == 0){ direction = 0; xPos = xVPos; AI_time1 = 0; } if(canMoveDown && direction != 0 && AI_time1 > 10 && u == 1){ direction = 1; xPos = xVPos; AI_time1 = 0; } if(canMoveLeft && direction != 3 && AI_time1 > 10 && u == 2){ direction = 2; xPos = xVPos; AI_time1 = 0; } if(canMoveRight && direction != 2 && AI_time1 > 10 && u == 3){ direction = 3; xPos = xVPos; AI_time1 = 0; }
if(changeMovement){ for(;;){ u = (int)(Math.random()*2); if(direction == 0 || direction == 1){ if(u == 0){ if(canMoveLeft) direction = 2; if(canMoveRight) direction = 3; } if(u == 1){ if(canMoveRight) direction = 3; if(canMoveLeft) direction = 2; } AI_time1 = 0; break; } if(direction == 2 || direction == 3){ if(u == 0){ if(canMoveUp) direction = 0; if(canMoveDown) direction = 1; } if(u == 1){ if(canMoveDown) direction = 1; if(canMoveUp) direction = 0; } AI_time1 = 0; break; } } interval = 63 + 21*((int)(Math.random()*10)); }
if(direction == 0 && canMoveUp) yPos-=1; if(direction == 1 && canMoveDown) yPos+=1; if(direction == 2 && canMoveLeft) xPos-=1; if(direction == 3 && canMoveRight) xPos+=1;
if(direction == 2 && xPos < 0) xPos = 420; if(direction == 3 && xPos > 420) xPos = 0;
fruit = new Rectangle(xPos - 6, yPos - 6, 13, 13);
int a = (xPos - 10)/21; int b = (xPos - 10)%21; if(b < 6) b = 0; if(b > 16) b = 21; if(b < 17 && b > 5) b = 11; xVPos = a*21 + b + 10;
int c = (yPos - 10)/21; int d = (yPos - 10)%21; if(d < 6) d = 0; if(d > 16) d = 21; if(d < 17 && d > 5) d = 11; yVPos = c*21 + d + 10; }
public void draw(Graphics g){ double y = yPos + + 3*(Math.sin(6*appearTime)); g.setColor (new Color (254, 184, 4)); g.fillOval(xPos - 5, (int)(y - 5), 11, 11); g.setColor (new Color (28, 232, 4)); g.fillRect(xPos - 5, (int)(y - 5), 3, 3); g.fillRect(xPos - 7, (int)(y - 7), 3, 3); }
public int getAppearTime(){ return appearTime; }
public Rectangle getBorder(){ return fruit; }}
java吃豆子游戏源代码import java.awt.*;
/** * 定义豆子的类 */public class Gold{ private int xPos, yPos; private Rectangle gold; private int goldSpecies;
/** * 根据x、y和豆子的类型构造一个豆子的对象 */ public Gold(int a, int b, int c){ xPos = a; yPos = b; goldSpecies = c; gold = new Rectangle(xPos - 2, yPos - 2, 5, 5); }
/** * 根据x、y和豆子的类型构造一个豆子的对象 */ public void draw(Graphics g){ g.setColor(Color.yellow); //普通的小豆子 if(goldSpecies == 1){ g.fillOval(xPos - 2, yPos - 2, 4, 4); } //可以使敌人失去防抗能力的大豆子 if(goldSpecies == 2){ g.fillOval(xPos - 4, yPos - 4, 9, 9); public boolean bigGold(){ return goldSpecies == 2; }}
java吃豆子游戏import java.awt.*;import javax.swing.*;import java.awt.event.*;import java.net.*;import java.applet.*;
public class Packman extends JApplet implements KeyListener, ActionListener{
private Ticker t; private Image offScreenImage; //定义吃豆者 private Player player; //定义四个敌人 private Enemy Red, Cyan, Pink, Orange; //健康值与分数 private int health, Scores; //游戏是否结束 private boolean gameover; //游戏是否开始 private boolean gameStarted; private int alarmTime; private boolean alarm;
private Fruit fruit;//定义水果
private boolean UP_TYPED, DOWN_TYPED, LEFT_TYPED, RIGHT_TYPED;
//定义墙 private Image[] WALL; private AudioClip[] SOUND;
private Wall[] wall; //定义豆子的数组 private Gold[] gold;
private int gameFlow;
private boolean levelComplete;
/** * Applet初始化 */ public void init(){ player = new Player(); health = 100; //初始化敌人 Red = new Enemy(210, 189, 1, false, 0); Cyan = new Enemy(210, 231, 2, true, 0); Pink = new Enemy(220, 231, 3, true, 66); Orange = new Enemy(200, 231, 4, true, 132);
//添加键盘事件侦听 addKeyListener(this); requestFocus();
t = new Ticker(30); t.addActionListener(this);
gold = golds();
wall = walls(); WALL = new Image[47]; for(int i = 0; i < 47; i++) WALL[i] = getImage(getDocumentBase(), "Image\\" + "Wall" + (i + 1) + ".jpg"); SOUND = new AudioClip[8]; for(int i = 0; i < 8; i++) SOUND[i] = getAudioClip(getDocumentBase(), "Sound\\" + (i + 1) + ".au");
}
public void start (){ if(t != null) t.start(); }
public void stop(){ SOUND[2].stop(); SOUND[5].stop(); t.stop(); t = null; }
public void actionPerformed(ActionEvent e){ if(gameStarted) gameFlow++;
if(alarm) alarmTime++;
if(alarm && alarmTime == 330 && !gameover){ alarm = false; SOUND[2].stop(); SOUND[5].loop(); }
if(gameFlow == 110 && !gameover) SOUND[5].loop();
if(health <= 0 && !gameover){ gameover = true; gameFlow = 0; Red.stop(); Cyan.stop(); Pink.stop(); Orange.stop(); player.stop(); if(fruit != null) fruit = null; SOUND[5].stop(); SOUND[2].stop(); }
if(gameover && gameFlow == 66){ player.Dead(); SOUND[3].play(); }
if(fruit != null && fruit.getAppearTime() >660) fruit = null; if(fruit == null && gameFlow != 0 && gameFlow%1980 ==0 && !gameover) fruit = new Fruit(); levelComplete = true; for(int i = 0; i < gold.length; i++){ if(gold[i] != null){ levelComplete = false; break; } }
if(levelComplete){ gold = null; gold = golds(); } if(fruit != null) fruit.move(wall);
if(gameStarted){ player.move(wall); Red.move(player.getxPos(), player.getyPos(), wall); Cyan.move(player.getxPos(), player.getyPos(), wall); Pink.move(player.getxPos(), player.getyPos(), wall); Orange.move(player.getxPos(), player.getyPos(), wall); }
if(fruit != null){ if((fruit.getBorder()).intersects(player.getBorder())){ fruit = null; Scores+=75; health+=50; if(health > 100) health = 100; SOUND[1].play(); } }
for(int i = 0; i < gold.length; i++){ //如果吃豆者吃的是小金豆 if(gold[i] != null){ if((gold[i].getBorder()).intersects(player.getBorder()) && !(gold[i].bigGold())){ if(gameFlow > 110) SOUND[0].play(); gold[i] = null; Scores+=10; break; } } if(gold[i] != null){ if((gold[i].getBorder()).intersects(player.getBorder()) && gold[i].bigGold()){ gold[i] = null; Red.Alarm(1); Cyan.Alarm(1); Pink.Alarm(1); Orange.Alarm(1); Scores+=40; if(gameFlow > 110){ SOUND[5].stop(); SOUND[2].loop(); alarm = true; alarmTime = 0; } break; } } }
if((player.getBorder()).intersects(Red.getBorder())){ if(Red.status() == 1){ Red.Ghost(); Scores+=50; SOUND[7].play(); } if(Red.status() == 0 && !gameover) health--; } if((player.getBorder()).intersects(Cyan.getBorder())){ if(Cyan.status() == 1){ Cyan.Ghost(); Scores+=50; SOUND[7].play(); } if(Cyan.status() == 0 && !gameover) health-=2; } if((player.getBorder()).intersects(Pink.getBorder())){ if(Pink.status() == 1){ Pink.Ghost(); Scores+=50; SOUND[7].play(); } if(Pink.status() == 0 && !gameover) health-=3; } if((player.getBorder()).intersects(Orange.getBorder())){ if(Orange.status() == 1){ Orange.Ghost(); Scores+=50; SOUND[7].play(); } if(Orange.status() == 0 && !gameover) health-=4; } if(UP_TYPED) player.ChangeDirection(0); if(DOWN_TYPED) player.ChangeDirection(1); if(LEFT_TYPED) player.ChangeDirection(2); if(RIGHT_TYPED) player.ChangeDirection(3);
repaint(); }
public void paint(Graphics g){ g.setColor(Color.black); g.fillRect(0, 0, 545, 482);
for(int i = 0; i < wall.length; i++) g.drawImage(WALL[wall[i].getImageIndex() - 1], wall[i].getxPos() - 10, wall[i].getyPos() - 10, this); for(int i = 0; i < gold.length; i++){ if(gold[i] != null && !(gold[i].bigGold() && gameFlow%16 > 7)) gold[i].draw(g); }
g.setColor(Color.black); g.fillRect(179, 200, 21, 21); g.fillRect(200, 200, 21, 21); g.fillRect(221, 200, 21, 21); g.setColor(Color.pink); g.fillRect(179, 204, 63, 2);
player.draw(g); if(fruit != null) fruit.draw(g); if(!(gameover && gameFlow > 66)){ Red.draw(g); Cyan.draw(g); Pink.draw(g); Orange.draw(g); }
g.setColor(Color.black); g.fillRect(0, 220, 10, 23); g.fillRect(410, 220, 21, 25); g.fillRect(-10, 200, 21, 21); g.fillRect(-10, 242, 21, 21); g.fillRect(410, 200, 21, 21); g.fillRect(410, 242, 21, 21);
g.setColor(Color.white); g.drawString("生命: " + health + "%", 420, 84); g.drawString("积分: " + Scores, 420, 105);
if(!gameStarted){ g.setColor(Color.cyan); g.drawString(" 按空格开始", 153, 273); } if(gameover && gameFlow > 100){ g.setColor(Color.red); g.drawString("游戏结束", 179, 238); } }
public void keyPressed(KeyEvent e){ //点击“空格”,游戏开始 if(e.getKeyCode() == KeyEvent.VK_SPACE){ SOUND[6].play(); gameStarted = true; }
//点击方向键,执行相应的操作 if(e.getKeyCode() == KeyEvent.VK_UP){ player.ChangeDirection(0); UP_TYPED = true; DOWN_TYPED = false; LEFT_TYPED = false; RIGHT_TYPED = false; } if(e.getKeyCode() == KeyEvent.VK_DOWN){ player.ChangeDirection(1); UP_TYPED = false; DOWN_TYPED = true; LEFT_TYPED = false; RIGHT_TYPED = false; } if(e.getKeyCode() == KeyEvent.VK_LEFT ){ player.ChangeDirection(2); UP_TYPED = false; DOWN_TYPED = false; LEFT_TYPED = true; RIGHT_TYPED = false; } if(e.getKeyCode() == KeyEvent.VK_RIGHT){ player.ChangeDirection(3); UP_TYPED = false; DOWN_TYPED = false; LEFT_TYPED = false; RIGHT_TYPED = true; } }
public void keyReleased(KeyEvent e){} public void keyTyped(KeyEvent e){}
public void update(Graphics g) { Graphics offScreenGraphics; if (offScreenImage == null) { //返回创建一幅用于双缓冲的、可在屏幕外绘制的图像。 //如果组件是不可显示的,则返回值可能为 null。 offScreenImage = createImage(545, 482); } offScreenGraphics = offScreenImage.getGraphics(); offScreenGraphics.setColor(Color.white); offScreenGraphics.fillRect(0, 0, 545, 482); paint(offScreenGraphics); g.drawImage(offScreenImage, 0, 0, this); }
/** * 布置豆子 */ private static Gold[] golds(){ Gold[] Golds = new Gold[157]; for(int i = 0; i < 8; i++) Golds[i] = new Gold(42 + (i - 0)*21, 42, 1); for(int i = 8; i < 16; i++) Golds[i] = new Gold(231 + (i - 8)*21, 42, 1); Golds[16] = new Gold(42, 63, 2); Golds[17] = new Gold(378, 63, 2); Golds[18] = new Gold(189, 63, 1); Golds[19] = new Gold(189, 84, 1); Golds[20] = new Gold(231, 63, 1); Golds[21] = new Gold(231, 84, 1); for(int i = 22; i < 26; i++) Golds[i] = new Gold(42, 84 + (i - 22)*21, 1); for(int i = 26; i < 30; i++) Golds[i] = new Gold(378, 84 + (i - 26)*21, 1); Golds[30] = new Gold(63, 105, 1); Golds[31] = new Gold(84, 105, 1); Golds[32] = new Gold(336, 105, 1); Golds[33] = new Gold(357, 105, 1); for(int i = 34; i < 43; i++) Golds[i] = new Gold(126 + (i - 34)*21, 105, 1); for(int i = 43; i < 60; i++) Golds[i] = new Gold(105, 63 + (i - 43)*21, 1); for(int i = 60; i < 77; i++) Golds[i] = new Gold(315, 63 + (i - 60)*21, 1); Golds[77] = new Gold(63, 147, 1); Golds[78] = new Gold(84, 147, 1); Golds[79] = new Gold(336, 147, 1); Golds[80] = new Gold(357, 147, 1); Golds[81] = new Gold(147, 126, 1); Golds[82] = new Gold(273, 126, 1); for(int i = 83; i < 86; i++) Golds[i] = new Gold(147 + (i - 83)*21, 147, 1); for(int i = 86; i < 89; i++) Golds[i] = new Gold(231 + (i - 86)*21, 147, 1); Golds[89] = new Gold(42, 315, 1); Golds[90] = new Gold(42, 336, 1); Golds[91] = new Gold(42, 357, 2); for(int i = 92; i < 95; i++) Golds[i] = new Gold(63, 357 + (i - 92)*21, 1); for(int i = 95; i < 98; i++) Golds[i] = new Gold(42, 399 + (i - 95)*21, 1); Golds[98] = new Gold(63, 315, 1); Golds[99] = new Gold(84, 315, 1); for(int i = 100; i < 104; i++) Golds[i] = new Gold(126 + (i - 100)*21, 315, 1); for(int i = 104; i < 108; i++) Golds[i] = new Gold(231 +(i - 104)*21, 315, 1); Golds[108] = new Gold(189, 336, 1); Golds[109] = new Gold(231, 336, 1); for(int i = 110; i < 119; i++) Golds[i] = new Gold(126 + (i - 110)*21, 357, 1); for(int i = 119; i < 122; i++) Golds[i] = new Gold(336 + (i - 119)*21, 315, 1); Golds[122] = new Gold(378, 336, 1); Golds[123] = new Gold(378, 357, 2); for(int i = 124; i < 127; i++) Golds[i] = new Gold(357, 357 + (i - 124)*21, 1); for(int i = 127; i < 130; i++) Golds[i] = new Gold(378, 399 + (i - 127)*21, 1); Golds[130] = new Gold(84, 399, 1); Golds[131] = new Gold(336, 399, 1); Golds[132] = new Gold(147, 378, 1); Golds[133] = new Gold(189, 420, 1); Golds[134] = new Gold(273, 378, 1); Golds[135] = new Gold(231, 420, 1); for(int i = 136; i < 139; i++) Golds[i] = new Gold(147 + (i - 136)*21, 399, 1); for(int i = 139; i < 142; i++) Golds[i] = new Gold(231 + (i - 139)*21, 399, 1); for(int i = 142; i < 157; i++) Golds[i] = new Gold(63 + (i - 142)*21, 441, 1); return Golds; }
private static Wall[] walls(){ Wall[] Walls = new Wall[216]; Walls[0] = new Wall(21, 21, 29); Walls[1] = new Wall(399, 21, 30); Walls[2] = new Wall(21, 462, 28); Walls[3] = new Wall(399, 462, 31);
for(int i = 4; i < 12; i++) Walls[i] = new Wall(42 + (i - 4)*21, 21, 27); Walls[12] = new Wall(210, 21, 22); for(int i = 13; i < 21; i++) Walls[i] = new Wall(231 + (i - 13)*21, 21, 27);
Walls[21] = new Wall(210, 42, 2); Walls[22] = new Wall(210, 63, 2); Walls[23] = new Wall(210, 84, 5);
for(int i = 24; i < 30; i++) Walls[i] = new Wall(21, 42 + 21*(i - 24), 26);
Walls[30] = new Wall(63, 63, 32); Walls[31] = new Wall(84, 63, 33); Walls[32] = new Wall(63, 84, 35); Walls[33] = new Wall(84, 84, 34);
Walls[34] = new Wall(63, 126, 6); Walls[35] = new Wall(84, 126, 4);
Walls[36] = new Wall(126, 63, 32); Walls[37] = new Wall(126, 84, 35); Walls[38] = new Wall(147, 63, 19); Walls[39] = new Wall(147, 84, 17); Walls[40] = new Wall(168, 63, 33); Walls[41] = new Wall(168, 84, 34);
Walls[42] = new Wall(252, 63, 32); Walls[43] = new Wall(252, 84, 35); Walls[44] = new Wall(273, 63, 19); Walls[45] = new Wall(273, 84, 17); Walls[46] = new Wall(294, 63, 33); Walls[47] = new Wall(294, 84, 34);
Walls[48] = new Wall(336, 63, 32); Walls[49] = new Wall(357, 63, 33); Walls[50] = new Wall(336, 84, 35); Walls[51] = new Wall(357, 84, 34);
Walls[52] = new Wall(336, 126, 6); Walls[53] = new Wall(357, 126, 4);
for(int i = 54; i < 60; i++) Walls[i] = new Wall(399, 42 + (i - 54)*21, 24);
Walls[60] = new Wall(21, 168, 28); Walls[61] = new Wall(42, 168, 25); Walls[62] = new Wall(63, 168, 25); Walls[63] = new Wall(84, 168, 37); Walls[64] = new Wall(84, 189, 26); Walls[65] = new Wall(84, 210, 38); for(int i = 66; i < 69; i++) Walls[i] = new Wall(63 - (i - 66)*21, 210, 27);
for(int i = 69; i < 72; i++) Walls[i] = new Wall(21 + (i - 69)*21, 252, 25); Walls[72] = new Wall(84, 252, 37); Walls[73] = new Wall(84, 273, 26); Walls[74] = new Wall(84, 294, 38); Walls[75] = new Wall(63, 294, 27); Walls[76] = new Wall(42, 294, 27);
Walls[77] = new Wall(126, 126, 3); Walls[78] = new Wall(126, 147, 2); Walls[79] = new Wall(126, 168, 13); Walls[80] = new Wall(147, 168, 1); Walls[81] = new Wall(168, 168, 4); Walls[82] = new Wall(126, 189, 2); Walls[83] = new Wall(126, 210, 5);
Walls[84] = new Wall(168, 126, 6); Walls[85] = new Wall(189, 126, 1); Walls[86] = new Wall(210, 126, 14); Walls[87] = new Wall(231, 126, 1); Walls[88] = new Wall(252, 126, 4); Walls[89] = new Wall(210, 147, 2); Walls[90] = new Wall(210, 168, 5);
Walls[91] = new Wall(294, 126, 3); Walls[92] = new Wall(294, 147, 2); Walls[93] = new Wall(294, 168, 15); Walls[94] = new Wall(273, 168, 1); Walls[95] = new Wall(252, 168, 6); Walls[96] = new Wall(294, 189, 2); Walls[97] = new Wall(294, 210, 5);
Walls[98] = new Wall(399, 168, 31); Walls[99] = new Wall(378, 168, 25); Walls[100] = new Wall(357, 168, 25); Walls[101] = new Wall(336, 168, 36); Walls[102] = new Wall(336, 189, 24); Walls[103] = new Wall(336, 210, 39); for(int i = 104; i < 107; i++) Walls[i] = new Wall(357 + (i - 104)*21, 210, 27);
for(int i = 107; i < 110; i++) Walls[i] = new Wall(357 + (i - 107)*21, 252, 25); Walls[110] = new Wall(336, 252, 36); Walls[111] = new Wall(336, 273, 24); Walls[112] = new Wall(336, 294, 39); Walls[113] = new Wall(357, 294, 27); Walls[114] = new Wall(378, 294, 27);
Walls[115] = new Wall(168, 210, 40); Walls[116] = new Wall(168, 231, 44); Walls[117] = new Wall(168, 252, 43); for(int i = 118; i < 121; i++) Walls[i] = new Wall(189 + (i - 118)*21, 252, 47); Walls[121] = new Wall(252, 252, 42); Walls[122] = new Wall(252, 231, 46); Walls[123] = new Wall(252, 210, 41);
Walls[124] = new Wall(126, 252, 3); Walls[125] = new Wall(126, 273, 2); Walls[126] = new Wall(126, 294, 5); Walls[127] = new Wall(294, 252, 3); Walls[128] = new Wall(294, 273, 2); Walls[129] = new Wall(294, 294, 5);
Walls[130] = new Wall(21, 294, 29); for(int i = 131; i < 134; i++) Walls[i] = new Wall(21, 315 + (i - 131)*21, 26); Walls[134] = new Wall(21, 378, 21); for(int i = 135; i < 138; i++) Walls[i] = new Wall(21, 399 + (i - 135)*21, 26);
Walls[138] = new Wall(399, 294, 30); for(int i = 139; i < 142; i++) Walls[i] = new Wall(399, 315 + (i - 139)*21, 24); Walls[142] = new Wall(399, 378, 23); for(int i = 143; i < 146; i++) Walls[i] = new Wall(399, 399 + (i - 143)*21, 24);
for(int i = 146; i < 163; i++) Walls[i] = new Wall(42 + (i - 146)*21, 462, 25);
Walls[163] = new Wall(42, 378, 4); Walls[164] = new Wall(378, 378, 6);
Walls[165] = new Wall(168, 294, 6); Walls[166] = new Wall(189, 294, 1); Walls[167] = new Wall(210, 294, 14); Walls[168] = new Wall(231, 294, 1); Walls[169] = new Wall(252, 294, 4); Walls[170] = new Wall(210, 315, 2); Walls[171] = new Wall(210, 336, 5);
Walls[172] = new Wall(168, 378, 6); Walls[173] = new Wall(189, 378, 1); Walls[174] = new Wall(210, 378, 14); Walls[175] = new Wall(231, 378, 1); Walls[176] = new Wall(252, 378, 4); Walls[177] = new Wall(210, 399, 2); Walls[178] = new Wall(210, 420, 5);
Walls[179] = new Wall(126, 336, 6); Walls[180] = new Wall(147, 336, 1); Walls[181] = new Wall(168, 336, 4); Walls[182] = new Wall(252, 336, 6); Walls[183] = new Wall(273, 336, 1); Walls[184] = new Wall(294, 336, 4);
Walls[185] = new Wall(63, 336, 6); Walls[186] = new Wall(84, 336, 9); Walls[187] = new Wall(84, 357, 2); Walls[188] = new Wall(84, 378, 5);
Walls[189] = new Wall(336, 336, 8); Walls[190] = new Wall(336, 357, 2); Walls[191] = new Wall(336, 378, 5); Walls[192] = new Wall(357, 336, 4);
Walls[193] = new Wall(63, 420, 6); Walls[194] = new Wall(84, 420, 1); Walls[195] = new Wall(105, 420, 1); Walls[196] = new Wall(126, 420, 12); Walls[197] = new Wall(147, 420, 1); Walls[198] = new Wall(168, 420, 4); Walls[199] = new Wall(126, 399, 2); Walls[200] = new Wall(126, 378, 3);
Walls[201] = new Wall(252, 420, 6); Walls[202] = new Wall(273, 420, 1); Walls[203] = new Wall(294, 420, 12); Walls[204] = new Wall(315, 420, 1); Walls[205] = new Wall(336, 420, 1); Walls[206] = new Wall(357, 420, 4); Walls[207] = new Wall(294, 399, 2); Walls[208] = new Wall(294, 378, 3); Walls[215] = new Wall(231, 210, 1); return Walls; }}
java吃豆子import java.awt.*;
/** * 定义吃豆者的类 */public class Player{
private int xPos; private int yPos; private int xVPos; private int yVPos; private int speed; private int direction; private Rectangle player; //吃豆者的张开角度 private int mouthDegree; //吃豆者是否移动 private boolean isMoving = true; //吃豆者的张、合情况 private boolean mouthOpen, mouthClose; //是否可以上下左右移动 private boolean canMoveUp, canMoveDown, canMoveLeft, canMoveRight; //游戏是否结束 private boolean dead;
/** **构造函数:初始化吃豆者的属性 */ public Player(){ xPos = 210; yPos = 357; xVPos = xPos; yVPos = yPos; direction = 2; //构造一个新的 Rectangle,其左上角被指定为(xPos - 10, yPos - 5),而其宽度和高度为(11,11)。 player = new Rectangle(xPos - 10, yPos - 5, 11, 11); //吃豆者的初始张开角度为30度 mouthDegree = 30; //初始张合状态:张开 mouthOpen = true; speed = 2; }
public void draw(Graphics g){ int DirectionAngle = 180; g.setColor(Color.yellow); if(direction == 0) DirectionAngle = 90; if(direction == 1) DirectionAngle = -90; if(direction == 2) DirectionAngle = 180; if(direction == 3) DirectionAngle = 0; if(dead) DirectionAngle = 90;
//填充覆盖指定矩形的圆弧或椭圆弧。 //得到的弧由(mouthDegree + DirectionAngle)并跨越(180 - mouthDegree)角度。 //弧的中心是矩形的中心,此矩形的原点为 (xPos - 10, yPos - 10),大小为(21, 21) g.fillArc(xPos - 10, yPos - 10, 21, 21, mouthDegree + DirectionAngle, 180 - mouthDegree); g.fillArc(xPos - 10, yPos - 10, 21, 21, 180 + DirectionAngle, 180 - mouthDegree); }
public void move(Wall[] wall){ isMoving = true;
canMoveUp = true; canMoveDown = true; canMoveLeft = true; canMoveRight = true;
if(!dead){ if(mouthDegree == 70){ mouthClose = true; mouthOpen = false; } if(mouthDegree == -10){ mouthOpen = true; mouthClose = false; } }
Rectangle R; // 构造一个新的 Rectangle,其左上角被指定为(xVPos - 10,yVPos - 12),而其宽度和高度为(21,21)。 Rectangle UP = new Rectangle(xVPos - 10, yVPos - 12, 21, 21); Rectangle DOWN = new Rectangle(xVPos - 10, yVPos - 8, 21, 21); Rectangle LEFT = new Rectangle(xVPos - 12, yVPos - 10, 21, 21); Rectangle RIGHT = new Rectangle(xVPos - 8, yVPos - 10, 21, 21);
for(int i = 0; i < wall.length; i++){ R = new Rectangle(wall[i].getxPos() - 10, wall[i].getyPos() - 10, 21, 21); //确定此 Rectangle R是否与指定 Rectangle UP 相交。如果两个矩形的交集为非空,则它们是相交的。 //如果R与UP相交,则返回 true;否则返回 false。 if(R.intersects(UP)) canMoveUp = false; if(R.intersects(UP) && direction == 0) yPos = yVPos;
if(R.intersects(DOWN)) canMoveDown = false; if(R.intersects(DOWN) && direction == 1) yPos = yVPos;
if(R.intersects(LEFT)) canMoveLeft = false; if(R.intersects(LEFT) && direction == 2) xPos = xVPos;
if(R.intersects(RIGHT)) canMoveRight = false; if(R.intersects(RIGHT) && direction == 3) xPos = xVPos; }
//如果吃豆者可以移动,则新坐标位置=原坐标位置+速度 if(direction == 0 && canMoveUp) yPos-=speed; else if(direction == 1 && canMoveDown) yPos+=speed; else if(direction == 2 && canMoveLeft) xPos-=speed; else if(direction == 3 && canMoveRight) xPos+=speed; //将吃豆者的移动状态设置成为:不能移动 else isMoving = false;
if(direction == 2 && xPos < 0) xPos = 420; if(direction == 3 && xPos > 420) xPos = 0;
player = new Rectangle(xPos - 5, yPos - 5, 11, 11);
int a = (xPos - 10)/21; int b = (xPos - 10)%21; if(b < 6) b = 0; if(b > 16) b = 21; if(b < 17 && b > 5) b = 11; xVPos = a*21 + b + 10;
int c = (yPos - 10)/21; int d = (yPos - 10)%21; if(d < 6) d = 0; if(d > 16) d = 21; if(d < 17 && d > 5) d = 11; yVPos = c*21 + d + 10;
int angularSpeed = 10; if(dead && mouthDegree <= 175){ isMoving = true; mouthOpen = true; mouthClose = false; angularSpeed = 5; }
if(dead && mouthDegree > 175){ isMoving = false; }
if(mouthOpen && isMoving){ mouthDegree+=angularSpeed ; } if(mouthClose && isMoving){ mouthDegree-=angularSpeed ; }
}
/** **方法:改变前进方向 */ public void ChangeDirection(int a){ if(a == 0 && canMoveUp){ direction = 0; xPos = xVPos; } if(a == 1 && canMoveDown){ direction = 1; xPos = xVPos; }
if(a == 2 && canMoveLeft){ direction = 2; yPos = yVPos; }
if(a == 3 && canMoveRight){ direction = 3; yPos = yVPos; } }
/** **返回值:int **方法:返回xPos */ public int getxPos(){ return xPos; }
/** **返回值:int **方法:返回yPos */ public int getyPos(){ return yPos; }
public Rectangle getBorder(){ return player; } /** **方法:吃豆者停止运动 */ public void stop(){ speed = 0;lwfree.cn }
/** **方法:游戏结束,设置吃豆者的角度为30 */ }}
java吃豆子游戏设计import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.PrintStream;
/** **实现线程 */public class Ticker extends Thread{
ActionListener al; private boolean isTicking; Thread t; int delay;
public Ticker(int i, ActionListener actionlistener){ al = actionlistener; delay = i; t = new Thread(this); t.start(); isTicking = false; }
public Ticker(int i){ delay = i; t = new Thread(this); t.start(); isTicking = false; }
public void addActionListener(ActionListener actionlistener){ if(al == null) al = actionlistener; else System.out.println("WARNING: ActionListener already added to Ticker."); }
public boolean isRunning(){ return isTicking; }
public void start(){ isTicking = true; }
private void fireActionPerformed(){ if(al == null || !isTicking){ return; } else{ ActionEvent actionevent = new ActionEvent(this, 0, null); al.actionPerformed(actionevent); return; } }
public void run(){ do{ fireActionPerformed(); try{ Thread.sleep(delay); } catch(InterruptedException interruptedexception){ }}
java吃豆子游戏import java.awt.*;
/** * 定义墙边界的类 */public class Wall{
private int xPos; private int yPos; private int IndexOfImage; private Rectangle wall;
/** * 根据x、y和图片索引构造 */ public Wall(int a, int b, int c){ xPos = a; yPos = b; IndexOfImage = c; wall = new Rectangle(xPos - 10, yPos - 10, 21, 21); } /** * 返回墙的边界 */ public Rectangle getWallBorder(){ return wall; }
/** * 返回图片索引构的值 */ public int getImageIndex(){ return IndexOfImage; }}