def death(score): s.high_score = score # Set high score death_text = s.big_font.render("You lost! Score: " + str(score), True, s.WHITE) return_text = s.normal_font.render("Press space to return to home.", True, s.WHITE) while not pygame.key.get_pressed()[pygame.K_SPACE]: s.win.fill(s.BLACK) pygame.draw.rect(s.win, s.RED, (s.WIDTH/20, s.HEIGHT / 5, (s.WIDTH/20)*18, (s.HEIGHT/5)*3)) s.win.blit(death_text, ((s.WIDTH-death_text.get_width()) / 2, (s.HEIGHT-death_text.get_height()) / 2-s.HEIGHT/8)) s.win.blit(return_text, ((s.WIDTH-return_text.get_width()) / 2, ((s.HEIGHT-return_text.get_height()) / 2)+s.HEIGHT/8)) s.check_closed() pygame.display.update()
def pause(background): background = s.PIL_to_surface(s.brightness(s.surface_to_PIL(background), 0.3)) unpauseBtn = pygame.Rect(s.WIDTH/5, s.HEIGHT/3, s.WIDTH/5*3, s.HEIGHT/3) unpauseBtnText = s.big_font.render("Unpause", True, s.WHITE) returnToHomeText = s.normal_font.render("Press SPACE to return to home", True, s.WHITE) while True: s.win.blit(background, (0, 0)) pygame.draw.rect(s.win, s.LIGHT_BLUE, unpauseBtn) s.win.blit(unpauseBtnText, ((s.WIDTH-unpauseBtnText.get_width()) / 2, ((s.HEIGHT-unpauseBtnText.get_height()) / 2))) s.win.blit(returnToHomeText, ((s.WIDTH-returnToHomeText.get_width()) / 2, ((s.HEIGHT-returnToHomeText.get_height()) / 2 + s.HEIGHT/8))) if pygame.key.get_pressed()[pygame.K_SPACE]: return 1 elif unpauseBtn.collidepoint(pygame.mouse.get_pos()) and pygame.mouse.get_pressed()[0]: return s.check_closed() pygame.display.update()
def home(): diff_button = pygame.Rect(s.WIDTH/2-s.WIDTH/4, s.HEIGHT/2+s.WIDTH/16, s.WIDTH/2, s.HEIGHT/4) # High score render high_score_text = s.normal_font.render("High score: " + str(s.high_score), True, s.WHITE) start_button_rect = pygame.Rect((s.WIDTH-s.start_button_image.get_width())/2, (s.HEIGHT-s.start_button_image.get_height())/2-100, s.start_button_image.get_rect()[2], s.start_button_image.get_rect()[3]) diff_state = 1 while not(pygame.mouse.get_pressed()[0] and start_button_rect.collidepoint(pygame.mouse.get_pos())): s.win.fill(s.DARK_GREEN) # High score s.win.blit(high_score_text, (s.WIDTH-high_score_text.get_width(), 0)) # Start button if start_button_rect.collidepoint(pygame.mouse.get_pos()): # Hovering s.win.blit(s.start_button_hover_image, ((s.WIDTH-s.start_button_image.get_width())/2, (s.HEIGHT-s.start_button_image.get_height())/2-100)) else: # Not hovering s.win.blit(s.start_button_image, ((s.WIDTH-s.start_button_image.get_width())/2, (s.HEIGHT-s.start_button_image.get_height())/2-100)) # Difficulty keypresses if diff_button.collidepoint(pygame.mouse.get_pos()): if pygame.key.get_pressed()[pygame.K_1]: diff_state = 1 if pygame.key.get_pressed()[pygame.K_2]: diff_state = 2 if pygame.key.get_pressed()[pygame.K_3]: diff_state = 3 # Difficulty button pygame.draw.rect(s.win, s.FUCHSIA, diff_button) diff_state_text = s.normal_font.render(s.DIFFS[diff_state], True, s.BLACK) s.win.blit(diff_state_text, tuple(np.subtract(diff_button.center, (diff_state_text.get_width()/2, diff_state_text.get_height()/2)))) s.check_closed() pygame.display.update() return diff_state
def win(score): s.high_score = score # Set high score winning_title = s.big_font.render("You won! Score: " + str(score), True, s.WHITE) return_text = s.normal_font.render("Press space to return to home.", True, s.WHITE) while not pygame.key.get_pressed()[pygame.K_SPACE]: s.win.fill(s.BLACK) pygame.draw.rect(s.win, s.ORANGE, (s.WIDTH/14, s.HEIGHT / 5, (s.WIDTH/14)*12, (s.HEIGHT/5)*3)) s.win.blit(winning_title, ((s.WIDTH-winning_title.get_width()) / 2, (s.HEIGHT-winning_title.get_height()) / 2-s.HEIGHT/8)) s.win.blit(return_text, ((s.WIDTH-return_text.get_width()) / 2, ((s.HEIGHT-return_text.get_height()) / 2)+s.HEIGHT/8)) # Easter egg if pygame.key.get_pressed()[pygame.K_KP_ENTER] or pygame.key.get_pressed()[pygame.K_ESCAPE]: if pygame.key.get_pressed()[pygame.K_KP_ENTER] and pygame.key.get_pressed()[pygame.K_ESCAPE]: s.win.blit(s.easterEgg_image_fliped, ((s.WIDTH-s.easterEgg_image_fliped.get_width())/2, (s.HEIGHT-s.easterEgg_image_fliped.get_height())/2)) else: s.win.blit(s.easterEgg_image, ((s.WIDTH-s.easterEgg_image.get_width())/2, (s.HEIGHT-s.easterEgg_image.get_height())/2)) s.check_closed() pygame.display.update()
def main_game(difficulty): ''' This method contains the main game. You should give it one parameter; the difficulty, as a number counting from 1 up to and including 3. ''' # Create blocks s.total_layers = 2**difficulty s.blocksspace = s.BOTTOM_MARGIN-s.TOP_MARGIN s.blockheight = s.blocksspace/s.total_layers s.blockwidth = int(s.WIDTH/(10*difficulty-6)) blocks = [] blocktops, blockbottoms, blocklefts, blockrights = [], [], [], [] for row in range(s.total_layers): for collunm in range(s.WIDTH//s.blockwidth): blocks.append(objects.Block(collunm*s.blockwidth, (row*s.blockheight) + s.TOP_MARGIN, s.blockwidth, s.blockheight, row, s.total_layers)) blocktops.append(blocks[len(blocks)-1].top) blockbottoms.append(blocks[len(blocks)-1].bottom) blocklefts.append(blocks[len(blocks)-1].left) blockrights.append(blocks[len(blocks)-1].right) begin_blockscount = len(blocks) # Create a paddle and ball paddle = objects.Paddle((s.WIDTH/2)-(s.PADDLE_WIDTH/2), s.HEIGHT-50, s.PADDLE_WIDTH, 15, s.paddle_image) ball = objects.Ball(s.WIDTH/2, s.BOTTOM_MARGIN+s.HEIGHT/10, s.BALL_SPEED, 10, s.ball_image, s.bounce_sound) start = time.time() # Main loop run = True while run: s.fpsClock.tick(60) s.win.fill(s.BLACK) score = int(begin_blockscount-len(blocks)-((time.time()-start)/(difficulty*10))) + 1 score_text = s.normal_font.render(str(score), True, s.WHITE) s.win.blit(score_text, (s.WIDTH-score_text.get_width()-10, s.HEIGHT-score_text.get_height()-10)) # Paddle control if pygame.mouse.get_pos()[0] > 0+(s.PADDLE_WIDTH/2) and pygame.mouse.get_pos()[0] < s.WIDTH-(s.PADDLE_WIDTH/2): paddle.control((difficulty-1)*50, (time.time()-start)) elif pygame.mouse.get_pos()[0] < 0+(s.PADDLE_WIDTH/2): paddle.x = 0 elif pygame.mouse.get_pos()[0] > s.WIDTH-(s.PADDLE_WIDTH/2): paddle.x = s.WIDTH-paddle.width paddle.draw() # Block control if len(blocks) == 0: scenes.win(score) return for block in blocks: block.draw() # Ball control if ball.rect.collidelist(blocks) != -1: ball.collided_block = ball.rect.collidelist(blocks) if blocks[ball.collided_block].layer == 0: ball.speed = s.BALL_SPEED+difficulty*2 elif ball.speed != s.BALL_SPEED+difficulty: ball.speed = s.BALL_SPEED+difficulty + (ball.speed-s.BALL_SPEED)*0.5 if blocktops[ball.collided_block].colliderect(ball.rect): ball.bounce_top() elif blockbottoms[ball.collided_block].colliderect(ball.rect): ball.bounce_bottom() elif blocklefts[ball.collided_block].colliderect(ball.rect): ball.bounce_left() elif blockrights[ball.collided_block].colliderect(ball.rect): ball.bounce_right() del blocks[ball.collided_block] del blocktops[ball.collided_block] del blockbottoms[ball.collided_block] del blocklefts[ball.collided_block] del blockrights[ball.collided_block] # Reset ball position if pygame.key.get_pressed()[pygame.K_r]: ball = objects.Ball(s.WIDTH/2, s.BOTTOM_MARGIN+s.HEIGHT/10, s.BALL_SPEED, 10, s.ball_image, s.bounce_sound) # Ball and paddle collide if pygame.sprite.collide_rect(ball, paddle): ball.alpha = np.random.uniform(0, -np.pi) if ball.rect.colliderect(paddle.leftrect): ball.bounce_left() elif ball.rect.colliderect(paddle.rightrect): ball.bounce_right() if (ball.rect.y+(ball.radius*2)) >= s.HEIGHT: scenes.death(score) return ball.update() ball.draw() if pygame.key.get_pressed()[pygame.K_ESCAPE]: pause = scenes.pause(pygame.display.get_surface()) if pause: return s.check_closed() pygame.display.update()