From 13786f52cb0bcb3c44ee390fb2e440ca07882b87 Mon Sep 17 00:00:00 2001 From: David Raison Date: Sun, 31 Aug 2014 10:29:04 +0200 Subject: [PATCH 1/4] tiny improvements --- avocado.py | 4 ++-- game.py | 43 ++++++++++++++++++++++--------------------- interface/hud.py | 2 +- 3 files changed, 25 insertions(+), 24 deletions(-) diff --git a/avocado.py b/avocado.py index 99600f7..787efbb 100644 --- a/avocado.py +++ b/avocado.py @@ -98,9 +98,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 diff --git a/game.py b/game.py index 0ce394a..0c12aa7 100755 --- a/game.py +++ b/game.py @@ -24,6 +24,13 @@ 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 = [BLUE, GREEN, RED, YELLOW] self.bg = pygame.image.load(os.path.join('img', 'lawyerCrystalBall.png')) @@ -95,7 +102,7 @@ class TheGame: def gameOver(self): - screen_width, screen_height = self.screen.get_size + screen_width, screen_height = self.screen.get_size() gameOverImage = pygame.image.load("img/gameOver.png") gameOverText = self.bigFont.render('GAME OVER', 0, YELLOW) gameOverImage.blit(gameOverText, (screen_width/8, screen_height/7)) @@ -131,18 +138,10 @@ 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 # 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 @@ -155,19 +154,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 @@ -178,7 +171,6 @@ class TheGame: 2000 ) texts.append(levelText) - # self.screen.blit(levelText, (screen_width / 3, screen_height / 2)) self.playLevel(self.level) self.resetLevel() @@ -191,18 +183,21 @@ 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) @@ -210,7 +205,13 @@ class TheGame: avocolor = self.chooseRandomColor() avosize = (50, 50) # should we randomize this? # Spawn a new avocado - a = avocado.Avocado(self.screen, avocolor, avosize, color, self.level) + a = avocado.Avocado( + self.screen, + avocolor, + avosize, + color, + self.level + ) self.movingAvocados.append(a) # Remove avocados from the list of moving avocados if they no longer move diff --git a/interface/hud.py b/interface/hud.py index 03cc94f..6031e9a 100644 --- a/interface/hud.py +++ b/interface/hud.py @@ -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): From b702494c6520ee9efcd364db1805e0896631df47 Mon Sep 17 00:00:00 2001 From: David Raison Date: Sun, 31 Aug 2014 11:48:20 +0200 Subject: [PATCH 2/4] Do collision detection on the glassball --- avocado.py | 27 +++++++++++++++++++++++---- crystal.py | 13 ++++++++++--- game.py | 9 +++++---- 3 files changed, 38 insertions(+), 11 deletions(-) diff --git a/avocado.py b/avocado.py index 177aef4..b334f3f 100644 --- a/avocado.py +++ b/avocado.py @@ -5,14 +5,16 @@ from support import operations class Avocado: - def __init__(self, screen, color, size, target, level, filename='img/AvoCado_0.png'): + def __init__(self, screen, boundaries, properties, target, level, filename='img/AvoCado_0.png'): # 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 + color = properties['color'] + self.w, self.y = properties['size'] + self.target = target + self.boundaries = boundaries + self.checkObstacle = True # Initialize the image self.i = pygame.image.load(filename).convert_alpha() @@ -81,9 +83,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: diff --git a/crystal.py b/crystal.py index 8a00f4a..acb792f 100644 --- a/crystal.py +++ b/crystal.py @@ -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 diff --git a/game.py b/game.py index 16f8f7e..0c56d28 100755 --- a/game.py +++ b/game.py @@ -140,6 +140,7 @@ class TheGame: def main(self): score = 0 + boundaries = [] # We could use this list for redrawing only this part # of the screen instead of all of it @@ -148,6 +149,7 @@ class TheGame: # 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() @@ -203,13 +205,12 @@ class TheGame: 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, + boundaries, + properties, color, self.level ) From b1f54e9e7c16394176c7694d0b216e5dbe82d3a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Vinot?= Date: Sun, 31 Aug 2014 11:49:02 +0200 Subject: [PATCH 3/4] Trick the random generator... --- game.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/game.py b/game.py index 291feed..b307862 100755 --- a/game.py +++ b/game.py @@ -29,6 +29,7 @@ class TheGame: 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 +94,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("img/gameOver.png") From e98f94575b43f9fdbef32e3134234b424e092d58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Vinot?= Date: Sun, 31 Aug 2014 11:52:40 +0200 Subject: [PATCH 4/4] Fix issue wit color --- avocado.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/avocado.py b/avocado.py index bc34929..defcc3f 100644 --- a/avocado.py +++ b/avocado.py @@ -10,7 +10,7 @@ class Avocado: # Set up our instance variables self.screen = screen self.screen_width, self.screen_height = screen.get_size() - color = properties['color'] + self.color = properties['color'] self.w, self.y = properties['size'] self.target = target self.boundaries = boundaries @@ -18,7 +18,7 @@ class Avocado: # Initialize the image self.i = pygame.image.load(filename).convert_alpha() - operations.color_surface(self.i, color) + operations.color_surface(self.i, self.color) self.image = pygame.transform.scale(self.i, (self.w, self.y)) self.rect = self.image.get_rect()