This post has been de-listed
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
2
Issues with breakout game in Python
Post Body
Having problems with my breakout game in python. The ball speeds up when I change it's x_move, the ball and paddle mysteriously speed up as more tiles are broken, and my tile collision isn't working smoothly.
I've tried all sorts of things like onscreentimer but I just can't quite get it.
from tkinter import *
from turtle import Screen
import time
from paddle import Paddle
from tile import Tile
from ball import Ball
screen = Screen()
screen.setup(width=1000, height=600)
screen.bgcolor("black")
screen.tracer(0)
screen.title("Breakout Game")
paddle_position = (0, -250)
paddle = Paddle(paddle_position)
x_values = [-400, -200, 0, 200, 400]
y_values = [250, 225, 200, 175, 150, 125, 100]
color = ["red", "orange", "yellow", "green", "blue", "purple", "teal"]
brick_list = []
for x in x_values:
for y in y_values:
color_choice = color[y_values.index(y)]
tile_position = (x, y)
tile = Tile(tile_position, color_choice)
brick_list.append(tile)
ball = Ball()
left_keys = ["a", "A", "Left"]
right_keys = ["d", "D", "Right"]
screen.listen()
for key in left_keys:
screen.onkeypress(paddle.go_left_start, key)
screen.onkeyrelease(paddle.go_left_end, key)
for key in right_keys:
screen.onkeypress(paddle.go_right_start, key)
screen.onkeyrelease(paddle.go_right_end, key)
# tile_set = 0
game_is_on = True
def breakout():
game_is_on = True
global tile_set
global brick_set
ball.move()
if paddle.move_left:
if paddle.xcor() - 100 > -500:
x = paddle.xcor()
x -= 2
paddle.setx(x)
if paddle.move_right:
if paddle.xcor() 100 < 500:
x = paddle.xcor()
x = 2
paddle.setx(x)
if ball.xcor() > 480 or ball.xcor() < -480:
ball.bounce_x()
for item in brick_list:
brick_set = 1
if item.ycor() - 20 <= ball.ycor() <= item.ycor() 20 and item.xcor() - 100 <= ball.xcor() <= item.xcor() 100 and brick_set == 1:
ball.bounce_y()
item.hideturtle()
brick_list.remove(item)
tile_set = 1
brick_set = 0
# print(tile_set)
if paddle.xcor() - 110 <= ball.xcor() <= paddle.xcor() 110 and ball.ycor() == -230 and tile_set == 1:
# print(ball.xcor() - paddle.xcor())
value = ball.xcor() - paddle.xcor()
ball.bounce_paddle(value)
tile_set = 0
if ball.ycor() > 280:
tile_set = 1
ball.bounce_y()
if ball.ycor() == -300:
game_is_on = False
screen.update()
while game_is_on:
breakout()
heres my ball class
from turtle import Turtle
class Ball(Turtle):
def __init__(self):
super().__init__()
self.shape("circle")
self.color("red")
# self.fillcolor("orange")
self.penup()
self.goto(0, -200)
self.x_move = 1
self.y_move = 1
# self.move_speed = 1.1
def move(self):
new_x = self.xcor() self.x_move
new_y = self.ycor() self.y_move
self.goto(new_x, new_y)
def bounce_y(self):
self.y_move *= -1
def bounce_paddle(self, value):
movement = value * 0.01
self.y_move *= -1
self.x_move = movement
here's paddle:
from turtle import Turtle
class Paddle(Turtle):
def __init__(self, position):
super().__init__()
self.shape("square")
self.color("gray")
self.right(90)
self.shapesize(stretch_wid=10, stretch_len=1)
self.penup()
self.goto(position)
self.move_left = False
self.move_right = False
self.move_speed = 1.1
def go_left_start(self):
self.move_left = True
def go_left_end(self):
self.move_left = False
def go_right_start(self):
self.move_right = True
def go_right_end(self):
self.move_right = False
Author
Account Strength
20%
Account Age
1 year
Verified Email
No
Verified Flair
No
Total Karma
2,275
Link Karma
43
Comment Karma
2,232
Profile updated: 4 days ago
Posts updated: 11 months ago
Subreddit
Post Details
We try to extract some basic information from the post title. This is not
always successful or accurate, please use your best judgement and compare
these values to the post title and body for confirmation.
- Posted
- 1 year ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/AskProgramm...