Merge branch 'master' of github.com:lvl2/avocados

Conflicts:
	avocado.py
master
Steve Clement 2014-08-31 12:12:57 +02:00
commit e7966ba408
4 changed files with 69 additions and 33 deletions

View File

@ -5,20 +5,22 @@ from support import operations
class Avocado:
def __init__(self, screen, color, size, target, level, filename=os.path.join('img', 'AvoCado_0_PINK.png')):
def __init__(self, screen, boundaries, properties, target, level, filename=os.path.join('img', 'AvoCado_0_PINK.png')):
# Set up our instance variables
self.screen = screen
self.color = color
self.screen_width, self.screen_height = screen.get_size()
self.avocados = {(255,0,0): os.path.join('img', 'AvoCado_0_RED.png'), \
(0,255,0): os.path.join('img', 'AvoCado_0_GREEN.png'), \
(0,0,255): os.path.join('img', 'AvoCado_0_BLUE.png'), \
(255,255,0): os.path.join('img', 'AvoCado_0_YELLOW.png'), \
(255,192,203): os.path.join('img', 'AvoCado_0_PINK.png')}
self.color = properties['color']
self.w, self.y = properties['size']
self.filename = self.avocados[self.color]
self.target = target
self.screen_width, self.screen_height = screen.get_size()
self.w, self.y = size
self.boundaries = boundaries
self.checkObstacle = True
# Initialize the image
self.i = pygame.image.load(filename).convert_alpha()
@ -88,9 +90,26 @@ class Avocado:
def checkBoundaries(self):
# Checking screen boundaries
if self.rect.right > self.screen_width or self.rect.left < 0:
self.checkObstacle = True
self.vx = -self.vx
# Checking for obstacle collisions
for obstacle in self.boundaries:
left, top, width, height = obstacle
right = left + width
bottom = top + height
if self.checkObstacle \
and (self.rect.right < right and self.rect.left > left):
self.checkObstacle = False
if self.checkObstacle \
and ((self.rect.right > right and self.rect.left < right) \
or (self.rect.left < left and self.rect.right > left)):
self.vx = -self.vx
def move(self):
if not self.has_been_pinned:
@ -105,9 +124,9 @@ class Avocado:
def hasLanded(self):
if self.rect.bottom > self.screen_height or self.rect.top < 0:
if self.rect.top > self.screen_height:
self.is_still_falling = False
print('DEBUG :: splash!')
print('DEBUG :: splatch!')
return True

View File

@ -12,11 +12,18 @@ class Crystal:
def __init__(self, screen):
self.screen = screen
screen_width, screen_height = screen.get_size()
self.imageCenterX = self.screen.get_rect().centerx
self.imageCenterY = self.screen.get_rect().centery
self.rect = self.screen.get_rect()
self.imageCenterX = self.rect.centerx
self.imageCenterY = self.rect.centery
self.pos = (self.imageCenterX-100,self.imageCenterY-5,200,183)
def getBoundaries(self):
return self.pos
def blitme(self):
pygame.draw.ellipse(self.screen, self.color, (self.imageCenterX-100,self.imageCenterY-5,200,183), 0)
pygame.draw.ellipse(self.screen, self.color, self.pos, 0)
def setColor(self, color):
self.color = color

56
game.py
View File

