Hi, so im new to this forum as well as python and I decided to create a little game to learn the language a couple of days ago (The game being a cube that dodges other cubes and get score when the other cubes reach bottom of screen. The speed increases at the same time as the score).
The game works just fine and I am looking to improve it. The first improve I am working on is a title screen with buttons. The title screen seems to work (at least I think the problem isn't there), but my game lacks a bunch of features it had before i decided to add the title screen
. Some examples of functionalities that doesn't appear in the "new" game are:
1. The speed doesn't increase anymore even if the score increases (stays stuck on its base value without changing like the code tells it to do).
2. I drew a line in the middle of the screen and if the player goes higher than that line, he is supposed to earn double the score (he doesn't in the "new" game).
My guess is that the problem must be into my bad use of functions though I have no idea where i messed up
. If someone could replie to identifie my mistake it would be really appreciated!
The next two sections will contain the code of the game alone and the one with the title screen respectively.
The game works just fine and I am looking to improve it. The first improve I am working on is a title screen with buttons. The title screen seems to work (at least I think the problem isn't there), but my game lacks a bunch of features it had before i decided to add the title screen

1. The speed doesn't increase anymore even if the score increases (stays stuck on its base value without changing like the code tells it to do).
2. I drew a line in the middle of the screen and if the player goes higher than that line, he is supposed to earn double the score (he doesn't in the "new" game).
My guess is that the problem must be into my bad use of functions though I have no idea where i messed up


import pygame import sys import random pygame.init() HEIGHT = 600 WIDTH = 800 GREEN = (0,255,0) BLACK = (0,0,0) RED = (255,0,0) YELLOW = (255,255,0) WHITE = (255,255,255) BLUE = (0,0,255) middle_line_pos = [0, HEIGHT/2] player_size = 46 player_pos = [WIDTH/2-player_size/2, HEIGHT-2*player_size] player_x = player_pos[0] player_y = player_pos[1] enemy_size = 46 enemy_pos = [random.randint(0, WIDTH-enemy_size), 0] enemy_list = [enemy_pos] SPEED = 10 screen = pygame.display.set_mode((WIDTH, HEIGHT)) game_over = False SCORE = 0 clock = pygame.time.Clock() myFont = pygame.font.SysFont("monospace", 35) def set_level(SCORE, SPEED): SPEED = SCORE/15 + 5 return SPEED def drop_enemies(enemy_list): delay = random.random() if len(enemy_list) < 10 and delay < 0.1: x_pos = random.randint(0, WIDTH-enemy_size) y_pos = 0 enemy_list.append([x_pos, y_pos]) def draw_enemies(enemy_list): for enemy_pos in enemy_list: pygame.draw.rect(screen, BLACK, (enemy_pos[0]-2, enemy_pos[1]-2, 50, 50)) pygame.draw.rect(screen, RED, (enemy_pos[0], enemy_pos[1], enemy_size, enemy_size)) def update_enemy_positions(enemy_list, SCORE): for idx, enemy_pos in enumerate(enemy_list): if enemy_pos[1] >= 0 and enemy_pos[1] < HEIGHT: enemy_pos[1] += SPEED else: if player_pos[1] < HEIGHT/2: enemy_list.pop(idx) SCORE += 2 else: enemy_list.pop(idx) SCORE += 1 return SCORE def collision_check(enemy_list, player_pos): for enemy_pos in enemy_list: if detect_collision(enemy_pos, player_pos): return True return False def detect_collision(player_pos, enemy_pos): p_x = player_pos[0] p_y = player_pos[1] e_x = enemy_pos[0] e_y = enemy_pos[1] if ((e_x >= p_x and e_x <= (p_x + player_size)) or (p_x >= e_x and p_x <= (e_x + enemy_size))): if (e_y >= p_y and e_y <= (p_y + player_size) or (p_y >= e_y and p_y <= (e_y + enemy_size))): return True return False while not game_over: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() if event.type == pygame.KEYDOWN: x = player_pos[0] y = player_pos[1] if event.key == pygame.K_LEFT: x -= player_size elif event.key == pygame.K_RIGHT: x += player_size elif event.key == pygame.K_UP: y -= player_size elif event.key == pygame.K_DOWN: y += player_size player_pos = [x, y] screen.fill(WHITE) #if enemy_pos[1] >= 0 and enemy_pos[1] < HEIGHT: #enemy_pos[1] += SPEED #else: #enemy_pos[0] = random.randint(0, WIDTH-enemy_size) #enemy_pos[1] = 1 if detect_collision(player_pos, enemy_pos): game_over = True elif (player_pos[0] < 0) or (player_pos[0] > WIDTH-player_size) or (player_pos[1] < 0) or (player_pos[1] > HEIGHT-player_size): game_over = True drop_enemies(enemy_list) SCORE = update_enemy_positions(enemy_list, SCORE) SPEED = set_level(SCORE, SPEED) text = "Score:" + " " + str(SCORE) label = myFont.render(text, 1, BLACK) screen.blit(label, (WIDTH-200, HEIGHT-40)) if collision_check(enemy_list, player_pos): game_over = True draw_enemies(enemy_list) pygame.draw.rect(screen, BLACK, (player_pos[0]-2, player_pos[1]-2, 50, 50)) pygame.draw.rect(screen, GREEN, (player_pos[0], player_pos[1], player_size, player_size)) pygame.draw.rect(screen, BLUE, (middle_line_pos[0], middle_line_pos[1], WIDTH, 2)) clock.tick(30) pygame.display.update() if game_over: print("Game Over!") print("You're score is" + " " + str(SCORE)) print("Try again!") else: pass
import pygame import time import random import sys pygame.init() intro = True WIDTH = 800 HEIGHT = 600 BLACK = (0,0,0) WHITE = (255,255,255) RED_B = (255,0,0) GREEN_B = (0,255,0) YELLOW = (255,255,0) BLUE_B = (0,0,255) RED = (200,0,0) BLUE = (0,0,200) GREEN = (0,200,0) middle_line_pos = [0, HEIGHT/2] player_size = 46 player_pos = [WIDTH/2-player_size/2, HEIGHT-2*player_size] player_x = player_pos[0] player_y = player_pos[1] enemy_size = 46 enemy_pos = [random.randint(0, WIDTH-enemy_size), 0] enemy_list = [enemy_pos] game_over = False SCORE = 0 SPEED = 10 myFont = pygame.font.SysFont("monospace", 35) gameDisplay = pygame.display.set_mode((WIDTH,HEIGHT)) pygame.display.set_caption('Dodge the Blocks') clock = pygame.time.Clock() def set_level(SCORE, SPEED): SPEED = SCORE/15 + 5 return SPEED def drop_enemies(enemy_list): delay = random.random() if len(enemy_list) < 10 and delay < 0.1: x_pos = random.randint(0, WIDTH-enemy_size) y_pos = 0 enemy_list.append([x_pos, y_pos]) def draw_enemies(enemy_list): for enemy_pos in enemy_list: pygame.draw.rect(gameDisplay, BLACK, (enemy_pos[0]-2, enemy_pos[1]-2, 50, 50)) pygame.draw.rect(gameDisplay, RED, (enemy_pos[0], enemy_pos[1], enemy_size, enemy_size)) def update_enemy_positions(enemy_list, SCORE): for idx, enemy_pos in enumerate(enemy_list): if enemy_pos[1] >= 0 and enemy_pos[1] < HEIGHT: enemy_pos[1] += SPEED else: if player_pos[1] < HEIGHT/2: enemy_list.pop(idx) SCORE += 2 else: enemy_list.pop(idx) SCORE += 1 return SCORE def detect_collision(player_pos, enemy_pos): p_x = player_pos[0] p_y = player_pos[1] e_x = enemy_pos[0] e_y = enemy_pos[1] if ((e_x >= p_x and e_x <= (p_x + player_size)) or (p_x >= e_x and p_x <= (e_x + enemy_size))): if (e_y >= p_y and e_y <= (p_y + player_size) or (p_y >= e_y and p_y <= (e_y + enemy_size))): return True return False def collision_check(enemy_list, player_pos): for enemy_pos in enemy_list: if detect_collision(enemy_pos, player_pos): return True return False def text_objects(text, font): textSurface = font.render(text, True, BLACK) return textSurface, textSurface.get_rect() def message_display(text): largeText = pygame.font.Font('freesansbold.ttf',75) TextSurf, TextRect = text_objects(text, largeText) TextRect.center = ((HEIGHT/2),(WIDTH/2)) gameDisplay.blit(TextSurf, TextRect) pygame.display.update() time.sleep(2) pygame.quit() quit() def button(msg,x,y,w,h,ic,ac,action=None): mouse = pygame.mouse.get_pos() click = pygame.mouse.get_pressed() print(click) if x+w > mouse[0] > x and y+h > mouse[1] > y: pygame.draw.rect(gameDisplay, ac,(x,y,w,h)) if click[0] == 1 and action != None: if action == "play": game_loop() elif action == "quit": pygame.quit() quit() def game_intro(): intro = True while intro: for event in pygame.event.get(): #print(event) if event.type == pygame.QUIT: pygame.quit() quit() gameDisplay.fill(BLUE) largeText = pygame.font.Font('freesansbold.ttf',75) TextSurf, TextRect = text_objects("Dodge the Blocks", largeText) TextRect.center = ((WIDTH/2),(HEIGHT/4)) gameDisplay.blit(TextSurf, TextRect) mouse = pygame.mouse.get_pos() if 150+150 > mouse[0] > 125 and 350+50 > mouse[1] > 350: pygame.draw.rect(gameDisplay, GREEN_B,(125,350,150,50)) else: pygame.draw.rect(gameDisplay, GREEN,(125,350,150,50)) if 150+525 > mouse[0] > 525 and 350+50 > mouse[1] > 350: pygame.draw.rect(gameDisplay, RED_B,(525,350,150,50)) else: pygame.draw.rect(gameDisplay, RED,(525,350,150,50)) button("Singleplayer", 125, 350, 150, 50, GREEN, GREEN_B, "play") button("Multiplayer", 525, 350, 150, 50, RED, RED_B, "quit") smallText = pygame.font.Font("freesansbold.ttf",20) textSurf, textRect = text_objects("Singleplayer", smallText) textRect.center = ( (150+(100/2)), (350+(50/2)) ) gameDisplay.blit(textSurf, textRect) smallText = pygame.font.Font("freesansbold.ttf",20) textSurf, textRect = text_objects("Multiplayer", smallText) textRect.center = ( (75+(525)), (350+(50/2)) ) gameDisplay.blit(textSurf, textRect) pygame.display.update() clock.tick(15) ######################################################################## #if game_over: #print("Game Over!") #print("You're score is" + " " + str(SCORE)) # print("Try again!") #else: #pass def game_loop(): game = True middle_line_pos = [0, HEIGHT/2] player_size = 46 player_pos = [WIDTH/2-player_size/2, HEIGHT-2*player_size] player_x = player_pos[0] player_y = player_pos[1] enemy_size = 46 enemy_pos = [random.randint(0, WIDTH-enemy_size), 0] enemy_list = [enemy_pos] SPEED = 10 SCORE = 0 while game: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() if event.type == pygame.KEYDOWN: x = player_pos[0] y = player_pos[1] if event.key == pygame.K_LEFT: x -= player_size elif event.key == pygame.K_RIGHT: x += player_size elif event.key == pygame.K_UP: y -= player_size elif event.key == pygame.K_DOWN: y += player_size player_pos = [x, y] gameDisplay.fill(WHITE) #if enemy_pos[1] >= 0 and enemy_pos[1] < HEIGHT: #enemy_pos[1] += SPEED #else: #enemy_pos[0] = random.randint(0, WIDTH-enemy_size) #enemy_pos[1] = 1 if detect_collision(player_pos, enemy_pos): message_display('Game Over!') elif (player_pos[0] < 0) or (player_pos[0] > WIDTH-player_size) or (player_pos[1] < 0) or (player_pos[1] > HEIGHT-player_size): message_display('Game Over!') drop_enemies(enemy_list) SCORE = update_enemy_positions(enemy_list, SCORE) SPEED = set_level(SCORE, SPEED) text = "Score:" + " " + str(SCORE) label = myFont.render(text, 1, BLACK) gameDisplay.blit(label, (WIDTH-200, HEIGHT-40)) if collision_check(enemy_list, player_pos): message_display('Game Over!') draw_enemies(enemy_list) pygame.draw.rect(gameDisplay, BLACK, (player_pos[0]-2, player_pos[1]-2, 50, 50)) pygame.draw.rect(gameDisplay, GREEN, (player_pos[0], player_pos[1], player_size, player_size)) pygame.draw.rect(gameDisplay, BLUE, (middle_line_pos[0], middle_line_pos[1], WIDTH, 2)) clock.tick(30) pygame.display.update() #if game_over: #print("Game Over!") #print("You're score is" + " " + str(SCORE)) # print("Try again!") #else: #pass game_intro() pygame.quit() quit()