screen freeze in breakout game - python

44 Views Asked by At
# bricks class

from turtle import Turtle
import random
colors = ["sky blue", "tomato", "lime green", "yellow"]
x_list = [385, 300, 215, 130, 45, -40, -125, -210, -295]


class Bonus:
    def __init__(self, position, bonus_type=None):
        self.bonus = Turtle()
        self.bonus.ht()
        self.bonus.shape("square")
        self.bonus.shapesize(stretch_wid=1, stretch_len=1)
        self.bonus.seth(270)
        self.bonus.color("blue")
        self.bonus.penup()
        self.bonus.goto(position)
        self.bonus.st()
        self.bonus_type = bonus_type


class Brick:
    def __init__(self,  position):
        self.tr = Turtle()
        self.squares = []
        self.brick_list = []
        self.tr.ht()
        self.tr.shape("square")
        self.tr.shapesize(stretch_wid=1, stretch_len=4)
        self.tr.color(random.choice(colors))
        self.tr.penup()
        self.tr.speed(0)
        self.tr.goto(position)
        self.tr.st()

    def levels(self, level_number):
        # level 1
        if level_number == 1:
            x, y = 350, 285
            brick_indices = list(range(36))
            random.shuffle(brick_indices)
            selected_indices = brick_indices[:8]
            for i in range(4):
                for j in range(9):
                    brick = Brick((x, y))
                    # Generate a random bonus type for this brick
                    if (i * 9 + j) in selected_indices:
                        bonus_type = random.choice(x_list)
                        bonus = Bonus((x, y), bonus_type)
                        self.squares.append(bonus)
                    self.brick_list.append(brick)
                    x -= 85
                x = 350
                y -= 25

        
#main.py

from turtle import Screen
from ball import Ball
from player import Player
from bricks import Brick, Bonus
from scoreboard import Score


game_is_on = True
screen = Screen()
screen.setup(width=800, height=600)
screen.bgcolor("black")
bricks = Brick((500, 500))
ball = Ball()
paddle = Player((0, -250))
score = Score()

screen.listen()
screen.onkey(paddle.move_paddle_right, "Right")
screen.onkey(paddle.move_paddle_left, "Left")

score.level(1)
score.life(3)
life = 3
level = 1
bricks.levels(level)


while game_is_on:
    for i in bricks.brick_list:
        if isinstance(i, Brick):
            if ball.distance(i.tr.position()) < 50:
                ball.bounce_down()
                i.tr.goto(500, 500)
                bricks.brick_list.remove(i)
                if len(bricks.brick_list) == 0:
                    level += 1
                    ball.remove_ball()
                    paddle.paddle_reset()
                    bricks.levels(level)
                    score.level(level)
                    ball.zero_ball()
                    print(level)
    for i in bricks.squares:
        if isinstance(i, Bonus):
            if ball.distance(i.bonus.position()) < 50:
                ball.ball_move()
                ball.bounce_down()
                i.bonus.goto(500, 500)
                # i.bonus.fd(500)
    ball.ball_move()
    # ball collision with walls up, right and left
    if ball.ycor() > 270:
        ball.bounce_down()
    elif ball.xcor() > 350:
        ball.bounce_up()
    elif ball.xcor() < -370:
        ball.bounce_up()
    if ball.ycor() < -270:
        ball.zero_ball()
        life -= 1
        score.clear()
        score.level(level)
        score.life(life)
    # ball collision with paddle
    if ball.distance(paddle) < 50:
        ball.bounce_down()


screen.mainloop()

Hi,i shorted the code: I'm newby in python and i want to build breakout game. i have issue that every time when the ball hit the bonus object, the screen freeze for a second, i do not know how to solve it, i tried: screen.update() , time.sleep() and ontimer but nothing helps

i tried: screen.update() , time.sleep() and ontimer but nothing helps.

0

There are 0 best solutions below