?? player.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;
}
/**
**方法:游戲結束,設置吃豆者的角度為30
*/
public void Dead(){
dead = true;
mouthDegree = 30;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -