def __init__(self): super().__init__() self.elements.append(objects.Button('ExitButton.png')) self.element_locations.append([479, 359]) self.elements.append(objects.Switch('On', 'Off')) self.element_locations.append([450, 60]) self.elements.append(objects.ButtonText('Clear', 32)) self.element_locations.append([450, 110]) self.elements.append(objects.ImageSwitch('PrevChar1.png', 'PrevChar2.png')) self.element_locations.append([450, 160]) self.elements.append(objects.ImageSwitch('PrevChar2.png', 'PrevChar1.png')) self.element_locations.append([450, 210]) self.elements.append(objects.Text('Music:', 32)) self.element_locations.append([100, 60]) self.elements.append(objects.Text('Settings', 32)) self.element_locations.append([300, 20]) self.elements.append(objects.Text('Clear scoreboard:', 32)) self.element_locations.append([100, 110]) self.elements.append(objects.Text('Player appearance:', 32)) self.element_locations.append([100, 160]) self.elements.append(objects.Text('Enemy appearance:', 32)) self.element_locations.append([100, 210])
def __init__(self): super().__init__() self.elements.append(objects.Button('ExitButton.png')) self.element_locations.append([479, 359]) self.elements.append(objects.Text('Date', 32)) self.element_locations.append([10, 20]) self.elements.append(objects.Text('Deaths', 32)) self.element_locations.append([185, 20]) self.elements.append(objects.Text('Time', 32)) self.element_locations.append([360, 20]) self.elements.append(objects.Text('Score', 32)) self.element_locations.append([535, 20]) scoreboard = open('Files/scoreboard.txt', 'r') scores = [] for line in scoreboard: scores.append(line.split(',')) scoreboard.close() scores.sort(key=lambda x: int(x[-1])) # lambda creates an inline function # Sorts score list based on the value of 'key' # Takes a single argument x and returns int(x[-1]). for i in range(9): try: for j in range(len(scores[-1 - i])): self.elements.append(objects.Text(scores[-1 - i][j], 32)) self.element_locations.append([10 + (j * 175), 50 + (i * 30)]) except IndexError: break
def __init__(self): super().__init__() self.elements.append(objects.Text('I keep having this dream . . .', 48)) self.element_locations.append([10, 10]) self.elements.append(objects.Button('PlayButton.png')) self.element_locations.append([5, 359]) self.elements.append(objects.Button('MenuButton.png')) self.element_locations.append([163, 359]) self.elements.append(objects.Button('ScoreButton.png')) self.element_locations.append([321, 359]) self.elements.append(objects.Button('ExitButton.png')) self.element_locations.append([479, 359]) self.clouds = [] self.cloud_locations = [] self.cloud_speeds = [] for i in range(random.randint(5, 10)): cloud = random.choice(['1', '2', '3']) self.clouds.append(objects.Image('cloud' + cloud + '.png')) self.cloud_locations.append([random.randint(-1000, -100), random.randint(0, 278)]) self.cloud_speeds.append(random.randint(1, 3))
enemy_explode.set_volume(0.1) pygame.mixer.music.load('audio/Concert_Of_The_Aerogami.wav') #fonts font_joystix = 'fonts/joystix monospace.ttf' font_gomarice = 'fonts/gomarice_no_continue.ttf' except Exception as e: print('Not all assets could be loaded: ' + str(e) + ' at line ' + str(e.__traceback__.tb_lineno)) ## menu assets menu_buttons = [] menu_buttons.append( obj.Button(lambda: change_gamestate(Gamestate.RUNNING), (SCREEN_WIDTH // 2) - 100, 250, screen, ' Start! ', font_gomarice, 80, pygame.Color(255, 255, 255), pygame.Color(120, 120, 120))) # button to start the game menu_buttons.append( obj.Button(quit, (SCREEN_WIDTH // 2) - 80, 470, screen, ' QUIT ', font_gomarice, 80, pygame.Color(255, 255, 255), pygame.Color(120, 120, 120))) # button to shut down the game menu_buttons.append( obj.Button(lambda: change_gamestate(Gamestate.SCOREBOARD), (SCREEN_WIDTH // 2) - 100, 360, screen, ' Score ', font_gomarice, 80, pygame.Color(255, 255, 255), pygame.Color(120, 120, 120))) # button to shut down the game menu_images = [] menu_images.append(obj.Image(100, 100, screen, 10, player_sprite)) # player sprite for the menu for x in range(4): menu_images.append(
# Initiate player variables score = 0 maxLives = 2 lives = 2 # Initialize game objects bat = objects.Bat( centreX, windowHeight - 25, pygame, surface, 100, 15) # Will make this wider when more complex bouncing is added ball = objects.Ball(pygame, surface, 15, bat) # Initialise buttons resumeButton = objects.Button(pygame, surface, colours.black, colours.white, (100, 200, 100, 50), "Resume", ubuntuFontSmall) def loseLife(): global lives lives -= 1 if lives < 0: setState(gameOver) else: ball.setSpeed() setState(onBat) def drawLives(asText=False, position=None): global lives
def home(): playText = pygame.font.Font(gameFont, 200).render("PLAY", True, WHITE) berryMode = False slider = pygame_gui.elements.ui_horizontal_slider.UIHorizontalSlider( pygame.Rect((width - (width / 2 * GOLDENRATIO)) / 2, 0, width / 2 * GOLDENRATIO, height / 20), 1, # default (1, 10), # range manager) fpsClock = pygame.time.Clock() while True: fpsClock.tick( maxFPS ) # Ensures that the game will not play higher than maxFPS fps background = repeatTileImage( startBgImage.resize((np.array(startBgImage.size) * 2).astype(int)), (width, height)) # Check if the B key is pressed to activate berry mode if pygame.key.get_pressed()[pygame.K_b]: berryMode = not berryMode time.sleep(0.2) # Draw background win.blit(pygame.transform.scale2x(PIL_to_surface(background)), (0, 0)) # Update and draw playButton with playText and difficultyText on it difficulty = int(round(slider.get_current_value())) difficultyText = pygame.font.Font(gameFont, 200).render( "level " + str(difficulty) + (('B' if berryMode else ('' if difficulty == 10 else ' '))), True, WHITE) fullPlayText = combineSufacesVertical(playText, difficultyText) _width, _height = min((width, height)) / 2 * GOLDENRATIO, min( (width, height)) / 2 playTextWidth = int(_width * 0.9) playTextHeight = int(_height * 0.9) playButton = obj.Button( pygame.Rect((width - _width) / 2, (height - _height) / 2, _width, _height), pygame.transform.smoothscale( fullPlayText, (playTextWidth, playTextHeight )) # Resize playText to fit nice in the startButton ) # Update pygame_gui slider.update(1 / maxFPS) manager.update(1 / maxFPS) if playButton.hover(pygame.mouse.get_pos()) or pygame.key.get_pressed( )[pygame.K_RETURN]: playButton.color = (145, 34, 0) if pygame.mouse.get_pressed()[0] or pygame.key.get_pressed()[ pygame.K_RETURN]: slider.kill() return difficulty, berryMode else: playButton.color = (123, 17, 19) # Draw stuff playButton.draw(win) manager.draw_ui(win) updateWindow()
def pause(bg): ''' The function responsable for the pause menu. The pause manu rect has a golden ratio and the height is: `50*np.sin(i/10) * (1.02**(-i)) + (400-(1000*1/i))` but it's never smaler than 0. ''' fpsClock = pygame.time.Clock() font = pygame.font.Font(gameFont, 200) # Rendering it firs so I can scale the surface instead of the font beacause then it would be really not smooth pauseTextLine1 = font.render(f"Paused, click to continue", True, DARK_BLUE) pauseTextLine2 = font.render(f"Hit SPACE to return home", True, DARK_BLUE) pauseText = combineSufacesVertical(pauseTextLine1, pauseTextLine2) pauseTextAspectRatio = np.divide(*pauseText.get_size()[::-1]) backgroundImage = PIL_to_surface(changeBrightness(surface_to_PIL(bg), 0.3)) hover = 0 frameCount = 1 while True: fpsClock.tick( maxFPS ) # Ensures that the game will not play higher than maxFPS fps win.blit(pygame.transform.scale(backgroundImage, (width, height)), (0, 0)) h = max(50 * np.sin(frameCount / 10) * (1.03**(-frameCount)) + (min( (width, height)) / 2 - (1000 * 1 / (2 * frameCount))), 0) # y = 50 * sin(x/10) * 1.03^-x + (maxWidth -(1000*1/x)) w = GOLDENRATIO * h maxHover = width / 80 hoverSpeed = maxHover / 3 # This ensures a constant magnification time of 0.05 seconds, calculated by (maxHover/hoverSpeed)/fps pauseTextWidth = int(w * 0.9 + hover) # Scle the width to 90% of the pauseButton pauseTextHeight = int( (w * 0.9 + hover) * pauseTextAspectRatio ) # Scale the font but remain the correct aspect ratio pauseButton = obj.Button( pygame.Rect( width / 2 - w / 2 - hover * GOLDENRATIO, # Make sure the button is centered height / 2 - h / 2 - hover, w + 2 * hover * GOLDENRATIO, h + 2 * hover), pygame.transform.smoothscale(pauseText, (pauseTextWidth, pauseTextHeight))) if pygame.key.get_pressed()[pygame.K_SPACE]: return -1 if pauseButton.hover(pygame.mouse.get_pos()): if pygame.mouse.get_pressed()[0]: return pauseButton.color = (110, 166, 255) hover += hoverSpeed hover = min(hover, maxHover) else: pauseButton.color = (66, 135, 245) hover -= hoverSpeed hover = max(hover, 0) pauseButton.draw(win) frameCount += 1 updateWindow()