Introduced more randomness

master
David Raison 2014-08-30 21:20:42 +02:00
parent 85a79e07af
commit d954d98f2b
2 changed files with 59 additions and 27 deletions

View File

@ -5,47 +5,78 @@ from support import operations
class Avocado: class Avocado:
def __init__(self, screen, color, size, filename='img/AvoCado_0.png'): def __init__(self, screen, color, size, select, filename='img/AvoCado_0.png'):
# We randomly decide whether we should instanciate or not
if random.randint(0,1) == 0:
self.is_falling = False
return None
print('New avocado is ' + ','.join(str(color))) print('New avocado is ' + ','.join(str(color)))
self.screen = screen self.screen = screen
self.color = color
self.select = select
self.screen_width, self.screen_height = screen.get_size() self.screen_width, self.screen_height = screen.get_size()
self.x = random.randint(0, self.screen_width) self.x = random.randint(0, self.screen_width)
self.y = 0 # change this to start somewhere above the screen self.y = 0 # change this to start somewhere above the screen
self.w , self.y = size self.w , self.y = size
# Initialize the image
self.i = pygame.image.load(filename).convert_alpha() self.i = pygame.image.load(filename).convert_alpha()
operations.color_surface(self.i, color) operations.color_surface(self.i, color)
self.image = pygame.transform.scale(self.i, (self.w, self.y)) self.image = pygame.transform.scale(self.i, (self.w, self.y))
self.pycard = self.image.get_rect() self.rect = self.image.get_rect()
# Set the avocado's initial position and velocity
self.init_pos() self.init_pos()
self.step_x = 10 self.vx = 10
self.step_y = 10 self.vy = 10
self.is_falling = True self.is_falling = True
def blitme(self):
self.screen.blit(self.image, self.rect)
def init_pos(self): def init_pos(self):
self.pycard.x = random.randint(0, self.screen_width) self.rect.x = random.randint(0, self.screen_width)
self.pycard.y = random.randint(20, 70) self.rect.y = random.randint(20, 70)
def collides(self, click): def collides(self, click):
""" """
Checks whether this object collides with the given position Checks whether this object collides with the given position
in click in click
""" """
return True #if collision and …
#if collision then self.destroy() if self.color == self.select:
self.destroy()
return True
def destroy(self): def destroy(self):
"""destroys this object""" """destroys this object"""
del(self)
def exists(self):
return self.is_falling
def move(self): def move(self):
if self.pycard.right > self.screen_width or self.pycard.left < 0: if self.rect.right > self.screen_width or self.rect.left < 0:
self.step_x = -self.step_x self.vx = -self.vy
if self.pycard.bottom > self.screen_height or self.pycard.top < 0:
print('platch') if self.hasLanded():
return False self.destroy()
self.is_falling = False
self.pycard.x += self.step_x self.rect.x += self.vx
self.pycard.y += self.step_y self.rect.y += self.vy
return True 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

23
game.py
View File

@ -28,7 +28,7 @@ def main():
#size = initialize_screen() #size = initialize_screen()
size = (800, 600) size = (800, 600)
bg = BLACK bg = BLACK
desired_fps = 60 desired_fps = 10
font = pygame.font.Font(None, 40) font = pygame.font.Font(None, 40)
# I don't know, should we move this text out of the way? # I don't know, should we move this text out of the way?
@ -44,7 +44,7 @@ def main():
# Well, we want this to select between several colors, so we need a list # Well, we want this to select between several colors, so we need a list
# of colors, right? # of colors, right?
colors = [BLUE, GREEN, RED, YELLOW] colors = [BLUE, GREEN, RED, YELLOW]
selected = random.randint(0,3) selected = random.randint(0, 3)
color = colors[selected] color = colors[selected]
score = 0 score = 0
@ -77,18 +77,19 @@ def main():
the_hud.draw_hud(score, displaytime, round(fps, 2)) the_hud.draw_hud(score, displaytime, round(fps, 2))
# Initialize a number of avocados, depending on the level # Initialize a number of avocados, depending on the level
if len(avocados) != level: avocados_in_game = len(avocados)
avocados = [] print(avocados_in_game)
for i in range(0, level): if avocados_in_game != level:
color = colors[random.randint(0,3)] for i in range(avocados_in_game, level):
avosize = (60,60) # should we randomize this? avocolor = colors[random.randint(0, 3)]
a = avocado.Avocado(screen, color, avosize) avosize = (50, 50) # should we randomize this?
a = avocado.Avocado(screen, avocolor, avosize, color)
avocados.append(a) avocados.append(a)
avocados[:] = [ x for x in avocados if x.exists() ]
for a in avocados: for a in avocados:
if not a.move(): a.move()
a.init_pos() a.blitme()
screen.blit(a.image, a.pycard)
# Catch events # Catch events
for event in pygame.event.get(): for event in pygame.event.get():