@ -24,11 +24,19 @@ class TheGame:
self.timeout = 30
self.level = 1
self.targetScore = 400
##############################
# Never set below 4, else we have a high
# probability of losing the game due to a missing color
# Alternatively, you could edit chooseRandomColor()
# to only work on the first multiplier colors
self.multiplier = 4
self.desired_fps = 60
self.screen = pygame.display.set_mode((self.WIDTH, self.HEIGHT))
self.colors = [RED, GREEN, BLUE, YELLOW, PINK]
self.bg = pygame.image.load(os.path.join('img', 'lawyerCrystalBall.png'))
self.bg.set_colorkey((255,255,255))
self.bg.set_alpha(75)
self.last_colors = []
# fonts
self.bigFont = pygame.font.Font(None, 90)
@ -93,9 +101,16 @@ class TheGame:
def chooseRandomColor(self):
selected = random.randint(0, 3)
if len(self.last_colors) > 5:
self.last_colors.pop(0)
for i in range(0, 5):
if selected in self.last_colors:
selected = random.randint(0, 3)
else:
break
self.last_colors.append(selected)
return self.colors[selected]
def gameOver(self):
screen_width, screen_height = self.screen.get_size()
gameOverImage = pygame.image.load(os.path.join('img', 'gameOver.png'))
@ -134,23 +149,17 @@ class TheGame:
def main(self):
desired_fps = 60
##############################
# Never set below 4, else we have a high
# probability of losing the game due to a missing color
# Alternatively, you could edit chooseRandomColor()
# to only work on the first multiplier colors
multiplier = 4
score = 0
boundaries = []
# We could use this list for redrawing only this part
# of the screen install of all of it
# of the screen instead of all of it
self.resetLevel()
# initialize the HUD class and the lawyer
the_hud = hud.Hud(self.screen)
crystalBall = crystal.Crystal(self.screen)
boundaries.append(crystalBall.getBoundaries())
# Initial color indication
color = self.chooseRandomColor()
@ -158,19 +167,13 @@ class TheGame:
texts = []
container = {'font': self.bigFont, 'screen': self.screen, 'clock': self.clock}
# onetext = itext.Text(container, 'Huhu', 2000)
# texts.append(onetext)
running = True
while running:
time_passed = self.clock.tick(desired_fps)
time_passed = self.clock.tick(self.desired_fps)
fps = self.clock.get_fps()
screen_width, screen_height = self.screen.get_size()
# Redraw the background and put our lawyer back on top
self.drawBackground()
crystalBall.blitme()
# Next level?
if score >= (self.targetScore * self.level):
self.level += 1
@ -181,7 +184,6 @@ class TheGame:
2000
)
texts.append(levelText)
# self.screen.blit(levelText, (screen_width / 3, screen_height / 2))
self.playLevel(self.level)
self.resetLevel()
@ -194,26 +196,34 @@ class TheGame:
displaytime = self.timeleft
# Redraw the background and put our lawyer back on top
self.drawBackground()
crystalBall.blitme()
# Check if there's any text that wants to get displayed
for text in texts:
text.blitme()
texts[:] = [text for text in texts if not text.hasExpired() ]
# Redraw the HUD
the_hud.draw_hud(score, displaytime, round(fps, 2))
# Initialize a number of avocados, depending on the level
avocadosInGame = len(self.movingAvocados)
avocadosWanted = self.level * multiplier
avocadosWanted = self.level * self.multiplier
if avocadosInGame < avocadosWanted:
probability = int(1.0/(avocadosWanted - avocadosInGame) * 100)
if random.randint(0, probability) < 3:
avocolor = self.chooseRandomColor()
avosize = (50, 50) # should we randomize this?
properties = {'color': self.chooseRandomColor(), 'size': (50,50)}
# Spawn a new avocado
a = avocado.Avocado(self.screen, avocolor, avosize, color, self.level)
a = avocado.Avocado(
self.screen,
boundaries,
properties,
color,
self.level
)
self.movingAvocados.append(a)
# Remove avocados from the list of moving avocados if they no longer move

View File

@ -19,7 +19,7 @@ class Hud:
self.hud.blit(self.draw_timeleft(timeleft), (self.screen_width / 2, 0))
thefps = self.draw_fps(fps)
fps_rect = thefps.get_rect()
self.hud.blit(thefps, (self.screen_width - fps_rect.w, 0))
self.hud.blit(thefps, (self.screen_width - (fps_rect.w + 5), 0))
self.blitme()
def draw_score(self, score):