Objectified the game

master
David Raison 2014-08-30 22:16:41 +02:00
parent a2e21a6be1
commit e060f746b2
2 changed files with 112 additions and 222 deletions

View File

@ -1,119 +0,0 @@
#!/usr/bin/env python3
import pygame, random
from support import operations
class Avocado:
def __init__(self, screen, color, size, select, sound=True, filename='img/AvoCado_0.png'):
# We randomly decide whether we should instanciate or not
# I'd rather just not return an instance,
# but I don't know how to do that :(
if random.randint(0,1) == 0:
self.is_falling = False
self.has_been_pinned = False
return None
print('New avocado is ' + ','.join(str(color)))
self.screen = screen
self.color = color
self.select = select
self.screen_width, self.screen_height = screen.get_size()
self.x = random.randint(0, self.screen_width)
self.y = 0 # change this to start somewhere above the screen
self.w , self.y = size
# Initialize the image
self.i = pygame.image.load(filename).convert_alpha()
operations.color_surface(self.i, color)
self.image = pygame.transform.scale(self.i, (self.w, self.y))
self.rect = self.image.get_rect()
# Set the avocado's initial position and velocity
self.init_pos()
self.vx = 10
self.vy = 10
self.is_falling = True
self.has_been_pinned = False
def blitme(self):
self.screen.blit(self.image, self.rect)
def init_pos(self):
self.rect.x = random.randint(0, self.screen_width)
self.rect.y = random.randint(20, 70)
def isHit(self, click):
"""
Checks whether this object collides with the given position
of a mouse-click
"""
mousex, mousey = click
if self.rect.left < mousex and self.rect.right > mousex and \
self.rect.top < mousey and self.rect.bottom > mousey:
if self.color == self.select:
self.has_been_pinned = True
return True
else:
return False
def exists(self):
return not self.has_been_pinned and self.is_falling
def move(self):
if self.rect.right > self.screen_width or self.rect.left < 0:
self.vx = -self.vy
if self.hasLanded():
self.destroy()
self.rect.x += self.vx
self.rect.y += self.vy
return True
def hasLanded(self):
if self.rect.bottom > self.screen_height or self.rect.top < 0:
self.is_falling = False
print('platch')
return True
def mute(self,mute=False):
if not sound:
return
if mute:
pygame.mixer.music.set_volume(0.0)
else:
pygame.mixer.music.set_volume(0.5)
def playLevel(self,lvl=1):
if not sound:
return
if lvl == 1:
pygame.mixer.music.load("""audio/level1.wav""")
elif lvl == 2:
pygame.mixer.music.load("""audio/level2.wav""")
elif lvl == 3:
pygame.mixer.music.load("""audio/level3.wav""")
pygame.mixer.music.play()
def fade(self):
if not sound:
return
pygame.mixer.music.fadeout(3000)
def loadClick(self):
if not sound:
return
self.click = pygame.mixer.Sound("audio/click.wav")
return self.click
def destroy(self):
del(self)

215
game.py
View File

