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:
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)))
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.pycard = self.image.get_rect()
self.rect = self.image.get_rect()
# Set the avocado's initial position and velocity
self.init_pos()
self.step_x = 10
self.step_y = 10
self.vx = 10
self.vy = 10
self.is_falling = True
def blitme(self):
self.screen.blit(self.image, self.rect)
def init_pos(self):
self.pycard.x = random.randint(0, self.screen_width)
self.pycard.y = random.randint(20, 70)
self.rect.x = random.randint(0, self.screen_width)
self.rect.y = random.randint(20, 70)
def collides(self, click):
"""
Checks whether this object collides with the given position
in click
"""
return True
#if collision then self.destroy()
#if collision and …
if self.color == self.select:
self.destroy()
return True
def destroy(self):
"""destroys this object"""
del(self)
def exists(self):
return self.is_falling
def move(self):
if self.pycard.right > self.screen_width or self.pycard.left < 0:
self.step_x = -self.step_x
if self.pycard.bottom > self.screen_height or self.pycard.top < 0:
print('platch')
return False
self.is_falling = False
self.pycard.x += self.step_x
self.pycard.y += self.step_y
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

23
game.py
View File

@ -28,7 +28,7 @@ def main():
#size = initialize_screen()
size = (800, 600)
bg = BLACK
desired_fps = 60
desired_fps = 10
font = pygame.font.Font(None, 40)
# 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
# of colors, right?
colors = [BLUE, GREEN, RED, YELLOW]
selected = random.randint(0,3)
selected = random.randint(0, 3)
color = colors[selected]
score = 0
@ -77,18 +77,19 @@ def main():
the_hud.draw_hud(score, displaytime, round(fps, 2))
# Initialize a number of avocados, depending on the level
if len(avocados) != level:
avocados = []
for i in range(0, level):
color = colors[random.randint(0,3)]
avosize = (60,60) # should we randomize this?
a = avocado.Avocado(screen, color, avosize)
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)
avocados.append(a)
avocados[:] = [ x for x in avocados if x.exists() ]
for a in avocados:
if not a.move():
a.init_pos()
screen.blit(a.image, a.pycard)
a.move()
a.blitme()
# Catch events
for event in pygame.event.get():