Added colored pane

master
David Raison 2014-08-30 20:47:21 +02:00
parent b41c4a3180
commit 42652e0a34
4 changed files with 42 additions and 25 deletions

View File

@ -5,8 +5,9 @@ import random
class Avocado:
def __init__(self, screensize, filename='img/AvoCado_0.png'):
self.screen_width, self.screen_height = screensize
def __init__(self, screen, color, filename='img/AvoCado_0.png'):
print('New avocado is ' + ','.join(str(color)))
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 = 100
@ -43,7 +44,6 @@ class Avocado:
print('platch')
return False
self.is_falling = False
print(self.pycard.x, self.pycard.y)
self.pycard.x += self.step_x
self.pycard.y += self.step_y
return True

22
game.py
View File

@ -21,7 +21,7 @@ def initialize_screen():
def main():
pygame.init()
pygame.display.set_caption('Pin the Avocados!')
pygame.display.set_caption('Pin Avo, the Cado!')
clock = pygame.time.Clock()
# initialize_screen() won't work for dualscreen
@ -37,11 +37,14 @@ def main():
# initialize the game canvas
screen = pygame.display.set_mode(size)
# initialize the HUD class
my_hud = hud.Hud(size)
# initialize our lawyer
# 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
@ -55,7 +58,8 @@ def main():
fps = clock.get_fps()
screen.fill(bg)
# Let's add the lawyer
# Let's add the lawyer and have him announce a color
fullegast.setColor(color)
fullegast.blitme()
timeleft -= time_passed / 1000
@ -69,14 +73,14 @@ def main():
displaytime = timeleft
# Redraw the HUD
chud = my_hud.draw_hud(score, displaytime, round(fps, 2))
screen.blit(chud, (10, 10))
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):
a = avocado.Avocado(size)
color = colors[random.randint(0,3)]
a = avocado.Avocado(screen, color)
avocados.append(a)
for a in avocados:

View File

@ -7,19 +7,20 @@ from support import operations
class Hud:
def __init__(self, screensize):
self.screen_width, self.screen_height = screensize
def __init__(self, screen):
self.screen = screen
self.screen_width, self.screen_height = screen.get_size()
self.font = pygame.font.Font(None, 30)
self.screen = pygame.Surface((self.screen_width, self.screen_height / 8))
self.hud = pygame.Surface((self.screen_width, self.screen_height / 8))
def draw_hud(self, score, timeleft, fps):
self.screen.fill(BLACK)
self.screen.blit(self.draw_score(score), (0, 0))
self.screen.blit(self.draw_timeleft(timeleft), (self.screen_width / 2, 0))
self.hud.fill(BLACK)
self.hud.blit(self.draw_score(score), (0, 0))
self.hud.blit(self.draw_timeleft(timeleft), (self.screen_width / 2, 0))
thefps = self.draw_fps(fps)
fps_rect = thefps.get_rect()
self.screen.blit(thefps, (self.screen_width - fps_rect.w, 0))
return self.screen
self.hud.blit(thefps, (self.screen_width - fps_rect.w, 0))
self.blitme()
def draw_score(self, score):
score = self.font.render('Score: ' + str(score), 0, WHITE)
@ -38,3 +39,6 @@ class Hud:
def draw_fps(self, fps):
return self.font.render('fps: ' + str(fps), 10, RED)
def blitme(self):
self.screen.blit(self.hud, (10, 10))

View File

@ -14,14 +14,23 @@ class Lawyer:
screen_width, screen_height = screen.get_size()
temp_image = pygame.image.load(os.path.join('img', 'lawyer.png'))
# WARNING!! FIXME Absolute sizes FIXME
self.image = pygame.transform.scale(temp_image, (200, 400))
rect = self.image.get_rect()
self.pos = (screen_width - rect.w, screen_height - rect.h)
self.image = pygame.transform.scale(temp_image, (220, 400))
self.rect = self.image.get_rect()
self.pos = (screen_width - self.rect.w, screen_height - self.rect.h)
def blitme(self):
""" Blit this object to the screen """
operations.color_surface(self.image, WHITE)
self.image.blit(self.pane, (self.rect.left, self.rect.bottom / 2))
self.screen.blit(self.image, self.pos)
def announce(self):
""" Announces the color to pin """
def setColor(self, color):
""" Announces the color to pin by drawing a rectangle
and filling it with a color """
self.pane = pygame.Surface((200, 100))
self.pane.fill(color)
# Add this to self.image?!
# using surface.fill is faster because it can be hw accel
#pygame.draw.rect(Surface, color, Rect, width=0)