fixed color setting, changing color when correct one was clicked
commit
3ff7fac4dc
|
@ -0,0 +1,127 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
import pygame, random
|
||||||
|
from support import operations
|
||||||
|
|
||||||
|
class Avocado:
|
||||||
|
|
||||||
|
def __init__(self, screen, color, size, target, sound=True, filename='img/AvoCado_0.png'):
|
||||||
|
|
||||||
|
# HELP please!!
|
||||||
|
# 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
|
||||||
|
|
||||||
|
# Set up our instance variables
|
||||||
|
self.screen = screen
|
||||||
|
self.color = color
|
||||||
|
self.target = target
|
||||||
|
self.screen_width, self.screen_height = screen.get_size()
|
||||||
|
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 updateTargetColor(self, targetColor):
|
||||||
|
self.target = targetColor
|
||||||
|
|
||||||
|
|
||||||
|
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. Return true is the correct color was hit and
|
||||||
|
false if it was the wrong one.
|
||||||
|
Just returns void if no avocado was hit
|
||||||
|
"""
|
||||||
|
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.target:
|
||||||
|
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)
|
15
game.py
15
game.py
|
@ -21,7 +21,6 @@ class TheGame:
|
||||||
def initialize_screen(self):
|
def initialize_screen(self):
|
||||||
displayInfo = pygame.display.Info()
|
displayInfo = pygame.display.Info()
|
||||||
zoom = 1.3
|
zoom = 1.3
|
||||||
|
|
||||||
WIDTH = int(displayInfo.current_w / zoom)
|
WIDTH = int(displayInfo.current_w / zoom)
|
||||||
HEIGHT = int(displayInfo.current_h / zoom)
|
HEIGHT = int(displayInfo.current_h / zoom)
|
||||||
return (WIDTH, HEIGHT)
|
return (WIDTH, HEIGHT)
|
||||||
|
@ -59,15 +58,16 @@ class TheGame:
|
||||||
|
|
||||||
# Initial color
|
# Initial color
|
||||||
color = self.chooseRandomColor()
|
color = self.chooseRandomColor()
|
||||||
|
fullegast.setColor(color)
|
||||||
|
|
||||||
score = 0
|
score = 0
|
||||||
time = 15
|
time = timeleft = 15
|
||||||
level = 5
|
level = 5
|
||||||
|
|
||||||
running = True
|
|
||||||
timeleft = time
|
|
||||||
avocados = []
|
avocados = []
|
||||||
|
running = True
|
||||||
while running:
|
while running:
|
||||||
|
|
||||||
time_passed = clock.tick(desired_fps)
|
time_passed = clock.tick(desired_fps)
|
||||||
fps = clock.get_fps()
|
fps = clock.get_fps()
|
||||||
if type(bg) is tuple:
|
if type(bg) is tuple:
|
||||||
|
@ -75,8 +75,7 @@ class TheGame:
|
||||||
else:
|
else:
|
||||||
screen.blit(pygame.transform.scale(bg,(800,600)),(0,0))
|
screen.blit(pygame.transform.scale(bg,(800,600)),(0,0))
|
||||||
|
|
||||||
# Let's add the lawyer and have him announce a color
|
# Let's add the lawyer
|
||||||
fullegast.setColor(color)
|
|
||||||
fullegast.blitme()
|
fullegast.blitme()
|
||||||
|
|
||||||
timeleft -= time_passed / 1000
|
timeleft -= time_passed / 1000
|
||||||
|
@ -95,7 +94,6 @@ class TheGame:
|
||||||
|
|
||||||
# Initialize a number of avocados, depending on the level
|
# Initialize a number of avocados, depending on the level
|
||||||
avocados_in_game = len(avocados)
|
avocados_in_game = len(avocados)
|
||||||
print(avocados_in_game)
|
|
||||||
if avocados_in_game != level:
|
if avocados_in_game != level:
|
||||||
for i in range(avocados_in_game, level):
|
for i in range(avocados_in_game, level):
|
||||||
avocolor = self.chooseRandomColor()
|
avocolor = self.chooseRandomColor()
|
||||||
|
@ -107,6 +105,7 @@ class TheGame:
|
||||||
# E.g. have been pinned or fallen down
|
# E.g. have been pinned or fallen down
|
||||||
avocados[:] = [ x for x in avocados if x.exists() ]
|
avocados[:] = [ x for x in avocados if x.exists() ]
|
||||||
for a in avocados:
|
for a in avocados:
|
||||||
|
a.updateTargetColor(color)
|
||||||
a.move()
|
a.move()
|
||||||
a.blitme()
|
a.blitme()
|
||||||
|
|
||||||
|
@ -118,6 +117,8 @@ class TheGame:
|
||||||
hit = avo.isHit(pygame.mouse.get_pos())
|
hit = avo.isHit(pygame.mouse.get_pos())
|
||||||
if hit:
|
if hit:
|
||||||
score += 100
|
score += 100
|
||||||
|
color = self.chooseRandomColor()
|
||||||
|
fullegast.setColor(color)
|
||||||
elif hit == False:
|
elif hit == False:
|
||||||
score -= 50
|
score -= 50
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue