Skip to content

Commit

Permalink
now character can die
Browse files Browse the repository at this point in the history
  • Loading branch information
Zilou Li authored and Zilou Li committed Apr 12, 2024
1 parent 6130586 commit 0068305
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 32 deletions.
60 changes: 34 additions & 26 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion Code/src/main/java/org/example/Bomb.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import processing.core.PApplet;
import processing.core.PImage;

import java.util.Iterator;

public class Bomb extends Objects{
PApplet parent;
PImage bombImage;
Expand Down Expand Up @@ -71,10 +73,14 @@ public static int encodeCoordinate(int x, int y) {
}

public static void bombRender(){
for (Bomb bomb : bombs) {
Iterator<Bomb> iterator = bombs.iterator();
while (iterator.hasNext()) {
Bomb bomb = iterator.next();
int packedNumber = bomb.update();
if (bomb.showed) {
bomb.render();
} else {
iterator.remove(); // 删除showed为false的元素
}
if (packedNumber != 0) {
int x = packedNumber / 10000;
Expand Down
10 changes: 10 additions & 0 deletions Code/src/main/java/org/example/Character.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class Character extends GameLoop{
int px, py;
int health;
int speed;
boolean exist;
public static ArrayList<Player> players;
public static ArrayList<Enemy> enemies;
public static int enemyNumber = 5;
Expand Down Expand Up @@ -71,6 +72,15 @@ public boolean collisionDetect() {
if (bomb.showed && bomb.bombActive && dist(x, y, bomb.x() + (float) tile / 2, bomb.y() + (float) tile / 2) < tile) {
}
}*/
public void ifDamageCharacter(){
//handle the interaction between rocks and flames
if (Flame.flameCheck(this.x(), this.y())) {
this.health -= 1;
if (this.health == 0) {
this.exist = false;
}
}
}

int x(){return px;}
int y(){return py;}
Expand Down
19 changes: 16 additions & 3 deletions Code/src/main/java/org/example/Enemy.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import processing.core.PImage;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;

public class Enemy extends Character{
Expand All @@ -17,6 +18,7 @@ public class Enemy extends Character{
this.enemyImage = enemy;
this.health = 1;
this.speed = 3;
this.exist = true;
}

public static ArrayList<Enemy> generateEnemies(PApplet parent) {
Expand All @@ -39,13 +41,24 @@ public static ArrayList<Enemy> generateEnemies(PApplet parent) {
return enemies;
}

public void enemyMovement(){

}

void render(){
parent.image(enemyImage,px,py,30,30);
}

public static void enemiesRender(){
for (Enemy enemy : enemies) {
enemy.render();
public static void enemiesRender() {
Iterator<Enemy> iterator = enemies.iterator();
while (iterator.hasNext()) {
Enemy enemy = iterator.next();
enemy.ifDamageCharacter();
if (enemy.exist) {
enemy.render();
} else {
iterator.remove();
}
}
}
}
6 changes: 6 additions & 0 deletions Code/src/main/java/org/example/GameLoop.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class GameLoop extends PApplet{
public static boolean move=false, up=false, down=false, left=false, right=false;
public static int rows = 15, cols = 31;
public static boolean gameWon = false;
public static boolean gameLost = false;

public void settings() {
size(width, height);
Expand Down Expand Up @@ -86,8 +87,10 @@ public void draw() {
BreakableRock.rocksRender();
DoorKey.doorKeyRender(this);
Door.doorRender(this);

Player.player1Render();


gameEndDetect();

BombPowerUp.getPowerUp(this);
Expand Down Expand Up @@ -146,6 +149,9 @@ public void keyReleased() {
}

private static void gameEndDetect(){
if(gameLost){
System.out.println("GameOver");
}
if (Items.doorKey != null && !Items.doorKey.collected && Items.doorKey.visible) {
Player player = Character.players.get(0);
float distance = dist(player.px, player.py, Items.doorKey.x, Items.doorKey.y);
Expand Down
10 changes: 8 additions & 2 deletions Code/src/main/java/org/example/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ public class Player extends Character {
this.px=x;
this.py=y;
this.playerImage = playerImage;
this.health = 3;
this.health = 1;
this.explosionDistance = 1;
this.maxBombs = 1;
this.speed = 3;
this.exist = true;
}

public static ArrayList<Player> setPlayer1(PApplet parent) {
Expand Down Expand Up @@ -96,6 +97,11 @@ public int getMaxBombs() {
public void increaseLife() {this.health += 1;}

public static void player1Render(){
Character.players.get(0).render();
players.get(0).ifDamageCharacter();
if(Character.players.get(0).exist){
Character.players.get(0).render();
}else {
GameLoop.gameLost = true;
}
}
}

0 comments on commit 0068305

Please sign in to comment.