def explode(self): explosion = None if self.correct: explosion = sprites.spriteAnimation(assets.explosionABMS, 30) s = [] a = int(self.ans) for i in self.enemy.solution: if i != a: s.append(i) self.enemy.solution = s self.player.abmh.starttime = 0 self.player.score += self.enemy.getValue() * cap(self.player.combo, 1, None) self.player.correct += 1 self.player.combo += 1 if self.player.combo % 10 == 0: self.player.hp += 1 else: explosion = sprites.spriteAnimation(assets.explosionABMF, 30) self.player.combo = 0 self.player.total +=1 explosion.start() self.player.game.explosions.append((explosion, self.getPos())) self.player.abms.remove(self)
def explode(self): explosion = None if self.correct: explosion = sprites.spriteAnimation(assets.explosionABMS, 30) s = [] a = int(self.ans) for i in self.enemy.solution: if i != a: s.append(i) self.enemy.solution = s self.player.abmh.starttime = 0 self.player.score += self.enemy.getValue() * cap( self.player.combo, 1, None) self.player.correct += 1 self.player.combo += 1 if self.player.combo % 10 == 0: self.player.hp += 1 else: explosion = sprites.spriteAnimation(assets.explosionABMF, 30) self.player.combo = 0 self.player.total += 1 explosion.start() self.player.game.explosions.append((explosion, self.getPos())) self.player.abms.remove(self)
def startscreen(display): miscfont = pygame.font.Font('freesansbold.ttf', 32) smallfont = pygame.font.Font('freesansbold.ttf', 16) largeFont = pygame.font.Font('freesansbold.ttf', 30) textFont = pygame.font.Font('freesansbold.ttf', 20) displayrect = display.get_rect() city = assets.city.copy() cityrect = city.get_rect() cityrect.midbottom = displayrect.midbottom title = assets.title.copy() titlerect = title.get_rect() titlerect.midtop = (int(displayrect.w / 2), int(displayrect.h / 4)) rockets = [] for i in range(0, 5): r = choice([assets.rocket1, assets.rocket2, assets.rocket3_2]) rocket = pygame.transform.rotate(r.copy(), 270) rocketrect = rocket.get_rect() rocketrect.center = randint(0, displayrect.w), randint(0, displayrect.h) rockets.append((rocket, rocketrect)) coin = assets.coin.copy() coinrect = coin.get_rect() coinrect.center = (randint(0, displayrect.w - 1), randint(0, displayrect.h - 1)) holdingCoin = False recep = sprites.spriteAnimation(assets.receptacle.copy(), 10) receprect = recep.getSurface().get_rect() receprect.midbottom = displayrect.midbottom insertcoin = smallfont.render('Please insert 3.14 cents', True, colors.aqua) insertcoinrect = insertcoin.get_rect() insertcoinrect.midbottom = receprect.midtop startingsoon = miscfont.render('Starting...', True, colors.white) startingsoonrect = startingsoon.get_rect() startingsoonrect.midbottom = displayrect.midbottom highscores = pygame.Surface((350, 210), pygame.SRCALPHA, 32) highscoresrect = highscores.get_rect() highscoresrect.midtop = displayrect.center highscoresborder = highscoresrect.copy() highscoresborder.top -= 10 highscoresborder.left -= 10 n = 0 c = colors.black for name, s in gameover.getHighScores(): highscores.blit(miscfont.render(str(n + 1), True, c), (0, n * 40)) highscores.blit(textFont.render(name.upper(), True, c), (50, n * 40)) highscores.blit(textFont.render(str(s), True, c), (250, n * 40)) n += 1 if n == 5: break while True: for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: MAIN.terminate() elif event.type == pygame.MOUSEBUTTONDOWN: if holdingCoin: if receprect.colliderect(coinrect): holdingCoin = False coinrect.bottomright = (0, 0) recep.start() else: holdingCoin = False else: if coinrect.collidepoint(event.pos): holdingCoin = True if holdingCoin: pygame.mouse.set_visible(False) coinrect.center = pygame.mouse.get_pos() else: pygame.mouse.set_visible(True) if recep.isFinished(): raise MAIN.LevelFinished() display.fill(colors.sky_blue) display.blit(city, cityrect) for rocket, rect in rockets: display.blit(rocket, rect) display.blit(title, titlerect) display.blit(recep.getSurface(), receprect) display.blit(insertcoin, insertcoinrect) pygame.draw.rect(display, colors.yellow, highscoresborder) display.blit(highscores, highscoresrect) if recep.hasStarted: display.blit(startingsoon, startingsoonrect) display.blit(coin, coinrect) if not pygame.mixer.get_busy(): assets.loop1.play() pygame.display.update()
def mainLoop(self): for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() elif event.type == pygame.KEYDOWN: key = pygame.key.name(event.key) if key in '1234567890' and len(self.player.answer) < 2: self.player.answer += key elif key in KEYPAD: self.player.answer += key[1:-1] elif event.key in (pygame.K_KP_MINUS, pygame.K_MINUS): self.player.negative = not self.player.negative elif event.key == pygame.K_BACKSPACE: self.player.answer = self.player.answer[:-1] elif event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1 and self.player.canLaunch(): self.player.launch() self.player.negative = False w, h = self.resolution screenrect = pygame.Rect(0, 0, w, h) mx, my = pygame.mouse.get_pos() surf = pygame.Surface(self.resolution) surf.fill(colors.sky_blue) surf.blit(assets.sun, self.sunpos) bgRect = assets.background.get_rect() bgRect.midright = screenrect.midright bgRect.right += 50 if self.dying: xdeviation = randint(-50, 50) ydeviation = randint(-50, 50) dist = int((time.time() - self.died) * 100) bgRect.right = screenrect.right + dist + xdeviation bgRect.top = bgRect.top + ydeviation surf.blit(assets.background, bgRect) surf.blit(self.enemy.getSurface(), (0, 0)) if self.enemy.canSpawn(): self.enemy.spawn() for abm in self.player.abms: surf.blit(abm.getSurface(), abm.getPos()) if abm.hasArrived(): abm.explode() for e, p in self.explosions: rect = e.getRect() rect.center = p surf.blit(e.getSurface(), rect) if e.isFinished(): self.explosions.remove((e, p)) for e in self.enemy: if e.getProgress() >= 1: e.explode() if self.player.hp != 0: self.player.hp -= 1 self.player.combo = 0 elif e.canExplode(): e.explode() if self.player.hp == 0: self.dying = True if self.died == -1: self.player.survived = secToMS(int(time.time() - self.start)) self.died = time.time() if self.dying and time.time() - self.lastExplosion > 60 / (140 * 2): self.lastExplosion = time.time() explosions = [ assets.explosionEnd1, assets.explosionEnd2, assets.explosion1, assets.explosion2, assets.explosion3, assets.explosionABMF ] sprite = choice(explosions) point = (randint(0, w - 1), randint(0, h - 1)) exp = sprites.spriteAnimation(sprite, 24) exp.start() self.explosions.append((exp, point)) if self.dying and time.time() - self.died > 5: txt = largeFont.render('GAME OVER', True, colors.black) txtRect = txt.get_rect() txtRect.center = screenrect.center surf.blit(txt, txtRect) if time.time() - self.died > 6: txt2 = abmlFont.render('Press any key to continue', True, colors.black) txt2Rect = txt2.get_rect() txt2Rect.midtop = txtRect.midbottom surf.blit(txt2, txt2Rect) if True in pygame.key.get_pressed(): gameover.endscreen(self.display, self.player) abml = None if self.player.lastMouseY == my: abml = self.player.lastABML elif self.player.lastMouseY < my: abml = assets.abmLauncherDown.copy() elif self.player.lastMouseY > my: abml = assets.abmLauncherUp.copy() self.player.lastABML = abml.copy() abml = self.player.getABML() abmlRect = abml.get_rect() abmlRect.midright = (w, my) abmlText = abmlFont.render(self.player.getAnswer(), True, colors.white) abmlTextRect = abmlText.get_rect() abmlTextRect.center = abmlRect.center surf.blit(abml, abmlRect) surf.blit(abmlText, abmlTextRect) surf.blit(self.player.getGUI(), (0, 0)) if not pygame.mixer.get_busy(): assets.loop1.play() self.display.fill(colors.white) self.display.blit(surf, (0, 0)) pygame.display.update()
def getABMHolder(self): return sprites.spriteAnimation(assets.abmHolder, 6)
def mainLoop(self): for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() elif event.type == pygame.KEYDOWN: key = pygame.key.name(event.key) if key in '1234567890' and len(self.player.answer) < 2: self.player.answer += key elif key in KEYPAD: self.player.answer += key[1:-1] elif event.key in (pygame.K_KP_MINUS, pygame.K_MINUS): self.player.negative = not self.player.negative elif event.key == pygame.K_BACKSPACE: self.player.answer = self.player.answer[:-1] elif event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1 and self.player.canLaunch(): self.player.launch() self.player.negative = False w, h = self.resolution screenrect = pygame.Rect(0, 0, w, h) mx, my = pygame.mouse.get_pos() surf = pygame.Surface(self.resolution) surf.fill(colors.sky_blue) surf.blit(assets.sun, self.sunpos) bgRect = assets.background.get_rect() bgRect.midright = screenrect.midright bgRect.right += 50 if self.dying: xdeviation = randint(-50, 50) ydeviation = randint(-50, 50) dist = int((time.time() - self.died) * 100) bgRect.right = screenrect.right + dist + xdeviation bgRect.top = bgRect.top + ydeviation surf.blit(assets.background, bgRect) surf.blit(self.enemy.getSurface(), (0, 0)) if self.enemy.canSpawn(): self.enemy.spawn() for abm in self.player.abms: surf.blit(abm.getSurface(), abm.getPos()) if abm.hasArrived(): abm.explode() for e, p in self.explosions: rect = e.getRect() rect.center = p surf.blit(e.getSurface(), rect) if e.isFinished(): self.explosions.remove((e, p)) for e in self.enemy: if e.getProgress() >= 1: e.explode() if self.player.hp != 0: self.player.hp -= 1 self.player.combo = 0 elif e.canExplode(): e.explode() if self.player.hp == 0: self.dying = True if self.died == -1: self.player.survived = secToMS(int(time.time() - self.start)) self.died = time.time() if self.dying and time.time() - self.lastExplosion > 60 / (140 * 2): self.lastExplosion = time.time() explosions = [ assets.explosionEnd1, assets.explosionEnd2, assets.explosion1, assets.explosion2, assets.explosion3, assets.explosionABMF ] sprite = choice(explosions) point = (randint(0, w-1), randint(0, h-1)) exp = sprites.spriteAnimation(sprite, 24) exp.start() self.explosions.append((exp, point)) if self.dying and time.time() - self.died > 5: txt = largeFont.render('GAME OVER', True, colors.black) txtRect = txt.get_rect() txtRect.center = screenrect.center surf.blit(txt, txtRect) if time.time() - self.died > 6: txt2 = abmlFont.render('Press any key to continue', True, colors.black) txt2Rect = txt2.get_rect() txt2Rect.midtop = txtRect.midbottom surf.blit(txt2, txt2Rect) if True in pygame.key.get_pressed(): gameover.endscreen(self.display, self.player) abml = None if self.player.lastMouseY == my: abml = self.player.lastABML elif self.player.lastMouseY < my: abml = assets.abmLauncherDown.copy() elif self.player.lastMouseY > my: abml = assets.abmLauncherUp.copy() self.player.lastABML = abml.copy() abml = self.player.getABML() abmlRect = abml.get_rect() abmlRect.midright = (w, my) abmlText = abmlFont.render(self.player.getAnswer(), True, colors.white) abmlTextRect = abmlText.get_rect() abmlTextRect.center = abmlRect.center surf.blit(abml, abmlRect) surf.blit(abmlText, abmlTextRect) surf.blit(self.player.getGUI(), (0, 0)) if not pygame.mixer.get_busy(): assets.loop1.play() self.display.fill(colors.white) self.display.blit(surf, (0, 0)) pygame.display.update()
def startscreen(display): miscfont = pygame.font.Font('freesansbold.ttf', 32) smallfont = pygame.font.Font('freesansbold.ttf', 16) largeFont = pygame.font.Font('freesansbold.ttf', 30) textFont = pygame.font.Font('freesansbold.ttf', 20) displayrect = display.get_rect() city = assets.city.copy() cityrect = city.get_rect() cityrect.midbottom = displayrect.midbottom title = assets.title.copy() titlerect = title.get_rect() titlerect.midtop = (int(displayrect.w/2), int(displayrect.h/4)) rockets = [] for i in range(0, 5): r = choice([assets.rocket1, assets.rocket2, assets.rocket3_2]) rocket = pygame.transform.rotate(r.copy(), 270) rocketrect = rocket.get_rect() rocketrect.center = randint(0, displayrect.w), randint(0, displayrect.h) rockets.append((rocket, rocketrect)) coin = assets.coin.copy() coinrect = coin.get_rect() coinrect.center = (randint(0, displayrect.w-1), randint(0, displayrect.h-1)) holdingCoin = False recep = sprites.spriteAnimation(assets.receptacle.copy(), 10) receprect = recep.getSurface().get_rect() receprect.midbottom = displayrect.midbottom insertcoin = smallfont.render('Please insert 3.14 cents', True, colors.aqua) insertcoinrect = insertcoin.get_rect() insertcoinrect.midbottom = receprect.midtop startingsoon = miscfont.render('Starting...', True, colors.white) startingsoonrect = startingsoon.get_rect() startingsoonrect.midbottom = displayrect.midbottom highscores = pygame.Surface((350, 210), pygame.SRCALPHA, 32) highscoresrect = highscores.get_rect() highscoresrect.midtop = displayrect.center highscoresborder = highscoresrect.copy() highscoresborder.top -= 10 highscoresborder.left -= 10 n = 0 c = colors.black for name, s in gameover.getHighScores(): highscores.blit(miscfont.render(str(n+1), True, c), (0, n*40)) highscores.blit(textFont.render(name.upper(), True, c), (50, n*40)) highscores.blit(textFont.render(str(s), True, c), (250, n*40)) n += 1 if n == 5: break while True: for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: MAIN.terminate() elif event.type == pygame.MOUSEBUTTONDOWN: if holdingCoin: if receprect.colliderect(coinrect): holdingCoin = False coinrect.bottomright = (0, 0) recep.start() else: holdingCoin = False else: if coinrect.collidepoint(event.pos): holdingCoin = True if holdingCoin: pygame.mouse.set_visible(False) coinrect.center = pygame.mouse.get_pos() else: pygame.mouse.set_visible(True) if recep.isFinished(): raise MAIN.LevelFinished() display.fill(colors.sky_blue) display.blit(city, cityrect) for rocket, rect in rockets: display.blit(rocket, rect) display.blit(title, titlerect) display.blit(recep.getSurface(), receprect) display.blit(insertcoin, insertcoinrect) pygame.draw.rect(display, colors.yellow, highscoresborder) display.blit(highscores, highscoresrect) if recep.hasStarted: display.blit(startingsoon, startingsoonrect) display.blit(coin, coinrect) if not pygame.mixer.get_busy(): assets.loop1.play() pygame.display.update()
def getExplosion(self): return sprites.spriteAnimation(assets.explosion3, 60)