Hello,
i’m coding a game in python with pygame in which there should be a start menu and several levels. however when i choose as a player to play a level it executes the program (you can see it with the print and log) but it doesn’t display the level as far as the images are concerned.
here’s the code in question:
import pygame
import random
import sys
import os
import math
from pathlib import Path
from datetime import datetime
import pandas as pd
import numpy as np
import time
import logging
from pygame.locals import *
# example imports from custom classes
from objects.window_features import Window #imports from folder objekt (file window_features.py) the class "Window"
from objects.ball import Ball #imports from folder objekt (file ball.py) the class "Ball"
from objects.paddle import Paddle #imports from folder objekt (file paddle.py) the class "Paddle"
from objects.bricks import Bricks #imports from folder objekt (file bricks.py) the class "bricks"
from objects.power_ups import power_ups
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger('my_first_logger')
if __name__ == '__main__': #is only executed, if file "main.py" is opened and executed; is ignorded if file is importet from another file
logging.info(f"main started")
# initialize pygame
pygame.init()
#initialize font
pygame.font.init()
SMALL_FONT = pygame.font.Font(None, 24)
MEDIUM_FONT = pygame.font.Font(None, 28)
LARGE_FONT = pygame.font.Font(None, 32)
# pygame setting variables
FPS = 50
# defines how much lives the player has
life = 3
heart = ('herz1.png')
heart = pygame.image.load(heart)
# image was taken on the folowing wed site https://cursefire.com/minecraft/texture-packs/hardcore-hearts-for-1-17 and has been modified to match with the background
#image for menu background
menu_background = ('riss-in-wand.jpg')
menu_background = pygame.image.load(menu_background)
# image was taken on the folowing wed site https://ratgeber.blauarbeit.de/bauen/risse-in-der-wand
#image for bockground_lvl1
background_lvl1 = ('hintergrund.jpeg')
background_lvl1 = pygame.image.load(background_lvl1)
# image was taken on the folowing wed site image comes from https://www.google.com/url?sa=i&url=https%3A%2F%2Fwww.tonytextures.de%2Fkostenlose-himmel-und-wolken-textur-foto-sammlung%2F&psig=AOvVaw3XdS8h6oA0o4zAaPoQYIVI&ust=1736508095513000&source=images&cd=vfe&opi=89978449&ved=0CBEQjhxqFwoTCOiym_XC6IoDFQAAAAAdAAAAABAE
#create a dictionary with all bricks of level 1 : the key is a specific number to each brick to indentify it and the value is 1 if the brick is existing and 0 if it is not
bricks_of_level1 = {101 : 1, 102 : 1, 103 : 1, 104 : 1, 105 : 1, 106 : 1, 107 : 1, 108 : 1, 109 : 1, 110 : 1, 111 : 1,
202 : 1, 203 : 1, 204 : 1, 205 : 1, 206 : 1, 207 : 1, 208 : 1, 209 : 1, 210 : 1,
303 : 1, 304 : 1, 305 : 1, 306 : 1, 307 : 1, 308 : 1, 309 : 1,
404 : 1, 405 : 1, 406 : 1, 407 : 1, 408 : 1,
505 : 1, 506 : 1, 507 : 1}
special_bricks1 = []
def create_brick1() :
for i in bricks_of_level1 :
# depending on the position of the brick defined by the index in the dictionary bricks_of_level we define 2 variables to give the bricks the right position on the screen l = i // 100
l = i // 100
h = i % 100 - 1
brick_health = 1
if 203 <= i <= 209 :
brick_health = 2
if 304 <= i <= 308 or 405 <= i <= 407 :
brick_health = 3
if i == 305 or i == 307 :
brick_health = 5
if i == 306 :
brick_health = 6
if i == 406 :
brick_health = 4
brick_h1_l1 = Bricks(x = h * brick_center_width , y = 200 - l * brick_center_height, width = brick_center_width, height = brick_center_height, health = brick_health, color='red')
bricks_of_level_classes[i] = brick_h1_l1
logging.info(f"brick position, size, health, color is initialised")
#create a dictionary with all bricks of level 2 : the key is a specific number to each brick to indentify it and the value is 1 if the brick is existing and 0 if it is not
bricks_of_level2 = {103 : 1, 104 : 1, 105 : 1, 106 : 1, 107 : 1, 108 : 1, 109 : 1,
202 : 1, 203 : 1, 204 : 1, 205 : 1, 206 : 1, 207 : 1, 208 : 1, 209 : 1, 210 : 1,
301 : 1, 302 : 1, 303 : 1, 304 : 1, 305 : 1, 306 : 1, 307 : 1, 308 : 1, 309 : 1, 310 : 1, 311 : 1,
402 : 1, 403 : 1, 404 : 1, 405 : 1, 406 : 1, 407 : 1, 408 : 1, 409 : 1, 410 : 1,
503 : 1, 504 : 1, 505 : 1, 506 : 1, 507 : 1, 508 : 1, 509 : 1}
special_bricks2 =[]
def create_bricks2() :
for i in bricks_of_level2 :
# depending on the position of the brick defined by the index in the dictionary bricks_of_level we define 2 variables to give the bricks the right position on the screen
l = i // 100
h = i % 100 - 1
brick_health = 1
if 203 <= i <= 209 or 302 <= i <= 310 or 403 <= i <= 409 :
brick_health = 2
if 204 <= i <= 208 or 303 <= i <= 309 or 404 <= i <= 408 :
brick_health = 3
if i == 304 or i == 308 :
brick_health = 4
if i == 205 or i == 207 or i == 405 or i == 407:
brick_health = 6
if i == 206 or i == 305 or i == 307 or i == 406 :
brick_health = 5
if i == 306 :
brick_health = 7
# defines the brick position, size, and health and temporarly the color
brick_h1_l1 = Bricks(x = h * brick_center_width , y = 200 - l * brick_center_height, width = brick_center_width, height = brick_center_height, health = brick_health, color='red')
bricks_of_level_classes[i] = brick_h1_l1
logging.info(f"brick position, size, health, color is initialised")
#create a dictionary with all bricks of level 3 : the key is a specific number to each brick to indentify it and the value is 1 if the brick is existing and 0 if it is not
bricks_of_level3 = {101 : 1, 102 : 1, 103 : 1, 104 : 1, 105 : 1, 106 : 1, 107 : 1, 108 : 1, 109 : 1, 110 : 1, 111 : 1,
201 : 1, 202 : 1, 203 : 1, 204 : 1, 205 : 1, 206 : 1, 207 : 1, 208 : 1, 209 : 1, 210 : 1, 211 : 1,
301 : 1, 302 : 1, 303 : 1, 304 : 1, 305 : 1, 306 : 1, 307 : 1, 308 : 1, 309 : 1, 310 : 1, 311 : 1,
401 : 1, 402 : 1, 403 : 1, 404 : 1, 405 : 1, 406 : 1, 407 : 1, 408 : 1, 409 : 1, 410 : 1, 411 : 1,
501 : 1, 502 : 1, 503 : 1, 504 : 1, 505 : 1, 506 : 1, 507 : 1, 508 : 1, 509 : 1, 510 : 1, 511 : 1}
special_bricks3 = [306]
def create_bricks3() :
for i in bricks_of_level3 :
# depending on the position of the brick defined by the index in the dictionary bricks_of_level we define 2 variables to give the bricks the right position on the screen
l = i // 100
h = i % 100 - 1
if i > 500 :
brick_health = 5
elif 500 > i > 400 :
brick_health = 4
elif 400 > i > 300 :
brick_health = 3
elif 300 > i > 200 :
brick_health = 2
elif 200 > i > 100 : #TODO : let the bricks have a short distance between each other at least visualy
brick_health = 1
# defines the brick position, size, and health and temporarly the color
if i == 305 or i == 307 or i == 406 or i == 206 :
brick_health = 7
elif i == 306 :
brick_health = random.randint(1,7)
# defines the brick position, size, and health and temporarly the color
brick_h1_l1 = Bricks(x = h * brick_center_width , y = 250 - l * brick_center_height, width = brick_center_width, height = brick_center_height, health = brick_health, color='red')
bricks_of_level_classes[i] = brick_h1_l1
logging.info(f"brick position, size, health, color is initialised")
#create a dictionary with all bricks of level 4 : the key is a specific number to each brick to indentify it and the value is 1 if the brick is existing and 0 if it is not
bricks_of_level4 = {104 : 1, 105 : 1, 107 : 1, 108 : 1,
205 : 1, 208 : 1,
305 : 1, 306 : 1, 307 : 1, 308 : 1,
404 : 1, 405 : 1, 406 : 1, 407 : 1, 408 : 1, 409 : 1,
504 : 1, 505 : 1, 506 : 1, 507 : 1, 508 : 1, 509 : 1, 510 : 1, 511 : 1,
603 : 1, 604 : 1, 605 : 1, 606 : 1, 607 : 1, 608 : 1, 609 : 1, 610 : 1,
701 : 1, 702 : 1, 703 : 1, 704 : 1, 705 : 1, 706 : 1, 707 : 1, 708 : 1,
802 : 1, 803 : 1, 804 : 1, 805 : 1, 806 : 1,
903 : 1, 904 : 1, 905 : 1, 906 : 1,
1004 : 1, 1005 : 1}
special_bricks4 = [505,506,605,606]
def create_bricks4() :
for i in bricks_of_level4 :
# depending on the position of the brick defined by the index in the dictionary bricks_of_level we define 2 variables to give the bricks the right position on the screen
l = i // 100
h = i % 100 - 1
brick_health = 3
if 104 <= i <= 208 or i == 701 or i == 702 or i == 802 :
brick_health = 4
elif i == 505 or i == 506 or i == 605 or i ==606 :
brick_health = random.randint(1,7)
elif i == 804 :
brick_health = 7
# defines the brick position, size, and health and temporarly the color
brick_h1_l1 = Bricks(x = h * brick_center_width , y = 300 - l * brick_center_height, width = brick_center_width, height = brick_center_height, health = brick_health, color='red')
bricks_of_level_classes[i] = brick_h1_l1
logging.info(f"brick position, size, health, color is initialised")
#create a dictionary with all bricks of level 5 : the key is a specific number to each brick to indentify it and the value is 1 if the brick is existing and 0 if it is not
bricks_of_level5 = {101 : 1, 102 : 1, 103 : 1, 104 : 1, 105 : 1, 106 : 1, 107 : 1, 108 : 1, 109 : 1, 110 : 1, 111 : 1,
201 : 1, 202 : 1, 203 : 1, 204 : 1, 205 : 1, 206 : 1, 207 : 1, 208 : 1, 209 : 1, 210 : 1, 211 : 1,
301 : 1, 302 : 1, 303 : 1, 304 : 1, 305 : 1, 306 : 1, 307 : 1, 308 : 1, 309 : 1, 310 : 1, 311 : 1,
401 : 1, 402 : 1, 403 : 1, 404 : 1, 405 : 1, 406 : 1, 407 : 1, 408 : 1, 409 : 1, 410 : 1, 411 : 1,
501 : 1, 502 : 1, 503 : 1, 504 : 1, 505 : 1, 506 : 1, 507 : 1, 508 : 1, 509 : 1, 510 : 1, 511 : 1}
special_bricks5 = [110,206,210,306,402,406,410,506 ]
def create_bricks5() :
for i in bricks_of_level5 :
# depending on the position of the brick defined by the index in the dictionary bricks_of_level we define 2 variables to give the bricks the right position on the screen
l = i // 100
h = i % 100 - 1
brick_health = 2
if h == 0 or ((h == 4 or h==6) and l!=1) or ((h == 8 or h==10) and l!=5) or i == 106 or i == 302 or i == 303 or i == 310 or i == 403 or i == 502 or i == 510 :
brick_health = 7
elif i == 110 or i == 206 or i == 210 or i == 306 or i == 402 or i == 406 or i == 410 or i == 506 :
brick_health = random.randint(1,7)
# defines the brick position, size, and health and temporarly the color
brick_h1_l1 = Bricks(x = h * brick_center_width , y = 300 - l * brick_center_height, width = brick_center_width, height = brick_center_height, health = brick_health, color='red')
bricks_of_level_classes[i] = brick_h1_l1
logging.info(f"brick position, size, health, color is initialised")
# the keys of these dictionary go from 101 to 511 the first number corresponds to the height of the block and the second the position in the line
# So brick number 209 is on the 2nd(lowest) line and on the 09th position with start on the left
logging.info(f"dictionary of all bricks is created")
#create a dictionary with all balls : the key is key to indentify it and the value is 1 if the brick is existing and 0 if it is not
ball_of_level = {1:1}
logging.info(f"dictionary of all balls is created and first ball is saved")
# create a game screen (700, 525 works good, defined in class "Window")
window = Window() #creates instance "window" in class "Window"
screen = pygame.display.set_mode(size=(window.display_width, window.display_height)) #creates screen with attributs defined in class "Window"
# initialize the clock for FPS calculation
clock = pygame.time.Clock() #How much frames per second? clock counts the time
# initialize a ball
balls_of_level_classes = {}
default_speed = 5
ball_speed = default_speed
ball = Ball(x = window.display_width//2, y = 400, dx = 0.2, dy = 1.2, radius = 15, speed = ball_speed, color='black') #start position and moving direction/speed (dx is random), size of the ball, colour
balls_of_level_classes[1] = ball
number_balls=1 # number of balls
probability_new_ball=0 # probabitlity to have a new ball
# initialize a paddle
paddle_height = 10
paddle_width = 80
paddle_default_speed = 10
paddle = Paddle(window.display_width//2, window.display_height-paddle_height, paddle_width, paddle_height) #position and size changed vor debugging of collision
#initialize a brick
brick_center_width = window.display_width / 11
brick_center_height = window.display_height /30
bricks_of_level_classes = {}
winned_levels = {1 : False, 2 : False, 3 : False, 4 : False}
#define score
begin_score = 0
for i in bricks_of_level_classes :
begin_score += bricks_of_level_classes[i].health * 10
logging.info(f"score is defined")
running = True
left = False
right = False
pass_level = False
end_game = False
win = True
loose = False
pause = False
level_reset = False
choosed_level = False
text_place= 0
pseudonym = ""
change_pseudo = False
click = False
continu = False
pygame.mixer.music.load('Tichy_Willkommen Student.mp3')
pygame.mixer.music.play(-1,0.0)
pygame.mixer.music.set_volume(1)
logging.info(f"music starts")
while running:
# always draw a black screen. then add objects as needed.
screen.fill('black')
scaled_background_menu = pygame.transform.scale(menu_background, (window.display_width, window.display_height))
screen.blit(scaled_background_menu, (0,0))
#get position of the mouse
mouse_x, mouse_y = pygame.mouse.get_pos()
if 50 < window.display_height/2 - text_place :
# Welkome text
Wlk_text = LARGE_FONT.render("Willkommen, Bienvenue, WELCOME", True, 'Black')
Wlk_text_position = Wlk_text.get_rect(center = (window.display_width/2, window.display_height/2 - text_place))
screen.blit(Wlk_text, Wlk_text_position)
TBO_text = MEDIUM_FONT.render("to Break out", True, 'Black')
TBO_text_position = TBO_text.get_rect(center = (window.display_width/2, window.display_height/2 + 50 - text_place))
screen.blit(TBO_text, TBO_text_position)
text_place +=1
else :
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN: #key is pressed down
if event.key == pygame.K_ESCAPE :
end_game = True
logging.info(f"user ended game")
elif event.key == pygame.K_RETURN :
change_pseudo = not change_pseudo
elif event.key == pygame.K_BACKSPACE and change_pseudo :
pseudonym = pseudonym[:-1]
elif event.key == pygame.K_1 :
winned_levels[1] = not winned_levels[1]
elif event.key == pygame.K_2 :
winned_levels[2] = not winned_levels[2]
elif event.key == pygame.K_3 :
winned_levels[3] = not winned_levels[3]
elif event.key == pygame.K_4 :
winned_levels[4] = not winned_levels[4]
elif change_pseudo :
pseudonym += event.unicode
elif event.type == pygame.KEYUP:
if event.key == pygame.K_ESCAPE :
end_game = False
elif event.key == pygame.K_RETURN :
pass
elif event.key == pygame.K_BACKSPACE or event.key == pygame.K_1 or event.key == pygame.K_2 or event.key == pygame.K_3 or event.key == pygame.K_4 :
pass
elif event.type == pygame.MOUSEBUTTONDOWN:
click = True
elif event.type == pygame.MOUSEBUTTONUP:
click = False
if end_game == True : #if escape is pressed end game by quitting pygame
pygame.quit()
running = False
logging.info(f"ending game is succesfully executed")
# Welkome text
Wlk_text = LARGE_FONT.render("Willkommen, Bienvenue, WELCOME", True, 'Black')
Wlk_text_position = Wlk_text.get_rect(center = (window.display_width/2, 50))
screen.blit(Wlk_text, Wlk_text_position)
TBO_text = MEDIUM_FONT.render("to Break out", True, 'Black')
TBO_text_position = TBO_text.get_rect(center = (window.display_width/2, 100))
screen.blit(TBO_text, TBO_text_position)
# Pseudonym
pls = SMALL_FONT.render("Please press return to start/end enter your name", True, 'blue')
pls_position = pls.get_rect(center = (window.display_width/2 , 150))
screen.blit(pls,pls_position)
pseudo = MEDIUM_FONT.render(pseudonym, True, 'Blue')
pseudo_postion = pseudo.get_rect(center = (window.display_width/2, 150 + 100/3))
screen.blit(pseudo, pseudo_postion)
# Impressum
Impr2 = SMALL_FONT.render("Many thanks to Prof. Walter Tichy for his extraordinary vocal talent ", True, 'Black')
Impr2_position = Impr2.get_rect(center = (window.display_width/2 , window.display_height - 200/3))
screen.blit(Impr2, Impr2_position)
Impr3 = SMALL_FONT.render("and his permission to immortalize it in this game.", True, 'Black')
Impr3_position = Impr3.get_rect(center = (window.display_width/2 , window.display_height - 100/3))
screen.blit(Impr3, Impr3_position)
# Showing buttons to choose level if the player has completed level 1 he can play level 2 and 1 but not 3 if he has completed level 2 he can play level 1, 2 and 3 else he only can play level 1
# if the player can play a level the button is red else it is gray
color_lvl2 = 'gray25'
color_lvl3 = 'gray25'
color_lvl4 = 'gray25'
color_lvl5 = 'gray25'
if winned_levels[1] :
color_lvl2 = 'red'
if winned_levels[2] :
color_lvl3 = 'red'
if winned_levels[3] :
color_lvl4 = 'red'
if winned_levels[4] :
color_lvl5 = 'red'
rect_width = window.display_width/2 - 100
rect_height = (window.display_height - 110 - (150 + 100/3))/3 - 50/3 # height of buttons addapted to the place taken by the welkome, pseudo, and impressum text
corner_radius = 15
border_width = 15
pygame.draw.rect(screen, 'red', (50, 200, rect_width, rect_height), border_width, corner_radius) # rect top left
pygame.draw.rect(screen, color_lvl2, (window.display_width/2 + 50, 200, rect_width, rect_height), border_width, corner_radius) # rect top right
pygame.draw.rect(screen, color_lvl3, (50, 200 + rect_height + 25, rect_width, rect_height), border_width, corner_radius) # rect mittle left
pygame.draw.rect(screen, color_lvl4, (window.display_width/2 + 50, 200 + rect_height + 25, rect_width, rect_height), border_width, corner_radius) # rect mittle right
pygame.draw.rect(screen, color_lvl5, (50, 200 + 2 * rect_height + 50, rect_width, rect_height), border_width, corner_radius) # rect bottom left
pygame.draw.rect(screen, 'red', (window.display_width/2 + 50, 200 + 2 * rect_height + 50, rect_width, rect_height), border_width, corner_radius) # rect bottom right
lvl1_text = SMALL_FONT.render("Level 1", True, 'red')
lvl1_text_position = lvl1_text.get_rect(center = (50 + rect_width/2 , 200 + rect_height / 2)) # position lvl1_text in rect top left
screen.blit(lvl1_text, lvl1_text_position)
lvl2_text = SMALL_FONT.render("Level 2", True, color_lvl2)
lvl2_text_position = lvl2_text.get_rect(center = (window.display_width/2 + 50 + rect_width/2 , 200 + rect_height / 2)) # position lvl2_text in rect top right
screen.blit(lvl2_text, lvl2_text_position)
lvl3_text = SMALL_FONT.render("Level 3", True, color_lvl3)
lvl3_text_position = lvl3_text.get_rect(center = (50 + rect_width/2 , 200 + 3 * rect_height / 2 + 25)) # position lvl3_text in rect bottom left
screen.blit(lvl3_text, lvl3_text_position)
lvl4_text = SMALL_FONT.render("Level 4", True, color_lvl4)
lvl4_text_position = lvl4_text.get_rect(center = (window.display_width/2 + 50 + rect_width/2 , 200 + 3 * rect_height / 2 + 25)) # position lvl2_text in rect top right
screen.blit(lvl4_text, lvl4_text_position)
lvl5_text = SMALL_FONT.render("Level 5", True, color_lvl5)
lvl5_text_position = lvl5_text.get_rect(center = (50 + rect_width/2 , 200 + 5 * rect_height / 2 + 50 )) # position lvl3_text in rect bottom left
screen.blit(lvl5_text, lvl5_text_position)
highscore_text = SMALL_FONT.render("Highscores", True, 'red')
highscore_text_position = highscore_text.get_rect(center = (window.display_width/2 + 50 + rect_width/2 , 200 + 5 * rect_height / 2 + 50)) # position highscore_text in rect bottom right
screen.blit(highscore_text, highscore_text_position)
# check if clicked on a button and if button available
if click == True and 50 <= mouse_x <= 50 + rect_width and 200 <= mouse_y <= 200 + rect_height :
choosed_level = 1
elif click == True and window.display_width/2 + 50 <= mouse_x <= window.display_width/2 + 50 + rect_width and 200 <= mouse_y <= 200 + rect_height and winned_levels[1] == True :
choosed_level = 2
elif click == True and 50 <= mouse_x <= 50 + rect_width and 200 + rect_height + 25 <= mouse_y <= 200 + 2 * rect_height + 25 and winned_levels[2] == True :
choosed_level = 3
elif click == True and window.display_width/2 + 50 <= mouse_x <= window.display_width/2 + 50 + rect_width and 200 + rect_height + 25 <= mouse_y <= 200 + 2 * rect_height + 25 and winned_levels[3] == True :
choosed_level = 4
elif click == True and 50 <= mouse_x <= 50 + rect_width and 200 + 2 * rect_height + 50 <= mouse_y <= 200 + 3 * rect_height + 50 and winned_levels[4] == True :
choosed_level = 5
elif click == True and window.display_width/2 + 50 <= mouse_x <= window.display_width/2 + 50 + rect_width and 200 + 2 * rect_height + 50 <= mouse_y <= 200 + 3 * rect_height + 50 :
wait_text = LARGE_FONT.render("Not ready for now ( ;", True, 'black')
wait_text_position = wait_text.get_rect(center = (window.display_width/2, window.display_height/2)) # position highscore_text in rect bottom right
screen.blit(wait_text, wait_text_position)
if choosed_level != False:
if choosed_level == 1 :
bricks_of_level = bricks_of_level1
special_bricks = special_bricks1
create_brick1()
elif choosed_level == 2 :
bricks_of_level = bricks_of_level2
special_bricks = special_bricks2
create_bricks2()
elif choosed_level == 3 :
bricks_of_level = bricks_of_level3
special_bricks = special_bricks3
create_bricks3()
elif choosed_level == 4 :
bricks_of_level = bricks_of_level4
special_bricks = special_bricks4
create_bricks4()
elif choosed_level == 5 :
bricks_of_level = bricks_of_level5
special_bricks = special_bricks5
create_bricks5()
while not continu :
screen.fill('black')
scaled_background_lvl1 = pygame.transform.scale(background_lvl1, (window.display_width, window.display_height))
screen.blit(scaled_background_lvl1, (0,0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN: #key is pressed down
if event.key == pygame.K_LEFT:
left = True
if event.key == pygame.K_RIGHT:
right = True
if event.key == pygame.K_ESCAPE :
end_game = True
logging.info(f"user ended game")
if event.key == pygame.K_p :
pass_level = True
logging.info(f"user passed level")
if event.key == pygame.K_r :
level_reset = True
logging.warning("level reseted")
if event.key == pygame.K_SPACE :
if pause == True :
pause = False
logging.info(f"user depaused game")
elif pause == False :
pause = True
logging.info(f"user paused game")
elif event.type == pygame.KEYUP: #key is not pressed anymore
if event.key == pygame.K_LEFT:
left = False
if event.key == pygame.K_RIGHT:
right = False
if event.key == pygame.K_ESCAPE :
end_game = False
if event.key == pygame.K_p :
pass
if event.key == pygame.K_r :
level_reset = False
if event.key == pygame.K_SPACE :
if pause == True :
pause = True
elif pause == False :
pause = False
if pass_level : #if p is pressed pass the level by changing all values of bricks_of_level to 0
win = True
score_before_pass = score
for i in bricks_of_level :
bricks_of_level[i] = 0
logging.info(f"passing level is succesfully executed")
if pause == True and win != True : # if ''space'' was pressed and the variable pause is set on True what sets the speed of the balls and of the paddle on 0 until ''space'' is pressed again
for ball_number, ball_exist in ball_of_level.items() :
if ball_exist == 1 :
balls_of_level_classes[ball_number].speed = 0
logging.info(f"pausing level is succesfully executed")
paddle.speed = 0
pause_text = LARGE_FONT.render("Pause press ''space'' to continue", True, 'red')
pause_text_position = pause_text.get_rect(center = (window.display_width/2, window.display_height/2))
screen.blit(pause_text, pause_text_position)
if pause == False : #sets the speed of the balls and of the paddle to the speed they had before the pause
for ball_number, ball_exist in ball_of_level.items() :
if ball_exist == 1 :
balls_of_level_classes[ball_number].speed = ball_speed
paddle.speed = paddle_default_speed
if end_game == True : #if escape is pressed end game by quitting pygame
pygame.quit()
logging.info(f"ending game is succesfully executed")
if level_reset == True :
if choosed_level == 1 :
create_brick1()
elif choosed_level == 2 :
create_bricks2()
elif choosed_level == 3 :
create_bricks3()
elif choosed_level == 4 :
create_bricks4()
elif choosed_level == 5 :
create_bricks5()
ball_of_level = {1 : 1}
life = 3
win = False
score = begin_score
score = begin_score
#we add a probability for each destroyed block to have a new ball
if probability_new_ball== 10:
number_balls+=1
ball_of_level[number_balls] = 1
balls_of_level_classes[number_balls] = Ball(x = paddle.centerx , y = paddle.centery - paddle_height/2 - ball.radius, dx = random.uniform(-1.00, 1.00), dy = -1.2, radius = 15, speed = 5, color='red')
probability_new_ball = 0
#creats one heart for each life of the player
for i in range (life ) :
heart_width = 30
heart_height = 30
scaled_heart = pygame.transform.scale(heart, (heart_width, heart_height))
screen.blit(scaled_heart,(heart_width*i + 10 * i,0))
#we test if there is any brick left if they are all destroyed -> show win text
z=0
for i in bricks_of_level.values() :
if i != 0 :
break
else :
win = True
if not pass_level :
Win_text = LARGE_FONT.render("GG! You WON!!!!!!", True, 'red')
Win_text_position = Win_text.get_rect(center = (window.display_width/2, window.display_height/2))
screen.blit(Win_text, Win_text_position)
logging.info(f"player won and win-text is shown")
elif pass_level :
pass_text = LARGE_FONT.render("Level passed !", True, 'red')
pass_text_position = pass_text.get_rect(center = (window.display_width/2, window.display_height/2))
screen.blit(pass_text, pass_text_position)
logging.info(f"player passed level and pass-text is shown")
#TODO: Implement collision for any number of balls on screen
#
for ball_number, ball_exist in ball_of_level.items() : #checks if the ball is still existing if it exist it will check all colisions
if ball_exist == 1 :
balls_of_level_classes[ball_number].collision_paddle_or_brick(paddle) #checks and executes paddle collision; DO NOT REMOVE
balls_of_level_classes[ball_number].collision_with_wall() #checks and executes wall collision; DO NOT REMOVE
for brick_number, brick_exist in bricks_of_level.items() : #checks if the brick is still existing
if brick_exist == 1 :
power_up_probability = balls_of_level_classes[ball_number].collision_paddle_or_brick(bricks_of_level_classes[brick_number]) #checks and executes brick collision; DO NOT REMOVE
if power_up_probability==[1] :
logging.info("Power up has appeared")
# move the ball one step
#TODO: Implement draw for any number of balls on screen
#check if the ball exists if yes make the ball move
for ball_number, ball_exist in ball_of_level.items() :
if ball_exist == 1 :
balls_of_level_classes[ball_number].move() #after ball is drawn, the ball moves (maybe it is better, to move first and to draw second)
# draw the ball if it exists
for ball_number, ball_exist in ball_of_level.items() :
if ball_exist == 1 :
balls_of_level_classes[ball_number].draw(screen)
#draw the paddle
paddle.draw(screen)
# defines the color of the bricks depending of the health
for brick_number, brick_exist in bricks_of_level.items() :
if brick_exist == 1 and brick_number not in special_bricks:
old_color = bricks_of_level_classes[brick_number].color
if bricks_of_level_classes[brick_number].health == 1 :
bricks_of_level_classes[brick_number].color = 'green'
elif bricks_of_level_classes[brick_number].health == 2 :
bricks_of_level_classes[brick_number].color = 'blue'
elif bricks_of_level_classes[brick_number].health == 3 :
bricks_of_level_classes[brick_number].color = 'yellow'
elif bricks_of_level_classes[brick_number].health == 4 :
bricks_of_level_classes[brick_number].color = 'orange'
elif bricks_of_level_classes[brick_number].health == 5 :
bricks_of_level_classes[brick_number].color = 'red'
elif bricks_of_level_classes[brick_number].health == 6 :
bricks_of_level_classes[brick_number].color = 'gray50'
elif bricks_of_level_classes[brick_number].health == 7 :
bricks_of_level_classes[brick_number].color = 'black'
if old_color != bricks_of_level_classes[brick_number].color :
logging.info(f" Color of brick number {brick_number} has changed from {old_color} to {bricks_of_level_classes[brick_number].color}")
elif brick_number in special_bricks and brick_exist == 1 : # generates a random color for the brick number 306
bricks_of_level_classes[brick_number].color = list(np.random.choice(range(256), size=3))
#draw the brick only if the brick is still existing
for brick_number, brick_exist in bricks_of_level.items() :
if brick_exist == 1 :
if bricks_of_level_classes[brick_number].health>0 : # checks if the brick's health is bigger than 0 if it is the brick will be drawn if not it will be deleted
bricks_of_level_classes[brick_number].draw(screen)
else :
del (bricks_of_level_classes[brick_number])
bricks_of_level[brick_number] -= 1 # sets the value of the dictionary bricks_of_level on 0 to make clear that the block is not existing anymore
probability_new_ball = random.randint (8, 11)
print(f"the porbability to have a new ball is {probability_new_ball}")
logging.info(f"brick number {brick_number} has been destroyed")
# move the paddle
if left:
paddle.move_left()
elif right:
paddle.move_right(screen)
#calculates the score
for i in bricks_of_level :
if bricks_of_level[i] == 0 :
score += 5
for brick_number, brick_exist in bricks_of_level.items() :
if brick_exist == 1 : #change score depending on the health of the bricks and shows it on the screen
score -= bricks_of_level_classes[brick_number].health * 10
number_existing_balls = 0
win_bonus = 100
if win and not pass_level: #change score if the player has won bonus score because he wo and depending on the number of his lifes and balls -1 because he needs at least 1 life to win and one ball to win so that is not deserved
for i in ball_of_level :
if ball_of_level[i] == 1 :
number_existing_balls += 1
score += win_bonus + (life - 1) * 50 + (number_existing_balls - 1) * 20
logging.info(f"win score is succesfully calculated")
if pass_level == True :
score = score_before_pass
logging.info(f"pass score is succesfully calculated")
my_text_small = SMALL_FONT.render(f"You have {score} points !", True, 'black')
my_text_small_position = my_text_small.get_rect(center = (450, 10))
screen.blit(my_text_small, my_text_small_position)
# check if the ball has left the screen at the bottom, if yes, delete it in game and sets the value in ball_of_level on 0 to stop all actions on this ball
for ball_number, ball_exist in ball_of_level.items() :
if ball_exist==1 :
if balls_of_level_classes[ball_number].y > screen.get_height(): #note the top-left defined coordinate system :)
if not win :
logging.info(f"Ball has left the screen")
ball_of_level[ball_number] = 0
del balls_of_level_classes[ball_number]
else :
logging.info(f"you won, Ball left the screen but is drawn again")
balls_of_level_classes[ball_number].y = 250
balls_of_level_classes[ball_number].x = window.display_width/2
#checks if at least one ball is still existing if not it takes one life and if the number of lifes is bigger than 0 it creates a new life if not it shows the loose text
for ball_exist in ball_of_level.values() :
if ball_exist==1 :
break
else :
if not win :
#create a new ball at the top if no ball anymore in the screen
if life != 0 :
logging.warning(f"user lost a live")
life -= 1
if life > 0 :
balls_of_level_classes[1] = Ball(x = window.display_width//2, y = 250, dx =random.uniform(-0.50, 0.50), dy = 0.6, radius = 15, speed = 5, color='red')
ball_of_level[1] = 1
logging.info(f"user have lifes left and new ball is generated")
else :
loose = True
loose_text = LARGE_FONT.render("Sorry, you lost :(", True, 'red')
loose_text_position = loose_text.get_rect(center = (window.display_width/2, window.display_height/2))
screen.blit(loose_text, loose_text_position)
pygame.display.flip
if win == True or pass_level == True :
winned_levels[choosed_level] = True
win = False
begin_score = score
pass_level = False
elif loose :
loose = False
choosed_level = False
bricks_of_level_classes = {}
#update
# pygame.time.wait(1) #slow things down by waiting 1 millisecond
pygame.display.update() #very important
clock.tick(FPS) # very important
pygame.quit() # Call the quit() method outside the while loop to end the application.
logging.warning(f"game code ended")