diff --git a/minigames/__init__.py b/minigames/__init__.py index 48e7001..b6c8459 100644 --- a/minigames/__init__.py +++ b/minigames/__init__.py @@ -1,3 +1,4 @@ +from avoid_the_guard.avoid_the_guard import AvoidTheGuard from stay_in_the_ring.stay_in_the_ring import StayInTheRing from blackcat import BlackCat from spingame import SpinGame diff --git a/minigames/avoid_the_guard/__init__.py b/minigames/avoid_the_guard/__init__.py new file mode 100644 index 0000000..b552838 --- /dev/null +++ b/minigames/avoid_the_guard/__init__.py @@ -0,0 +1 @@ +from avoid_the_guard import AvoidTheGuard \ No newline at end of file diff --git a/minigames/avoid_the_guard/avoid_the_guard.py b/minigames/avoid_the_guard/avoid_the_guard.py new file mode 100644 index 0000000..709fcba --- /dev/null +++ b/minigames/avoid_the_guard/avoid_the_guard.py @@ -0,0 +1,95 @@ +import pygame +import input_map +from minigames import minigame +from minigames import multiplayer +from entities import PersoPlayer +from entities import PersoGuard + +class AvoidTheGuard(multiplayer.Minigame): + name = 'Push him on the guard!' + max_duration = 10000 + + def __init__(self, game): + minigame.Minigame.__init__(self, game) + + self.width = game.GAME_WIDTH + self.height = game.GAME_HEIGHT + + def init(self): + self.background = pygame.image.load("./res/img/avoid_the_guard/Background.png").convert() + + self.players = [PersoPlayer(50, 300, "./res/img/avoid_the_guard/Player1.png", self.difficulty), \ + PersoPlayer(700, 300, "./res/img/avoid_the_guard/Player2.png", self.difficulty), ] + self.enemies = [] + self.initEnnemies() + + self.score = [self.players[0].life, self.players[1].life] + + self.currentTime = pygame.time.get_ticks()/1000.0 + + def initEnnemies(self): + for index in range(self.difficulty+6): + self.enemies.append(PersoGuard(375, 300, "./res/img/avoid_the_guard/Guard.png", self.difficulty, self.players)) + + def tick(self): + self.score[0] = self.players[0].life + self.score[1] = self.players[1].life + + self.events() + self.update(pygame.time.get_ticks()/1000.0 - self.currentTime) + self.draw() + + self.currentTime = pygame.time.get_ticks()/1000.0 + + def get_results(self): + if self.players[0].life > self.players[1].life: + return [True, False] + elif self.players[0].life < self.players[1].life: + return [False, True] + else: + return [False, False] + + def update(self, timeElapsed): + for player in self.players: + player.update(timeElapsed) + + for enemy in self.enemies: + enemy.update(timeElapsed) + + self.checkEnnemyCollisions() + + def draw(self): + self.screen.blit(self.background, [0, 0]) + for player in self.players: + player.draw(self.screen) + + for enemy in self.enemies: + enemy.draw(self.screen) + + def events(self): + pygame.event.get() + for i in range(2): + keys = input_map.get_player_keys(i) + if keys[input_map.UP]: + self.players[i].move("up") + if keys[input_map.RIGHT]: + self.players[i].move("right") + if keys[input_map.DOWN]: + self.players[i].move("down") + if keys[input_map.LEFT]: + self.players[i].move("left") + if keys[input_map.ACTION]: + self.hit(i) + + def checkEnnemyCollisions(self): + for ennemy in self.enemies: + for player in self.players: + if ennemy.sprite.rect.colliderect(player.sprite.rect): + player.life -= 1 + self.elapsed_ms = 10000 + + def hit(self, player): + if self.players[player].hit(): + other = abs(player-1) + if not self.players[other].isHurt() and self.players[player].sprite.rect.colliderect(self.players[other].sprite.rect): + self.players[other].hurt(self.players[player].pos) diff --git a/minigames/avoid_the_guard/entities/__init__.py b/minigames/avoid_the_guard/entities/__init__.py new file mode 100644 index 0000000..ac2d8b2 --- /dev/null +++ b/minigames/avoid_the_guard/entities/__init__.py @@ -0,0 +1,4 @@ +from perso import Perso +from persoGuard import PersoGuard +from persoPlayer import PersoPlayer +from sprite import Sprite diff --git a/minigames/avoid_the_guard/entities/perso.py b/minigames/avoid_the_guard/entities/perso.py new file mode 100644 index 0000000..22883b8 --- /dev/null +++ b/minigames/avoid_the_guard/entities/perso.py @@ -0,0 +1,97 @@ +from sprite import Sprite + +class Perso: + def __init__(self, x, y, path, difficulty): + self.HIT_DURATION = 0.8 + self.HURT_DURATION = 0.8 + self.HURT_POWER = 300 + self.hitTime = 0 + self.hurtTime = 0 + + self.state = "normal" + self.sprite = Sprite(path) + self.sprite.rect.x = x + self.sprite.rect.y = y + + self.pos = [x,y] + self.speed = difficulty * 1.5 + 6 + self.life = 1 + + self.pushedVector = [0,0] + + def update(self, timeElapsed): + if self.state == "hit": + self.hitTime += timeElapsed + if self.hitTime >= self.HIT_DURATION: + self.normal() + self.hitTime = 0 + if self.state == "hurt": + self.hurtTime += timeElapsed + if self.hurtTime >= self.HURT_DURATION: + self.normal() + self.hurtTime = 0 + + self.updatePos(timeElapsed) + self.sprite.update(timeElapsed) + + def updatePos(self, timeElapsed): + if self.pos[0] < 25: + self.pos[0] = 25 + self.pushedVector[0] = -self.pushedVector[0] + elif self.pos[0] > 725: + self.pos[0] = 725 + self.pushedVector[0] = -self.pushedVector[0] + if self.pos[1] < 200: + self.pos[1] = 200 + self.pushedVector[1] = -self.pushedVector[1] + elif self.pos[1] > 525: + self.pos[1] = 525 + self.pushedVector[1] = -self.pushedVector[1] + + self.pos[0] = self.pos[0] + self.pushedVector[0] * timeElapsed / self.HURT_DURATION + self.pos[1] = self.pos[1] + self.pushedVector[1] * timeElapsed / self.HURT_DURATION + self.pushedVector[0] = self.pushedVector[0] - self.pushedVector[0] * timeElapsed / self.HURT_DURATION + self.pushedVector[1] = self.pushedVector[1] - self.pushedVector[1] * timeElapsed / self.HURT_DURATION + + def draw(self, screen): + self.sprite.draw(screen, self.pos) + + def move(self, direction): + if direction == "up": + self.pos[1] -= self.speed + elif direction == "left": + self.pos[0] -= self.speed + elif direction == "right": + self.pos[0] += self.speed + elif direction == "down": + self.pos[1] += self.speed + + if self.state != "hit" and self.state != "hurt": + self.sprite.changeSprite(direction) + + def normal(self): + self.state = "normal" + self.sprite.changeSprite("normal") + + def hit(self): + if self.state != "hit" and self.state != "hurt": + self.state = "hit" + self.sprite.changeSprite("action") + + return True + else: + return False + + def isHurt(self): + return self.state == "hurt" + + def hurt(self, pos): + newPos = [self.pos[0] - pos[0], self.pos[1] - pos[1]] + if newPos[0] == 0 and newPos[1] == 0: + newPos[0] = 1 + newPos[1] = 1 + max = abs(newPos[0]) if (abs(newPos[0]) > abs(newPos[1])) else abs(newPos[1]) + self.pushedVector = [self.HURT_POWER * newPos[0] / max, self.HURT_POWER * newPos[1] / max] + print self.pushedVector + self.state = "hurt" + self.sprite.changeSprite("action") diff --git a/minigames/avoid_the_guard/entities/persoGuard.py b/minigames/avoid_the_guard/entities/persoGuard.py new file mode 100644 index 0000000..69e5bf9 --- /dev/null +++ b/minigames/avoid_the_guard/entities/persoGuard.py @@ -0,0 +1,30 @@ +from perso import Perso +import random + +class PersoGuard(Perso): + def __init__(self, x, y, path, difficulty, players): + self.KEEP_DIR_TIME = 0.25 + + Perso.__init__(self, x, y, path, difficulty) + self.difficulty = difficulty + self.players = players + self.keepDirTime = 0.5 + self.dir = 0 + + def update(self, timeElapsed): + Perso.update(self, timeElapsed) + + self.keepDirTime += timeElapsed + + if self.keepDirTime >= self.KEEP_DIR_TIME: + self.keepDirTime = 0 + self.dir = random.randint(0,3) + + if self.dir == 0: + self.move("up") + elif self.dir == 1: + self.move("right") + elif self.dir == 2: + self.move("down") + elif self.dir == 3: + self.move("left") \ No newline at end of file diff --git a/minigames/avoid_the_guard/entities/persoPlayer.py b/minigames/avoid_the_guard/entities/persoPlayer.py new file mode 100644 index 0000000..4943814 --- /dev/null +++ b/minigames/avoid_the_guard/entities/persoPlayer.py @@ -0,0 +1,4 @@ +from perso import Perso + +class PersoPlayer(Perso): + pass diff --git a/minigames/avoid_the_guard/entities/sprite.py b/minigames/avoid_the_guard/entities/sprite.py new file mode 100644 index 0000000..c642ca7 --- /dev/null +++ b/minigames/avoid_the_guard/entities/sprite.py @@ -0,0 +1,43 @@ +import pygame + +class Sprite: + def __init__(self, path): + self.NB_SPRITE = 3 + self.NB_IMG_PER_SPRITE = 4 + self.UPDATE_TIME = 0.1 + self.updateTime = 0 + + self.path = path + self.image = pygame.image.load(path).convert_alpha() + self.flip = False + self.spriteRect = pygame.Rect(0, 0, self.image.get_rect().w / self.NB_IMG_PER_SPRITE, self.image.get_rect().h / self.NB_SPRITE) + self.rect = pygame.Rect(0, 0, self.image.get_rect().w / self.NB_IMG_PER_SPRITE, self.image.get_rect().h / self.NB_SPRITE) + + def update(self, elapsed): + if self.updateTime >= self.UPDATE_TIME: + self.updateTime = 0 + self.spriteRect.x += self.spriteRect.w + if self.spriteRect.x >= self.image.get_rect().w: + self.spriteRect.x = 0 + else: + self.updateTime += elapsed + + def changeSprite(self, sprite): + if sprite == "normal": + self.spriteRect.y = 0 * self.spriteRect.h + elif sprite == "left": + self.spriteRect.y = 1 * self.spriteRect.h + self.flip = True + elif sprite == "right": + self.spriteRect.y = 1 * self.spriteRect.h + self.flip = False + elif sprite == "action": + self.spriteRect.y = 2 * self.spriteRect.h + + def draw(self, screen, pos): + self.rect.x = pos[0] - self.rect.w/2 + self.rect.y = pos[1] - self.rect.h/2 + if self.flip: + screen.blit(pygame.transform.flip(self.image,1,0), self.rect, self.spriteRect) + else: + screen.blit(self.image, self.rect, self.spriteRect) diff --git a/minigames/stay_in_the_ring/stay_in_the_ring.py b/minigames/stay_in_the_ring/stay_in_the_ring.py index 606d21d..8f9a141 100644 --- a/minigames/stay_in_the_ring/stay_in_the_ring.py +++ b/minigames/stay_in_the_ring/stay_in_the_ring.py @@ -7,7 +7,7 @@ from entities import Ring class StayInTheRing(multiplayer.Minigame): - name = 'Stay in the dome!' + name = 'Knock him out of ring!' max_duration = 10000 def __init__(self, game): diff --git a/res/img/avoid_the_guard/Background.png b/res/img/avoid_the_guard/Background.png new file mode 100644 index 0000000..8dc36cf Binary files /dev/null and b/res/img/avoid_the_guard/Background.png differ diff --git a/res/img/avoid_the_guard/Guard.png b/res/img/avoid_the_guard/Guard.png new file mode 100644 index 0000000..528413a Binary files /dev/null and b/res/img/avoid_the_guard/Guard.png differ diff --git a/res/img/avoid_the_guard/Player1.png b/res/img/avoid_the_guard/Player1.png new file mode 100644 index 0000000..2e557a5 Binary files /dev/null and b/res/img/avoid_the_guard/Player1.png differ diff --git a/res/img/avoid_the_guard/Player2.png b/res/img/avoid_the_guard/Player2.png new file mode 100644 index 0000000..ff2fdcf Binary files /dev/null and b/res/img/avoid_the_guard/Player2.png differ