Merge branch 'master' of github.com:lvl2/avocados
commit
85386092c8
31
avocado.py
31
avocado.py
|
@ -5,14 +5,16 @@ from support import operations
|
||||||
|
|
||||||
class Avocado:
|
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
|
# Set up our instance variables
|
||||||
self.screen = screen
|
self.screen = screen
|
||||||
self.color = color
|
|
||||||
self.target = target
|
|
||||||
self.screen_width, self.screen_height = screen.get_size()
|
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
|
# Initialize the image
|
||||||
self.i = pygame.image.load(filename).convert_alpha()
|
self.i = pygame.image.load(filename).convert_alpha()
|
||||||
|
@ -82,9 +84,26 @@ class Avocado:
|
||||||
|
|
||||||
|
|
||||||
def checkBoundaries(self):
|
def checkBoundaries(self):
|
||||||
|
# Checking screen boundaries
|
||||||
if self.rect.right > self.screen_width or self.rect.left < 0:
|
if self.rect.right > self.screen_width or self.rect.left < 0:
|
||||||
|
self.checkObstacle = True
|
||||||
self.vx = -self.vx
|
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):
|
def move(self):
|
||||||
if not self.has_been_pinned:
|
if not self.has_been_pinned:
|
||||||
|
@ -99,9 +118,9 @@ class Avocado:
|
||||||
|
|
||||||
|
|
||||||
def hasLanded(self):
|
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
|
self.is_still_falling = False
|
||||||
print('DEBUG :: splash!')
|
print('DEBUG :: splatch!')
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
|
13
crystal.py
13
crystal.py
|
@ -12,11 +12,18 @@ class Crystal:
|
||||||
def __init__(self, screen):
|
def __init__(self, screen):
|
||||||
self.screen = screen
|
self.screen = screen
|
||||||
screen_width, screen_height = screen.get_size()
|
screen_width, screen_height = screen.get_size()
|
||||||
self.imageCenterX = self.screen.get_rect().centerx
|
self.rect = self.screen.get_rect()
|
||||||
self.imageCenterY = self.screen.get_rect().centery
|
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):
|
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):
|
def setColor(self, color):
|
||||||
self.color = color
|
self.color = color
|
||||||
|
|
46
game.py
46
game.py
|
@ -24,6 +24,13 @@ class TheGame:
|
||||||
self.timeout = 30
|
self.timeout = 30
|
||||||
self.level = 1
|
self.level = 1
|
||||||
self.targetScore = 400
|
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.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'))
|
||||||
|
@ -142,23 +149,17 @@ class TheGame:
|
||||||
|
|
||||||
|
|
||||||
def main(self):
|
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
|
score = 0
|
||||||
|
boundaries = []
|
||||||
|
|
||||||
# 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 instead of all of it
|
||||||
self.resetLevel()
|
self.resetLevel()
|
||||||
|
|
||||||
# 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)
|
||||||
crystalBall = crystal.Crystal(self.screen)
|
crystalBall = crystal.Crystal(self.screen)
|
||||||
|
boundaries.append(crystalBall.getBoundaries())
|
||||||
|
|
||||||
# Initial color indication
|
# Initial color indication
|
||||||
color = self.chooseRandomColor()
|
color = self.chooseRandomColor()
|
||||||
|
@ -166,19 +167,13 @@ class TheGame:
|
||||||
|
|
||||||
texts = []
|
texts = []
|
||||||
container = {'font': self.bigFont, 'screen': self.screen, 'clock': self.clock}
|
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 = self.clock.tick(desired_fps)
|
time_passed = self.clock.tick(self.desired_fps)
|
||||||
fps = self.clock.get_fps()
|
fps = self.clock.get_fps()
|
||||||
screen_width, screen_height = self.screen.get_size()
|
screen_width, screen_height = self.screen.get_size()
|
||||||
|
|
||||||
# Redraw the background and put our lawyer back on top
|
|
||||||
self.drawBackground()
|
|
||||||
crystalBall.blitme()
|
|
||||||
|
|
||||||
# Next level?
|
# Next level?
|
||||||
if score >= (self.targetScore * self.level):
|
if score >= (self.targetScore * self.level):
|
||||||
self.level += 1
|
self.level += 1
|
||||||
|
@ -189,7 +184,6 @@ class TheGame:
|
||||||
2000
|
2000
|
||||||
)
|
)
|
||||||
texts.append(levelText)
|
texts.append(levelText)
|
||||||
# self.screen.blit(levelText, (screen_width / 3, screen_height / 2))
|
|
||||||
self.playLevel(self.level)
|
self.playLevel(self.level)
|
||||||
self.resetLevel()
|
self.resetLevel()
|
||||||
|
|
||||||
|
@ -202,26 +196,34 @@ class TheGame:
|
||||||
displaytime = self.timeleft
|
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
|
# Check if there's any text that wants to get displayed
|
||||||
for text in texts:
|
for text in texts:
|
||||||
text.blitme()
|
text.blitme()
|
||||||
texts[:] = [text for text in texts if not text.hasExpired() ]
|
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))
|
||||||
|
|
||||||
# Initialize a number of avocados, depending on the level
|
# Initialize a number of avocados, depending on the level
|
||||||
avocadosInGame = len(self.movingAvocados)
|
avocadosInGame = len(self.movingAvocados)
|
||||||
avocadosWanted = self.level * multiplier
|
avocadosWanted = self.level * self.multiplier
|
||||||
|
|
||||||
if avocadosInGame < avocadosWanted:
|
if avocadosInGame < avocadosWanted:
|
||||||
probability = int(1.0/(avocadosWanted - avocadosInGame) * 100)
|
probability = int(1.0/(avocadosWanted - avocadosInGame) * 100)
|
||||||
if random.randint(0, probability) < 3:
|
if random.randint(0, probability) < 3:
|
||||||
avocolor = self.chooseRandomColor()
|
properties = {'color': self.chooseRandomColor(), 'size': (50,50)}
|
||||||
avosize = (50, 50) # should we randomize this?
|
|
||||||
# Spawn a new avocado
|
# 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)
|
self.movingAvocados.append(a)
|
||||||
|
|
||||||
# Remove avocados from the list of moving avocados if they no longer move
|
# Remove avocados from the list of moving avocados if they no longer move
|
||||||
|
|
|
@ -19,7 +19,7 @@ class Hud:
|
||||||
self.hud.blit(self.draw_timeleft(timeleft), (self.screen_width / 2, 0))
|
self.hud.blit(self.draw_timeleft(timeleft), (self.screen_width / 2, 0))
|
||||||
thefps = self.draw_fps(fps)
|
thefps = self.draw_fps(fps)
|
||||||
fps_rect = thefps.get_rect()
|
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()
|
self.blitme()
|
||||||
|
|
||||||
def draw_score(self, score):
|
def draw_score(self, score):
|
||||||
|
|
Loading…
Reference in New Issue