Merge branch 'master' of github.com:lvl2/avocados
Conflicts: avocado.py game.pymaster
commit
1e4bfd1ed5
Binary file not shown.
BIN
audio/click.wav
BIN
audio/click.wav
Binary file not shown.
Binary file not shown.
17
avocado.py
17
avocado.py
|
@ -31,6 +31,7 @@ class Avocado:
|
||||||
|
|
||||||
# Avocado sounds
|
# Avocado sounds
|
||||||
self.click = self.loadClick()
|
self.click = self.loadClick()
|
||||||
|
self.clickFail = self.loadFailClick()
|
||||||
|
|
||||||
|
|
||||||
def setTargetColor(self, targetColor):
|
def setTargetColor(self, targetColor):
|
||||||
|
@ -57,14 +58,18 @@ class Avocado:
|
||||||
if self.rect.left < mousex and self.rect.right > mousex and \
|
if self.rect.left < mousex and self.rect.right > mousex and \
|
||||||
self.rect.top < mousey and self.rect.bottom > mousey:
|
self.rect.top < mousey and self.rect.bottom > mousey:
|
||||||
|
|
||||||
self.click.play()
|
|
||||||
|
|
||||||
if self.color == self.target:
|
if self.color == self.target:
|
||||||
self.has_been_pinned = True
|
self.has_been_pinned = True
|
||||||
self.is_still_falling = False
|
self.is_still_falling = False
|
||||||
|
self.click.play()
|
||||||
return True, self.rect.center
|
return True, self.rect.center
|
||||||
|
else:
|
||||||
|
self.clickFail.play()
|
||||||
return False, (0, 0)
|
return False, (0, 0)
|
||||||
|
# BUG - isHit is called 4 times upon click which makes it return the
|
||||||
|
# first time but fail the consecutive times
|
||||||
|
#else:
|
||||||
|
# self.clickFail.play()
|
||||||
|
|
||||||
|
|
||||||
def isFalling(self):
|
def isFalling(self):
|
||||||
|
@ -101,5 +106,11 @@ class Avocado:
|
||||||
return pygame.mixer.Sound("audio/click.wav")
|
return pygame.mixer.Sound("audio/click.wav")
|
||||||
|
|
||||||
|
|
||||||
|
def loadFailClick(self, sound=True):
|
||||||
|
if not sound:
|
||||||
|
return
|
||||||
|
return pygame.mixer.Sound("audio/poop.wav")
|
||||||
|
|
||||||
|
|
||||||
def destroy(self):
|
def destroy(self):
|
||||||
del(self)
|
del(self)
|
||||||
|
|
194
game.py
194
game.py
|
@ -5,7 +5,7 @@ Avocados and stuff
|
||||||
|
|
||||||
import os, random, sys
|
import os, random, sys
|
||||||
import pygame
|
import pygame
|
||||||
import avocado, crystal, pingenerator
|
import avocado, crystal, pingenerator, itext
|
||||||
from pygame.locals import *
|
from pygame.locals import *
|
||||||
from support.colors import *
|
from support.colors import *
|
||||||
from interface import hud
|
from interface import hud
|
||||||
|
@ -17,18 +17,14 @@ class TheGame:
|
||||||
""" Initialize the game """
|
""" Initialize the game """
|
||||||
pygame.init()
|
pygame.init()
|
||||||
pygame.display.set_caption('Pin Avo, the Cado!')
|
pygame.display.set_caption('Pin Avo, the Cado!')
|
||||||
|
self.clock = pygame.time.Clock()
|
||||||
displayInfo = pygame.display.Info()
|
self.initializeScreen()
|
||||||
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
|
|
||||||
|
|
||||||
# initialize the game canvas
|
# initialize the game canvas
|
||||||
self.size = (self.WIDTH, self.HEIGHT)
|
self.timeout = 30
|
||||||
self.screen = pygame.display.set_mode(self.size)
|
self.level = 1
|
||||||
|
self.targetScore = 400
|
||||||
|
self.screen = pygame.display.set_mode((self.WIDTH, self.HEIGHT))
|
||||||
self.colors = [BLUE, GREEN, RED, YELLOW]
|
self.colors = [BLUE, GREEN, RED, YELLOW]
|
||||||
self.bg = pygame.image.load(os.path.join('img', 'lawyerCrystalBall.png'))
|
self.bg = pygame.image.load(os.path.join('img', 'lawyerCrystalBall.png'))
|
||||||
|
|
||||||
|
@ -37,7 +33,13 @@ class TheGame:
|
||||||
|
|
||||||
# Set splashscreen
|
# Set splashscreen
|
||||||
splashScreen = pygame.image.load("img/splashScreen.png")
|
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.display.flip()
|
||||||
pygame.time.wait(3000)
|
pygame.time.wait(3000)
|
||||||
|
|
||||||
|
@ -50,6 +52,17 @@ class TheGame:
|
||||||
sound = False
|
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):
|
def mute(self,mute=False, sound=True):
|
||||||
if not sound:
|
if not sound:
|
||||||
return
|
return
|
||||||
|
@ -82,7 +95,7 @@ class TheGame:
|
||||||
|
|
||||||
|
|
||||||
def gameOver(self):
|
def gameOver(self):
|
||||||
screen_width, screen_height = self.size
|
screen_width, screen_height = self.screen.get_size
|
||||||
gameOverImage = pygame.image.load("img/gameOver.png")
|
gameOverImage = pygame.image.load("img/gameOver.png")
|
||||||
gameOverText = self.bigFont.render('GAME OVER', 0, YELLOW)
|
gameOverText = self.bigFont.render('GAME OVER', 0, YELLOW)
|
||||||
gameOverImage.blit(gameOverText, (screen_width/8, screen_height/7))
|
gameOverImage.blit(gameOverText, (screen_width/8, screen_height/7))
|
||||||
|
@ -102,28 +115,35 @@ class TheGame:
|
||||||
if type(self.bg) is tuple:
|
if type(self.bg) is tuple:
|
||||||
self.screen.fill(self.bg)
|
self.screen.fill(self.bg)
|
||||||
else:
|
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):
|
def main(self):
|
||||||
clock = pygame.time.Clock()
|
|
||||||
desired_fps = 60
|
desired_fps = 60
|
||||||
|
|
||||||
|
##############################
|
||||||
# Never set below 4, else we have a high
|
# Never set below 4, else we have a high
|
||||||
# probability of losing the game due to a missing color
|
# probability of losing the game due to a missing color
|
||||||
# Alternatively, you could edit chooseRandomColor()
|
# Alternatively, you could edit chooseRandomColor()
|
||||||
# to only work on the first multiplier colors
|
# to only work on the first multiplier colors
|
||||||
multiplier = 4
|
multiplier = 4
|
||||||
time = timeleft = 30
|
|
||||||
level = 1
|
|
||||||
levelChange = 0
|
|
||||||
score = 0
|
score = 0
|
||||||
targetScore = 400
|
|
||||||
|
|
||||||
# We could use this list for redrawing only this part
|
# We could use this list for redrawing only this part
|
||||||
# of the screen install of all of it
|
# of the screen install of all of it
|
||||||
pinnedAvocados = []
|
self.resetLevel()
|
||||||
movingAvocados = []
|
|
||||||
thrownPins = []
|
|
||||||
|
|
||||||
# initialize the HUD class and the lawyer
|
# initialize the HUD class and the lawyer
|
||||||
the_hud = hud.Hud(self.screen)
|
the_hud = hud.Hud(self.screen)
|
||||||
|
@ -133,85 +153,95 @@ class TheGame:
|
||||||
color = self.chooseRandomColor()
|
color = self.chooseRandomColor()
|
||||||
crystalBall.setColor(color)
|
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
|
running = True
|
||||||
while running:
|
while running:
|
||||||
time_passed = clock.tick(desired_fps)
|
time_passed = self.clock.tick(desired_fps)
|
||||||
fps = clock.get_fps()
|
fps = self.clock.get_fps()
|
||||||
screen_width, screen_height = self.size
|
screen_width, screen_height = self.screen.get_size()
|
||||||
|
|
||||||
# Redraw the background and put our lawyer back on top
|
# Redraw the background and put our lawyer back on top
|
||||||
self.drawBackground()
|
self.drawBackground()
|
||||||
crystalBall.blitme()
|
crystalBall.blitme()
|
||||||
|
|
||||||
# Next level?
|
# Next level?
|
||||||
if score >= (targetScore * level):
|
if score >= (self.targetScore * self.level):
|
||||||
level += 1
|
self.level += 1
|
||||||
levelChange = 70
|
levelText = itext.Text(
|
||||||
timeleft = time
|
container,
|
||||||
print('DEBUG :: Score: ' + str(score))
|
'Level ' + str(self.level),
|
||||||
print('DEBUG :: Level ' + str(level))
|
(screen_width / 3, screen_height /2),
|
||||||
self.playLevel(level)
|
2000
|
||||||
pinnedAvocados = []
|
)
|
||||||
movingAvocados = []
|
texts.append(levelText)
|
||||||
thrownPins = []
|
# 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
|
self.timeleft -= time_passed / 1000
|
||||||
timeleft = round(timeleft, 2)
|
self.timeleft = round(self.timeleft, 2)
|
||||||
if timeleft <= 0:
|
if self.timeleft <= 0:
|
||||||
self.gameOver()
|
self.gameOver()
|
||||||
else:
|
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
|
# Redraw the HUD
|
||||||
the_hud.draw_hud(score, displaytime, round(fps, 2))
|
the_hud.draw_hud(score, displaytime, round(fps, 2))
|
||||||
|
|
||||||
# If we're not currently in between levels…
|
# Initialize a number of avocados, depending on the level
|
||||||
if levelChange == 0:
|
avocadosInGame = len(self.movingAvocados)
|
||||||
# Initialize a number of avocados, depending on the level
|
avocadosWanted = self.level * multiplier
|
||||||
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)
|
|
||||||
|
|
||||||
# Remove avocados from the list of moving avocados if they no longer move
|
if avocadosInGame < avocadosWanted:
|
||||||
# Or add them to the list of pinned avocados if they're been hit
|
probability = int(1.0/(avocadosWanted - avocadosInGame) * 100)
|
||||||
pinnedAvocados += [avo for avo in movingAvocados if avo.isPinned() ]
|
if random.randint(0, probability) < 3:
|
||||||
movingAvocados[:] = [ avo for avo in movingAvocados if avo.isFalling() ]
|
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)
|
||||||
|
|
||||||
##############################
|
# 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
|
||||||
# Late-Night-Comments:
|
self.pinnedAvocados += [avo for avo in self.movingAvocados if avo.isPinned() ]
|
||||||
#
|
self.movingAvocados[:] = [ avo for avo in self.movingAvocados if avo.isFalling() ]
|
||||||
# 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…
|
|
||||||
#
|
|
||||||
##############################
|
|
||||||
|
|
||||||
# Now redraw our avocados
|
##############################
|
||||||
for a in movingAvocados:
|
#
|
||||||
a.setTargetColor(color)
|
# Late-Night-Comments:
|
||||||
a.move()
|
#
|
||||||
a.blitme()
|
# 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:
|
# Now redraw our avocados
|
||||||
a.blitme()
|
for a in self.movingAvocados:
|
||||||
|
a.setTargetColor(color)
|
||||||
|
a.move()
|
||||||
|
a.blitme()
|
||||||
|
|
||||||
# And finally check if we need to redraw any pins
|
for a in self.pinnedAvocados:
|
||||||
for activePin in thrownPins:
|
a.blitme()
|
||||||
activePin.blitme()
|
|
||||||
if not activePin.isStuck():
|
# And finally check if we need to redraw any pins
|
||||||
activePin.moveTowardsTarget()
|
for activePin in self.thrownPins:
|
||||||
|
activePin.blitme()
|
||||||
|
if not activePin.isStuck():
|
||||||
|
activePin.moveTowardsTarget()
|
||||||
|
|
||||||
# Catch events
|
# Catch events
|
||||||
for event in pygame.event.get():
|
for event in pygame.event.get():
|
||||||
|
@ -219,10 +249,10 @@ class TheGame:
|
||||||
if event.type == MOUSEBUTTONDOWN:
|
if event.type == MOUSEBUTTONDOWN:
|
||||||
mousepos = pygame.mouse.get_pos()
|
mousepos = pygame.mouse.get_pos()
|
||||||
|
|
||||||
# Throw a pin here
|
# Throwing a new pin
|
||||||
newPin = pingenerator.Generate(self.screen)
|
newPin = pingenerator.Generate(self.screen)
|
||||||
newPin.throwAt(mousepos)
|
newPin.throwAt(mousepos)
|
||||||
thrownPins.append(newPin)
|
self.thrownPins.append(newPin)
|
||||||
|
|
||||||
# Check if any avocados have been hit
|
# Check if any avocados have been hit
|
||||||
for avo in movingAvocados:
|
for avo in movingAvocados:
|
||||||
|
|
BIN
img/pin.png
BIN
img/pin.png
Binary file not shown.
Before Width: | Height: | Size: 2.9 KiB After Width: | Height: | Size: 8.2 KiB |
Binary file not shown.
Before Width: | Height: | Size: 508 KiB After Width: | Height: | Size: 1.1 MiB |
|
@ -23,7 +23,7 @@ class Generate:
|
||||||
|
|
||||||
def isStuck(self):
|
def isStuck(self):
|
||||||
if self.pos == self.target:
|
if self.pos == self.target:
|
||||||
print('Pin stuck!')
|
# print('Pin stuck!')
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
@ -70,9 +70,9 @@ class Generate:
|
||||||
|
|
||||||
newy = y + ystep
|
newy = y + ystep
|
||||||
|
|
||||||
print('DEBUG :: pin target: ' + str(tx) + ',' + str(ty))
|
# print('DEBUG :: pin target: ' + str(tx) + ',' + str(ty))
|
||||||
print('DEBUG :: pin position: ' + str(newx) + ',' + str(newy))
|
# print('DEBUG :: pin position: ' + str(newx) + ',' + str(newy))
|
||||||
print('DEBUG :: pin distance: ' + str(xToCover) + ',' + str(yToCover))
|
# print('DEBUG :: pin distance: ' + str(xToCover) + ',' + str(yToCover))
|
||||||
print('DEBUG :: pin speed: ' + str(self.vx) + ',' + str(self.vy))
|
# print('DEBUG :: pin speed: ' + str(self.vx) + ',' + str(self.vy))
|
||||||
print('')
|
# print('')
|
||||||
self.pos = (newx, newy)
|
self.pos = (newx, newy)
|
||||||
|
|
Loading…
Reference in New Issue