From a6b33598562862789d597383d49ba0c521f429ad Mon Sep 17 00:00:00 2001 From: David Raison Date: Sun, 31 Aug 2014 10:06:22 +0200 Subject: [PATCH 1/4] Introduced self-drawing text --- game.py | 196 ++++++++++++++++++++++++++++-------------------- pingenerator.py | 12 +-- 2 files changed, 119 insertions(+), 89 deletions(-) diff --git a/game.py b/game.py index 9ca2342..0ce394a 100755 --- a/game.py +++ b/game.py @@ -5,7 +5,7 @@ Avocados and stuff import os, random, sys import pygame -import avocado, crystal, pingenerator +import avocado, crystal, pingenerator, itext from pygame.locals import * from support.colors import * from interface import hud @@ -17,18 +17,14 @@ class TheGame: """ Initialize the game """ pygame.init() pygame.display.set_caption('Pin Avo, the Cado!') - - displayInfo = pygame.display.Info() - self.resize = 1.3 - - self.WIDTH = int(displayInfo.current_w / self.resize) - self.HEIGHT = int(displayInfo.current_h / self.resize) - - self.WIDTH, self.HEIGHT = 800, 600 + self.clock = pygame.time.Clock() + self.initializeScreen() # initialize the game canvas - self.size = (self.WIDTH, self.HEIGHT) - self.screen = pygame.display.set_mode(self.size) + self.timeout = 30 + self.level = 1 + self.targetScore = 400 + 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')) @@ -37,7 +33,13 @@ class TheGame: # Set splashscreen splashScreen = pygame.image.load("img/splashScreen.png") - self.screen.blit(pygame.transform.scale(splashScreen, self.size), (0, 0)) + self.screen.blit( + pygame.transform.scale( + splashScreen, (self.WIDTH, self.HEIGHT) + ), + (0, 0) + ) + pygame.display.flip() pygame.time.wait(3000) @@ -50,6 +52,17 @@ class TheGame: sound = False + def initializeScreen(self): + # displayInfo = pygame.display.Info() + # self.resize = 1.3 + + # self.WIDTH = int(displayInfo.current_w / self.resize) + # self.HEIGHT = int(displayInfo.current_h / self.resize) + + # Look at the cleverness ;) + self.WIDTH, self.HEIGHT = 800, 600 + + def mute(self,mute=False, sound=True): if not sound: return @@ -82,7 +95,7 @@ class TheGame: def gameOver(self): - screen_width, screen_height = self.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)) @@ -102,28 +115,35 @@ class TheGame: if type(self.bg) is tuple: self.screen.fill(self.bg) else: - self.screen.blit(pygame.transform.scale(self.bg, self.size), (0, 0)) + self.screen.blit( + pygame.transform.scale( + self.bg, self.screen.get_size() + ), + (0, 0) + ) + + + def resetLevel(self): + self.pinnedAvocados = [] + self.movingAvocados = [] + self.thrownPins = [] + self.timeleft = self.timeout def main(self): - clock = pygame.time.Clock() 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 - time = timeleft = 30 - level = 1 - levelChange = 0 score = 0 - targetScore = 400 # We could use this list for redrawing only this part # of the screen install of all of it - pinnedAvocados = [] - movingAvocados = [] - thrownPins = [] + self.resetLevel() # initialize the HUD class and the lawyer the_hud = hud.Hud(self.screen) @@ -133,85 +153,95 @@ class TheGame: color = self.chooseRandomColor() crystalBall.setColor(color) + 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 = clock.tick(desired_fps) - fps = clock.get_fps() - screen_width, screen_height = self.size + time_passed = self.clock.tick(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 >= (targetScore * level): - level += 1 - levelChange = 70 - timeleft = time - print('DEBUG :: Score: ' + str(score)) - print('DEBUG :: Level ' + str(level)) - self.playLevel(level) - pinnedAvocados = [] - movingAvocados = [] - thrownPins = [] + if score >= (self.targetScore * self.level): + self.level += 1 + levelText = itext.Text( + container, + 'Level ' + str(self.level), + (screen_width / 3, screen_height /2), + 2000 + ) + texts.append(levelText) + # self.screen.blit(levelText, (screen_width / 3, screen_height / 2)) + self.playLevel(self.level) + self.resetLevel() - if levelChange > 0: - levelText = self.bigFont.render('Level ' + str(level), 0, WHITE) - self.screen.blit(levelText, (screen_width / 3, screen_height / 2)) - levelChange -= 1 - timeleft -= time_passed / 1000 - timeleft = round(timeleft, 2) - if timeleft <= 0: + self.timeleft -= time_passed / 1000 + self.timeleft = round(self.timeleft, 2) + if self.timeleft <= 0: self.gameOver() else: - displaytime = timeleft + displaytime = self.timeleft + + + # 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)) - # If we're not currently in between levels… - if levelChange == 0: - # Initialize a number of avocados, depending on the level - avocadosInGame = len(movingAvocados) - avocadosWanted = level * 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? - a = avocado.Avocado(self.screen, avocolor, avosize, color, level) - movingAvocados.append(a) + # Initialize a number of avocados, depending on the level + avocadosInGame = len(self.movingAvocados) + avocadosWanted = self.level * multiplier - # Remove avocados from the list of moving avocados if they no longer move - # Or add them to the list of pinned avocados if they're been hit - pinnedAvocados += [avo for avo in movingAvocados if avo.isPinned() ] - movingAvocados[:] = [ avo for avo in movingAvocados if avo.isFalling() ] + 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? + # Spawn a new avocado + a = avocado.Avocado(self.screen, avocolor, avosize, color, self.level) + self.movingAvocados.append(a) - ############################## - # - # Late-Night-Comments: - # - # Can we maybe handle the pinned avocados the same way I handle "stuck" - # pins? It seems to be easier.. well, the pins don't fall out of the screen - # though… - # - ############################## + # Remove avocados from the list of moving avocados if they no longer move + # Or add them to the list of pinned avocados if they're been hit + self.pinnedAvocados += [avo for avo in self.movingAvocados if avo.isPinned() ] + self.movingAvocados[:] = [ avo for avo in self.movingAvocados if avo.isFalling() ] - # Now redraw our avocados - for a in movingAvocados: - a.setTargetColor(color) - a.move() - a.blitme() + ############################## + # + # Late-Night-Comments: + # + # Can we maybe handle the pinned avocados the same way I handle "stuck" + # pins? It seems to be easier.. well, the pins don't fall out of the screen + # though… + # + ############################## - for a in pinnedAvocados: - a.blitme() + # Now redraw our avocados + for a in self.movingAvocados: + a.setTargetColor(color) + a.move() + a.blitme() - # And finally check if we need to redraw any pins - for activePin in thrownPins: - activePin.blitme() - if not activePin.isStuck(): - activePin.moveTowardsTarget() + for a in self.pinnedAvocados: + a.blitme() + + # And finally check if we need to redraw any pins + for activePin in self.thrownPins: + activePin.blitme() + if not activePin.isStuck(): + activePin.moveTowardsTarget() # Catch events for event in pygame.event.get(): @@ -219,13 +249,13 @@ class TheGame: if event.type == MOUSEBUTTONDOWN: mousepos = pygame.mouse.get_pos() - # Throw a pin here + # Throwing a new pin newPin = pingenerator.Generate(self.screen) newPin.throwAt(mousepos) - thrownPins.append(newPin) + self.thrownPins.append(newPin) # Check if any avocados have been hit - for avo in movingAvocados: + for avo in self.movingAvocados: hit = avo.isHit(mousepos) if hit: score += 100 diff --git a/pingenerator.py b/pingenerator.py index 610365d..18bb84c 100644 --- a/pingenerator.py +++ b/pingenerator.py @@ -22,7 +22,7 @@ class Generate: def isStuck(self): if self.pos == self.target: - print('Pin stuck!') + # print('Pin stuck!') return True @@ -70,9 +70,9 @@ class Generate: newy = y + ystep - print('DEBUG :: pin target: ' + str(tx) + ',' + str(ty)) - print('DEBUG :: pin position: ' + str(newx) + ',' + str(newy)) - print('DEBUG :: pin distance: ' + str(xToCover) + ',' + str(yToCover)) - print('DEBUG :: pin speed: ' + str(self.vx) + ',' + str(self.vy)) - print('') + # print('DEBUG :: pin target: ' + str(tx) + ',' + str(ty)) + # print('DEBUG :: pin position: ' + str(newx) + ',' + str(newy)) + # print('DEBUG :: pin distance: ' + str(xToCover) + ',' + str(yToCover)) + # print('DEBUG :: pin speed: ' + str(self.vx) + ',' + str(self.vy)) + # print('') self.pos = (newx, newy) From cdc81ffbc219380dd651078dacf55f0095d32e40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Vinot?= Date: Sun, 31 Aug 2014 10:15:28 +0200 Subject: [PATCH 2/4] make sure the pin hit the center of the avocado. --- avocado.py | 6 +++--- game.py | 3 ++- pingenerator.py | 6 +++--- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/avocado.py b/avocado.py index 02cf6c6..88e5961 100644 --- a/avocado.py +++ b/avocado.py @@ -62,9 +62,9 @@ class Avocado: if self.color == self.target: self.has_been_pinned = True self.is_still_falling = False - return True - else: - return False + return True, self.rect.center + return False, (0, 0) + def isFalling(self): diff --git a/game.py b/game.py index 9ca2342..c9f57c6 100755 --- a/game.py +++ b/game.py @@ -226,9 +226,10 @@ class TheGame: # Check if any avocados have been hit for avo in movingAvocados: - hit = avo.isHit(mousepos) + hit, center = avo.isHit(mousepos) if hit: score += 100 + newPin.throwAt(center) color = self.chooseRandomColor() crystalBall.setColor(color) elif hit == False: diff --git a/pingenerator.py b/pingenerator.py index 610365d..cf470e9 100644 --- a/pingenerator.py +++ b/pingenerator.py @@ -13,11 +13,12 @@ class Generate: screen_width, screen_height = screen.get_size() self.pos = (screen_width / 2, screen_height) self.image = pygame.image.load(os.path.join('img','pin.png')).convert_alpha() - + self.size = self.image.get_size() def throwAt(self, target): self.inFlight = True - self.target = target + x, y = target + self.target = (x, y - self.size[1]) def isStuck(self): @@ -29,7 +30,6 @@ class Generate: def blitme(self): self.screen.blit(self.image, self.pos) - def moveTowardsTarget(self): ############################## # From 633be6c6c07677b208afd9ed612f1ca785ad9621 Mon Sep 17 00:00:00 2001 From: David Raison Date: Sun, 31 Aug 2014 10:20:12 +0200 Subject: [PATCH 3/4] Added missing fileg --- avocado.py | 8 ++++++-- itext.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 itext.py diff --git a/avocado.py b/avocado.py index 61b1931..99600f7 100644 --- a/avocado.py +++ b/avocado.py @@ -80,10 +80,14 @@ class Avocado: return self.has_been_pinned + def checkBoundaries(self): + if self.rect.right > self.screen_width or self.rect.left < 0: + self.vx = -self.vx + + def move(self): if not self.has_been_pinned: - if self.rect.right > self.screen_width or self.rect.left < 0: - self.vx = -self.vy + self.checkBoundaries() if self.hasLanded(): self.destroy() diff --git a/itext.py b/itext.py new file mode 100644 index 0000000..eeb9b06 --- /dev/null +++ b/itext.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python3 + +import pygame +from support.colors import * + +class Text: + + def __init__(self, container, text, pos, duration): + """ Foo """ + self.clock = container['clock'] + self.duration = duration + self.totalTime = 0 + self.text = container['font'].render(text, 0 , WHITE) + self.screen = container['screen'] + self.pos = pos + + + def blitme(self): + """ sd """ + self.screen.blit(self.text, self.pos) + + + def hasExpired(self): + self.totalTime += self.clock.get_time() + if self.totalTime > self.duration: + del(self) + return True + return False From f45632aceeab37f3a4c2c1013a75156ef22a56b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Vinot?= Date: Sun, 31 Aug 2014 10:25:37 +0200 Subject: [PATCH 4/4] Fix last commit --- game.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/game.py b/game.py index b77ffdc..6af1237 100755 --- a/game.py +++ b/game.py @@ -95,11 +95,12 @@ 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)) - self.screen.blit(pygame.transform.scale(gameOverImage, self.size), (0, 0)) + self.screen.blit(pygame.transform.scale(gameOverImage, + self.screen.get_size()), (0, 0)) pygame.display.flip() self.fadeSound() pygame.time.wait(3000) @@ -255,7 +256,7 @@ class TheGame: self.thrownPins.append(newPin) # Check if any avocados have been hit - for avo in movingAvocados: + for avo in self.movingAvocados: hit, center = avo.isHit(mousepos) if hit: score += 100