avocados/game.py

216 lines
6.6 KiB
Python
Raw Normal View History

2014-08-30 15:28:07 +02:00
#!/usr/bin/env python3
"""
Avocados and stuff
"""
2014-08-30 22:42:20 +02:00
import os, random, sys
2014-08-30 15:28:07 +02:00
import pygame
2014-08-31 01:03:39 +02:00
import avocado, crystal
2014-08-30 15:28:07 +02:00
from pygame.locals import *
2014-08-30 16:28:56 +02:00
from support.colors import *
from interface import hud
2014-08-30 15:28:07 +02:00
2014-08-30 22:02:17 +02:00
2014-08-30 22:16:41 +02:00
class TheGame:
def __init__(self):
2014-08-31 00:15:36 +02:00
""" Initialize the game """
pygame.init()
pygame.display.set_caption('Pin Avo, the Cado!')
2014-08-31 01:07:45 +02:00
displayInfo = pygame.display.Info()
self.resize = 1.3
self.WIDTH = int(displayInfo.current_w / self.resize)
self.HEIGHT = int(displayInfo.current_h / self.resize)
2014-08-31 01:15:03 +02:00
self.WIDTH, self.HEIGHT = 800, 600
2014-08-30 23:57:31 +02:00
# initialize the game canvas
2014-08-31 01:07:45 +02:00
self.size = (self.WIDTH, self.HEIGHT)
2014-08-30 23:57:31 +02:00
self.screen = pygame.display.set_mode(self.size)
2014-08-30 22:16:41 +02:00
self.colors = [BLUE, GREEN, RED, YELLOW]
2014-08-31 00:47:36 +02:00
self.bg = pygame.image.load(os.path.join('img', 'lawyerCrystalBall.png'))
2014-08-31 00:15:36 +02:00
# fonts
2014-08-31 00:47:36 +02:00
self.bigFont = pygame.font.Font(None, 90)
2014-08-31 00:15:36 +02:00
2014-08-31 01:12:39 +02:00
# Set splashscreen
2014-08-31 00:54:33 +02:00
splashScreen = pygame.image.load("img/splashScreen.png")
self.screen.blit(pygame.transform.scale(splashScreen, self.size), (0, 0))
pygame.display.flip()
pygame.time.wait(3000)
try:
pygame.mixer.init()
pygame.mixer.music.set_volume(0.5)
2014-08-31 00:15:36 +02:00
sound = True
except:
print("Y U NO sound? :(")
2014-08-31 00:15:36 +02:00
sound = False
2014-08-30 22:16:41 +02:00
2014-08-31 00:02:36 +02:00
def mute(self,mute=False, sound=True):
if not sound:
return
if mute:
pygame.mixer.music.set_volume(0.0)
else:
pygame.mixer.music.set_volume(0.5)
def playLevel(self,lvl=1,sound=True):
if not sound:
return
if lvl == 1:
pygame.mixer.music.load("""audio/level1.wav""")
elif lvl == 2:
pygame.mixer.music.load("""audio/level2.wav""")
2014-08-30 23:15:35 +02:00
elif lvl >= 3:
pygame.mixer.music.load("""audio/level3.wav""")
pygame.mixer.music.play()
2014-08-31 01:07:12 +02:00
def fadeSound(self, sound=True):
if not sound:
return
pygame.mixer.music.fadeout(3000)
2014-08-31 01:07:12 +02:00
def chooseRandomColor(self):
selected = random.randint(0, 3)
return self.colors[selected]
2014-08-30 22:16:41 +02:00
2014-08-30 23:34:52 +02:00
2014-08-30 23:57:31 +02:00
def gameOver(self):
screen_width, screen_height = self.size
gameOverImage = pygame.image.load("img/gameOver.png")
2014-08-31 00:47:36 +02:00
gameOverText = self.bigFont.render('GAME OVER', 0, YELLOW)
gameOverImage.blit(gameOverText, (screen_width/8, screen_height/7))
2014-08-30 23:57:31 +02:00
self.screen.blit(pygame.transform.scale(gameOverImage, self.size), (0, 0))
2014-08-30 23:27:24 +02:00
pygame.display.flip()
2014-08-31 01:07:12 +02:00
self.fadeSound()
2014-08-30 23:27:24 +02:00
pygame.time.wait(3000)
2014-08-30 23:57:31 +02:00
pygame.quit()
2014-08-31 00:15:36 +02:00
sys.exit()
2014-08-30 23:57:31 +02:00
2014-08-30 23:27:24 +02:00
2014-08-30 23:34:15 +02:00
def keepPinned(self, avocado):
self.pinned.append(avocado)
2014-08-30 22:16:41 +02:00
2014-08-30 23:57:31 +02:00
2014-08-31 00:47:36 +02:00
def drawBackground(self):
if type(self.bg) is tuple:
self.screen.fill(self.bg)
else:
self.screen.blit(pygame.transform.scale(self.bg, self.size), (0, 0))
2014-08-30 22:16:41 +02:00
def main(self):
clock = pygame.time.Clock()
2014-08-30 23:34:52 +02:00
desired_fps = 60
2014-08-30 23:10:41 +02:00
multiplier = 3
2014-08-30 22:48:49 +02:00
time = timeleft = 30
level = 1
2014-08-30 23:03:51 +02:00
levelChange = 0
2014-08-31 01:07:12 +02:00
score = 0
targetScore = 400
2014-08-30 22:16:41 +02:00
# initialize the HUD class and the lawyer
2014-08-30 23:57:31 +02:00
the_hud = hud.Hud(self.screen)
2014-08-31 01:03:39 +02:00
crystalBall = crystal.Crystal(self.screen)
2014-08-30 22:16:41 +02:00
2014-08-31 00:15:36 +02:00
# Initial color indication
2014-08-30 22:16:41 +02:00
color = self.chooseRandomColor()
2014-08-31 01:38:26 +02:00
crystalBall.setColor(color)
2014-08-30 22:16:41 +02:00
2014-08-31 00:47:36 +02:00
# We could use this list for redrawing only this part
# of the screen install of all of it
avocados = []
running = True
2014-08-31 00:15:36 +02:00
while running:
2014-08-30 22:16:41 +02:00
time_passed = clock.tick(desired_fps)
fps = clock.get_fps()
2014-08-30 23:57:31 +02:00
screen_width, screen_height = self.size
2014-08-30 22:48:49 +02:00
2014-08-31 00:47:36 +02:00
# Redraw the background and put our lawyer back on top
self.drawBackground()
2014-08-31 01:38:26 +02:00
crystalBall.blitme()
2014-08-30 22:48:49 +02:00
# Next level?
2014-08-31 01:38:26 +02:00
if score >= (targetScore * level):
2014-08-30 22:48:49 +02:00
level += 1
2014-08-31 01:38:26 +02:00
levelChange = 70
2014-08-30 23:10:41 +02:00
timeleft = time
2014-08-30 23:03:51 +02:00
avocados = []
2014-08-31 01:24:10 +02:00
print('DEBUG :: Score: ' + str(score))
2014-08-30 23:03:51 +02:00
print('DEBUG :: Level ' + str(level))
2014-08-31 00:47:36 +02:00
self.playLevel(level)
2014-08-30 22:16:41 +02:00
2014-08-30 23:03:51 +02:00
if levelChange > 0:
2014-08-31 00:47:36 +02:00
levelText = self.bigFont.render('Level ' + str(level), 0, WHITE)
2014-08-31 00:15:36 +02:00
self.screen.blit(levelText, (screen_width / 3, screen_height / 2))
2014-08-30 23:03:51 +02:00
levelChange -= 1
2014-08-30 22:16:41 +02:00
timeleft -= time_passed / 1000
timeleft = round(timeleft, 2)
if timeleft <= 0:
2014-08-30 23:57:31 +02:00
self.gameOver()
2014-08-30 22:16:41 +02:00
else:
displaytime = timeleft
# Redraw the HUD
the_hud.draw_hud(score, displaytime, round(fps, 2))
2014-08-31 01:07:12 +02:00
# If we're not currently in between levels…
2014-08-30 23:03:51 +02:00
if levelChange == 0:
2014-08-31 01:07:12 +02:00
# Initialize a number of avocados, depending on the level
2014-08-30 23:03:51 +02:00
avocados_in_game = len(avocados)
avocadosWanted = level * multiplier
if avocados_in_game < avocadosWanted:
for i in range(avocados_in_game, avocadosWanted):
avocolor = self.chooseRandomColor()
avosize = (50, 50) # should we randomize this?
2014-08-30 23:57:31 +02:00
a = avocado.Avocado(self.screen, avocolor, avosize, color)
2014-08-30 23:03:51 +02:00
avocados.append(a)
# Remove avocados from the list if they no longer exist
# E.g. have been pinned or fallen down
avocados[:] = [ x for x in avocados if x.exists() ]
for a in avocados:
a.setTargetColor(color)
2014-08-31 01:07:12 +02:00
if not a.isPinned():
a.move()
2014-08-30 23:34:15 +02:00
a.blitme()
2014-08-30 22:16:41 +02:00
# Catch events
for event in pygame.event.get():
# Collision detection
if event.type == MOUSEBUTTONDOWN:
2014-08-31 01:07:12 +02:00
mousepos = pygame.mouse.get_pos()
# Throw a pin here
# pin.throwAt(mousepos)
# Yep, above here
2014-08-30 22:16:41 +02:00
for avo in avocados:
2014-08-31 01:07:12 +02:00
hit = avo.isHit(mousepos)
2014-08-30 22:16:41 +02:00
if hit:
score += 100
color = self.chooseRandomColor()
2014-08-31 01:03:39 +02:00
crystalBall.setColor(color)
2014-08-30 22:16:41 +02:00
elif hit == False:
score -= 50
# Had enough of this?
if event.type == pygame.QUIT:
running = False
2014-08-30 22:42:20 +02:00
pygame.quit()
sys.exit()
2014-08-30 22:16:41 +02:00
2014-08-30 21:11:10 +02:00
pygame.display.flip()
2014-08-30 22:16:41 +02:00
2014-08-30 15:28:07 +02:00
if __name__ == '__main__':
2014-08-30 22:16:41 +02:00
game = TheGame()
game.playLevel()
2014-08-30 22:16:41 +02:00
game.main()