@ -11,119 +11,128 @@ from support.colors import *
from interface import hud
def initialize_screen():
displayInfo = pygame.display.Info()
zoom = 1.3
class TheGame:
WIDTH = int(displayInfo.current_w / zoom)
HEIGHT = int(displayInfo.current_h / zoom)
return (WIDTH, HEIGHT)
def __init__(self):
""" foo """
self.colors = [BLUE, GREEN, RED, YELLOW]
def main():
pygame.init()
try:
pygame.mixer.init()
pygame.mixer.music.set_volume(0.5)
noSound = False
except:
print("Setting no sound :(")
noSound = True
def initialize_screen(self):
displayInfo = pygame.display.Info()
zoom = 1.3
pygame.display.set_caption('Pin Avo, the Cado!')
clock = pygame.time.Clock()
# initialize_screen() won't work for dualscreen
#size = initialize_screen()
size = (800, 600)
bg = pygame.image.load("img/background.png")
desired_fps = 10
font = pygame.font.Font(None, 40)
# I don't know, should we move this text out of the way?
game_over = font.render('GAME OVER', 0, RED)
# initialize the game canvas
screen = pygame.display.set_mode(size)
# initialize the HUD class and the lawyer
the_hud = hud.Hud(screen)
fullegast = lawyer.Lawyer(screen)
# Well, we want this to select between several colors, so we need a list
# of colors, right?
colors = [BLUE, GREEN, RED, YELLOW]
selected = random.randint(0, 3)
color = colors[selected]
score = 0
time = 15
level = 5
running = True
timeleft = time
avocados = []
while running:
time_passed = clock.tick(desired_fps)
fps = clock.get_fps()
if type(bg) is tuple:
screen.fill(bg)
else:
screen.blit(pygame.transform.scale(bg,(800,600)),(0,0))
WIDTH = int(displayInfo.current_w / zoom)
HEIGHT = int(displayInfo.current_h / zoom)
return (WIDTH, HEIGHT)
# Let's add the lawyer and have him announce a color
fullegast.setColor(color)
fullegast.blitme()
def main(self):
pygame.init()
try:
pygame.mixer.init()
pygame.mixer.music.set_volume(0.5)
noSound = False
except:
print("Y U NO sound? :(")
noSound = True
pygame.display.set_caption('Pin Avo, the Cado!')
clock = pygame.time.Clock()
# initialize_screen() won't work for dualscreen
#size = initialize_screen()
size = (800, 600)
bg = pygame.image.load("img/background.png")
desired_fps = 10
font = pygame.font.Font(None, 40)
# I don't know, should we move this text out of the way?
game_over = font.render('GAME OVER', 0, RED)
# initialize the game canvas
screen = pygame.display.set_mode(size)
# initialize the HUD class and the lawyer
the_hud = hud.Hud(screen)
fullegast = lawyer.Lawyer(screen)
# Initial color
color = self.chooseRandomColor()
score = 0
time = 15
level = 5
running = True
timeleft = time
avocados = []
while running:
time_passed = clock.tick(desired_fps)
fps = clock.get_fps()
if type(bg) is tuple:
screen.fill(bg)
else:
screen.blit(pygame.transform.scale(bg,(800,600)),(0,0))
# Let's add the lawyer and have him announce a color
fullegast.setColor(color)
fullegast.blitme()
timeleft -= time_passed / 1000
timeleft = round(timeleft, 2)
if timeleft <= 0:
screen_width, screen_height = size
screen.blit(game_over, (screen_width/3, screen_height/2))
displaytime = 'Timed out!'
pygame.display.flip()
continue
else:
displaytime = timeleft
# Redraw the HUD
the_hud.draw_hud(score, displaytime, round(fps, 2))
# Initialize a number of avocados, depending on the level
avocados_in_game = len(avocados)
print(avocados_in_game)
if avocados_in_game != level:
for i in range(avocados_in_game, level):
avocolor = self.chooseRandomColor()
avosize = (50, 50) # should we randomize this?
a = avocado.Avocado(screen, avocolor, avosize, color, noSound)
avocados.append(a)
# Remove avocados from the list if they no longer exist
# E.g. have been pinned or fallen down
avocados[:] = [ x for x in avocados if x.exists() ]
for a in avocados:
a.move()
a.blitme()
# Catch events
for event in pygame.event.get():
# Collision detection
if event.type == MOUSEBUTTONDOWN:
for avo in avocados:
hit = avo.isHit(pygame.mouse.get_pos())
if hit:
score += 100
elif hit == False:
score -= 50
# Had enough of this?
if event.type == pygame.QUIT:
running = False
timeleft -= time_passed / 1000
timeleft = round(timeleft, 2)
if timeleft <= 0:
screen_width, screen_height = size
screen.blit(game_over, (screen_width/3, screen_height/2))
displaytime = 'Timed out!'
pygame.display.flip()
continue
else:
displaytime = timeleft
# Redraw the HUD
the_hud.draw_hud(score, displaytime, round(fps, 2))
# Initialize a number of avocados, depending on the level
avocados_in_game = len(avocados)
print(avocados_in_game)
if avocados_in_game != level:
for i in range(avocados_in_game, level):
avocolor = colors[random.randint(0, 3)]
avosize = (50, 50) # should we randomize this?
a = avocado.Avocado(screen, avocolor, avosize, color, noSound)
avocados.append(a)
# Remove avocados from the list if they no longer exist
# E.g. have been pinned or fallen down
avocados[:] = [ x for x in avocados if x.exists() ]
for a in avocados:
a.move()
a.blitme()
# Catch events
for event in pygame.event.get():
# Collision detection
if event.type == MOUSEBUTTONDOWN:
for avo in avocados:
hit = avo.isHit(pygame.mouse.get_pos())
if hit:
score += 100
elif hit == False:
score -= 50
# Had enough of this?
if event.type == pygame.QUIT:
running = False
pygame.display.flip()
def chooseRandomColor(self):
selected = random.randint(0, 3)
return self.colors[selected]
if __name__ == '__main__':
main()
game = TheGame()
game.main()