敵人、人物增加血量功能,並且讓子彈有傷害,新增各類地圖
因為目前敵人都是一槍倒,但為了後續加上選擇槍械功能所以幫敵人加上血量設定
class Enemy { float x, y; float speed = 1; int health = 50; ///血量設定 float Endv; Enemy(float vx, float vy, int dir) { x = vx; y = vy; Endv = dir; } Enemy(){ reset(); } void move() ///移動時會以最短距離靠近角色 { float angle = atan2(pl.y - y, pl.x - x); ///計算玩家與敵人的角度 x += cos(angle) * speed; y += sin(angle) * speed; } void drawApp() { image(enImg, x, y, 50, 50); } void reset() { x = random(pl.x+400); ///隨機傳送敵人,並與玩家有一段距離 y = random(pl.y+400); } } 並在主程式的加上
for (int j=0; j < ens.size() ; j++){ ///產生敵人 Enemy enemy = ens.get(j); if (bullet.bump(enemy)){ ///敵人碰觸高子彈時消失 if (enemy.health <= 0) { 新增的程式 ens.remove(j); } } }
同樣方法給人物也加上血量並且製作一個長方形掛在人物身上class Player { float x, y; int speed = 8;///移動速度 int dv = 0; ///控制子彈方向 boolean left, right, up, down; int health = 100; Player(float dx, float dy){ x = dx; y = dy; } void move(){ if (left){ x -= speed; dv = 1; } if (right){ x += speed; dv = 2; } if (up){ y -= speed; dv = 3; } if (down){ y += speed; dv = 4; } } void drawApp(){ 人類的draw fill(255); // 血條顏色為白色 rect(x , y-15, health * 0.5, 5); // 血條的寬度為 health * 0.5 當血量減少時寬度也會減少 image(plImg, x, y, 50, 60); } }
畫人時就會掛一個長方形在人物上了 敵人沒有血量條是因為這樣會導致畫面太亂並給敵人加上會扣血量程式
if (dist(x, y, pl.x, pl.y) < 20) {
pl.health -= 3;
if(pl.health<0)state = 2;}
2.子彈傷害
class Bullet ///定義子彈的屬性 { float x, y; float speed = 30; ///子彈移動速度 float damage = 10; 子彈傷害 int dv; boolean bump(Enemy en) 碰撞功能 { if(dist(x,y,en.x,en.y)< 30){ en.health -= damage; 敵人碰到子彈會扣血 return true; }
各類地圖製作
PImage map1,map2,map3;在void setup(){
map1 = loadImage("snow.png"); map2 = loadImage("desert.png"); map3 = loadImage("prison.png"); }加上選擇畫面
void chooseScreen() { image(mainuiImg,0,0); fill(255); PFont font = createFont("微軟正黑體 Bold",40); textFont(font); text("地圖選擇",525,100); text("雪地",150,200); image(map1,100,250,200,200); fill(#FABD21); text("沙漠",550,200); image(map2,500,250,200,200); fill(#9D9B97); text("監獄",950,200); image(map3,900,250,200,200); if (mouseX > 100 && mouseX < 300 && mouseY > 250 && mouseY < 450)tint(#95948F, 255);///滑鼠碰到會變灰 image(map1,100,250,200,200); noTint(); if (mouseX > 500 && mouseX < 700 && mouseY > 250 && mouseY < 450)tint(#95948F, 255);///滑鼠碰到會變灰 image(map2,500,250,200,200); noTint(); if (mouseX > 900 && mouseX < 1100 && mouseY > 250 && mouseY < 450)tint(#95948F, 255);///滑鼠碰到會變灰 image(map3,900,250,200,200); noTint(); }
void mousePressed() { if (state == 0 && mouseX > 450 && mouseX < 730 && mouseY > 450 && mouseY < 560)state = 5;///start畫面 按下按鈕後跳到選擇地圖畫面 if (state == 5 && mouseX > 100 && mouseX < 300 && mouseY > 250 && mouseY < 450)state = 1; 選擇地圖後跳到遊戲畫面 if (state == 5 && mouseX > 500 && mouseX < 700 && mouseY > 250 && mouseY < 450)state = 6; if (state == 5 && mouseX > 900 && mouseX < 1100 && mouseY > 250 && mouseY < 450)state = 7; }
結果圖 選圖他會變灰
沒有留言:
張貼留言