Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding new minigame: Avoid the guard #31

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions minigames/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 1 addition & 0 deletions minigames/avoid_the_guard/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from avoid_the_guard import AvoidTheGuard
95 changes: 95 additions & 0 deletions minigames/avoid_the_guard/avoid_the_guard.py
Original file line number Diff line number Diff line change
@@ -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)
4 changes: 4 additions & 0 deletions minigames/avoid_the_guard/entities/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from perso import Perso
from persoGuard import PersoGuard
from persoPlayer import PersoPlayer
from sprite import Sprite
97 changes: 97 additions & 0 deletions minigames/avoid_the_guard/entities/perso.py
Original file line number Diff line number Diff line change
@@ -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")
30 changes: 30 additions & 0 deletions minigames/avoid_the_guard/entities/persoGuard.py
Original file line number Diff line number Diff line change
@@ -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")
4 changes: 4 additions & 0 deletions minigames/avoid_the_guard/entities/persoPlayer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from perso import Perso

class PersoPlayer(Perso):
pass
43 changes: 43 additions & 0 deletions minigames/avoid_the_guard/entities/sprite.py
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 1 addition & 1 deletion minigames/stay_in_the_ring/stay_in_the_ring.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Binary file added res/img/avoid_the_guard/Background.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/img/avoid_the_guard/Guard.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/img/avoid_the_guard/Player1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/img/avoid_the_guard/Player2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.