Detect collision of two Turtles using 'if' statements - python

I am trying to create a game where a turtle is confined in a box. A ball bounces around the box, and if it collides with the turtle, or if the turtle touches the perimeter of the box, the game is over. A red dot spawns in a random location in the box, and when the turtle runs over that dot, the dot is supposed to disappear and respawn somewhere else. However, this only works sometimes. Sometimes the dot will randomly disappear and spawn somewhere else even though the turtle isn't even close to it. How can I fix this?
from turtle import Screen, Turtle, mainloop
wn = Screen()
wn.bgcolor("light blue")
#CONTROLLABLE TURTLE
t = Turtle()
t.color('black')
t.pensize(10)
t.shape("turtle")
t.speed("fastest")
t.penup()
t.goto(-130,-200)
t.pendown()
global hit
global score
hit = False
score = 0
#MAKE SQUARE
for i in range(4):
t.forward(400)
t.left(90)
t.penup()
t.goto(50,0)
speed = 2
t.color("black")
#BALL
ball1 = turtle.Turtle()
ball1.color("blue")
ball1.speed(0)
ball1.penup()
ball1.shape("circle")
A = random.randint(30,60)
B = random.randint(120,150)
C = random.randint(210,240)
D = random.randint(300,330)
Directions = [A, B, C, D]
direct = random.choice(Directions)
def tDirection(direct):
ball1.right(direct)
tDirection(direct)
angle = 90
#DOT TURTLE
dot = turtle.Turtle()
dot.color("black")
dot.speed(0)
dot.hideturtle()
def createDot():
dotx = random.randint(-10,230)
doty = random.randint(-160,200)
dot.penup()
dot.goto(dotx,doty)
dot.pendown()
dot.pensize(3)
dot.fillcolor("Red")
dot.begin_fill()
dot.circle(7)
dot.end_fill()
createDot()
#make score function
while True:
if hit == False:
#moving ball
ty = ball1.ycor()
tx = ball1.xcor()
if ty < -183:
angleCurr = ball1.heading()
if(270>angleCurr>180):
ball1.right(angle)
else:
ball1.left(angle)
ball1.forward(2)
elif ty > 185:
angleCurr = ball1.heading()
if(0<angleCurr<90):
ball1.right(angle)
else:
ball1.left(angle)
ball1.forward(2)
elif tx < -115:
angleCurr = ball1.heading()
if(180<angleCurr<270):
ball1.left(angle)
else:
ball1.right(angle)
ball1.forward(2)
elif tx > 251:
angleCurr = ball1.heading()
if(0<angleCurr<90):
ball1.left(angle)
else:
ball1.right(angle)
ball1.forward(9)
wn.onkey(lambda: t.setheading(180), 'Left')
wn.onkey(lambda: t.setheading(0), 'Right')
wn.onkey(lambda: t.setheading(90), 'Up')
wn.onkey(lambda: t.setheading(270), 'Down')
w = turtle.Turtle()
w.hideturtle()
w.penup()
w.speed("fastest")
def end():
w.goto(-95,-20)
w.pendown()
w.write("GAME OVER", font=("Arial", 40, "normal"))
t.hideturtle()
ball1.hideturtle()
dot.hideturtle()
dot.clear()
speed = 2
def turtleMove():
t.forward(speed)
wn.ontimer(turtleMove, 10)
dodx = dot.xcor()
dody = dot.ycor()
if abs(t.xcor() - dodx) < 5 and abs(t.ycor() == dody) < 5:
hit = True
dot.clear()
elif abs(t.xcor() - tx) < 5 and abs(t.ycor() - ty) < 5:
end()
if t.xcor() > 253 or t.xcor() < -115 or t.ycor() > 185 or t.ycor() < -183:
end()
turtleMove()
wn.mainloop()
wn.listen()
if hit == True:
createDot()
score+=1
print(score)
hit = False

I think it might just be a typo:
if abs(t.xcor() - dodx) < 5 and abs(t.ycor() == dody) < 5:
Your collision detection is doing a comparison with the y coordinates instead of subtracting like you did with the x coordinates.
So this should fix it:
if abs(t.xcor() - dodx) < 5 and abs(t.ycor() - dody) < 5:

I would avoid code like this:
dodx = dot.xcor()
dody = dot.ycor()
if abs(t.xcor() - dodx) < 5 and abs(t.ycor() == dody) < 5:
hit = True
dot.clear()
elif abs(t.xcor() - tx) < 5 and abs(t.ycor() - ty) < 5:
end()
And instead use turtle's distance() method:
if t.distance(dot) < 5:
hit = True
dot.clear()
elif t.distance(ball1) < 5:
end()
However, this only works sometimes. Sometimes the dot will randomly
disappear and spawn somewhere else even though the turtle isn't even
close to it.
Are you pretending that this code runs? It doesn't even start up due to two different import issues. Fixing those, the turtle controls don't work at all. And the ball doesn't bounce around the box!
This appears to be a random collection of bits of code borrowed from other programs patched together with wishful thinking. It simply doesn't work.
Below I've taken apart your code and put it back together again such that it basically runs. The blue ball does bounce around the box; you can move the turtle with the arrow keys; the red dot does disappear and reappear elsewhere when the turtle touches it:
from turtle import Screen, Turtle
from random import randint, choice
CURSOR_SIZE = 20
def tDirection(direct):
ball.right(direct)
def turtleMove():
turtle.forward(speed)
screen.ontimer(turtleMove, 10)
def createDot():
x, y = randint(CURSOR_SIZE - 200, 200 - CURSOR_SIZE), randint(CURSOR_SIZE - 200, 200 - CURSOR_SIZE)
dot.goto(x, y)
def end():
marker.write("GAME OVER", align='center', font=("Arial", 40, "normal"))
turtle.hideturtle()
ball.hideturtle()
dot.hideturtle()
screen = Screen()
screen.bgcolor("light blue")
score = 0
speed = 2
# CONTROLLABLE TURTLE
turtle = Turtle("turtle")
turtle.color('black')
turtle.pensize(10)
turtle.speed("fastest")
turtle.penup()
turtle.goto(-200, -200)
turtle.pendown()
# MAKE SQUARE
for i in range(4):
turtle.forward(400)
turtle.left(90)
turtle.penup()
turtle.goto(50, 0)
# BALL
ball = Turtle("circle")
ball.color("blue")
ball.speed('fastest')
ball.penup()
A = randint(30, 60)
B = randint(120, 150)
C = randint(210, 240)
D = randint(300, 330)
directions = [A, B, C, D]
direct = choice(directions)
tDirection(direct)
angle = 90
# DOT TURTLE
dot = Turtle('circle')
dot.color("red")
dot.speed('fastest')
dot.pensize(3)
dot.penup()
createDot()
marker = Turtle(visible=False)
marker.penup()
marker.sety(-20)
screen.onkey(lambda: turtle.setheading(0), 'Right')
screen.onkey(lambda: turtle.setheading(90), 'Up')
screen.onkey(lambda: turtle.setheading(180), 'Left')
screen.onkey(lambda: turtle.setheading(270), 'Down')
screen.listen()
turtleMove()
while True:
# moving ball
tx, ty = ball.position()
if ty < CURSOR_SIZE - 200:
angleCurr = ball.heading()
if 270 > angleCurr > 180:
ball.right(angle)
else:
ball.left(angle)
ball.forward(2)
elif ty > 200 - CURSOR_SIZE:
angleCurr = ball.heading()
if 0 < angleCurr < 90:
ball.right(angle)
else:
ball.left(angle)
ball.forward(2)
elif tx < CURSOR_SIZE - 200:
angleCurr = ball.heading()
if 180 < angleCurr < 270:
ball.left(angle)
else:
ball.right(angle)
ball.forward(2)
elif tx > 200 - CURSOR_SIZE:
angleCurr = ball.heading()
if 0 < angleCurr < 90:
ball.left(angle)
else:
ball.right(angle)
ball.forward(2)
ball.forward(9)
if turtle.distance(dot) < CURSOR_SIZE/2:
score += 1
dot.hideturtle()
createDot()
dot.showturtle()
elif turtle.distance(ball) < CURSOR_SIZE/2:
end()
if not CURSOR_SIZE - 200 < turtle.xcor() < 200 - CURSOR_SIZE or not CURSOR_SIZE - 200 < turtle.ycor() < 200 - CURSOR_SIZE:
end()
screen.mainloop()
When asking for help on SO, please be honest about the state of your code.
The above code isn't complete, there is still work to do. E.g. the while True: loop should be converted to a function and ontimer() event; the score needs to be displayed; the game should be made restartable.

Related

How to get the scoreboard to work in Turtle Graphics?

**I just need to update the score constantly when the ball crashes into the platform.
What also I do not know is how to clone the ball to make multiple balls in the arena
If anyone can give some input that would be great also
Here is the code I have:**
import turtle
import random
from random import randint
import time
from turtle import Turtle
HEIGHT, WIDTH = 500, 500
screen = turtle.Screen()
screen.screensize(HEIGHT, WIDTH)
COLORS = 'white', 'green' ,'cyan', 'orange', 'skyblue'
screen.bgcolor(random.choice(COLORS))
screen.title("Bounce a ball")
CURSOR_SIZE = 20
def tDirection(direct):
t.setheading(direct)
# make arena
def rectangle():
t.pendown()
for i in range(2):
t.forward(600)
t.left(90)
t.forward(600)
t.left(90)
t.penup()
#defines new turtle
pen = turtle.Turtle()
#right and left keys
def move_left():
pen.penup()
pen.setheading(0)
pen.bk(100)
def move_right():
pen.penup()
pen.setheading(0)
pen.fd(100)
#plaform###########################
pen.penup()
pen.goto(0, -250)
pen.shape("square")
pen.color("black")
pen.shapesize(1, 5)
screen.listen()
screen.onkey(move_right, "Right")
screen.onkey(move_left, "Left")
#####################################
n = turtle.Turtle()
score = 0
n.penup()
n.goto(-50,250)
n.write("Your score:", font=20)
n.hideturtle()
#circle######################################
t = Turtle("circle", visible=False)
t.speed('fastest')
t.pensize(5)
t.penup()
t.goto(-300, -300)
###########################################
rectangle()
index = 0
#####################################
t.color('black')
t.home()
t.showturtle()
#####################################
direct = randint(1, 600)
tDirection(direct)
while True:
t.forward(2)
ty = t.ycor()
def is_collided_with(a, b):
return abs(a.xcor() - b.xcor()) < 10 and abs(a.ycor() - b.ycor()) < 10
# breaking out top or bottom
if is_collided_with(t, pen):
score += 1
print("Coll")
continue
if not CURSOR_SIZE - 300 <= ty <= 300 - CURSOR_SIZE:
index += 1
t.color('pink')
angleCurr = t.heading()
if 0 < angleCurr < 180:
tDirection(0 - angleCurr)
else:
tDirection(360 - angleCurr)
t.forward(2)
n.getscreen().update()
tx = t.xcor()
# breaking out left or right
if not CURSOR_SIZE - 300 <= tx <= 300 - CURSOR_SIZE:
index += 1
t.color('blue')
angleCurr = t.heading()
if 0 < angleCurr < 180:
tDirection(180 - angleCurr)
else:
tDirection(540 - angleCurr)
t.forward(2)
Most of game is working I just do not know how to write a scoreboard and how to make it run smoothly.

How to get the circle to bounce off the platform and end the game once it hits the red bar at the bottom python?

Any input would be grateful
I need help understanding why it won't break the loop and what I can do to make it bounce off the platform
It could be because of turtle graphics
I figured out most of program to run
import turtle
import random
from random import randint
import time
from turtle import Turtle
HEIGHT, WIDTH = 500, 500
screen = turtle.Screen()
screen.screensize(HEIGHT, WIDTH)
COLORS = 'white', 'green' ,'cyan', 'orange', 'skyblue'
screen.bgcolor(random.choice(COLORS))
screen.title("Bounce a ball")
CURSOR_SIZE = 20
#new direction
def tDirection(direct):
t.setheading(direct)
# make arena
def rectangle():
t.pendown()
for i in range(2):
t.forward(600)
t.left(90)
t.forward(600)
t.left(90)
t.penup()
#defines new turtle
pen = turtle.Turtle()
#right and left keys
def move_left():
pen.penup()
pen.setheading(0)
pen.bk(100)
def move_right():
pen.penup()
pen.setheading(0)
pen.fd(100)
#plaform###########################
pen.penup()
pen.goto(0, -250)
pen.shape("square")
pen.color("black")
pen.shapesize(1, 5)
screen.listen()
screen.onkey(move_right, "Right")
screen.onkey(move_left, "Left")
#score = 0
#####################################
#circle######################################
t = Turtle("circle", visible=False)
t.speed('fastest')
t.pensize(5)
t.penup()
t.goto(-300, -300)
###########################################
n = turtle.Turtle()
n.penup()
n.goto(0, -290)
n.shape('square')
n.color('red')
n.shapesize(1,30)
score = 0
rectangle()
score_turtle = turtle.Turtle()
score_turtle.penup()
score_turtle.goto(-50, 250)
score_turtle.write("Your score: {}".format(score), font=20)
score_turtle.hideturtle()
index = 0
#####################################
t.color('black')
t.home()
t.showturtle()
#####################################
direct = randint(1, 600)
tDirection(direct)
while True:
t.forward(2)
ty = t.ycor()
def is_collided_with(a, b):
return abs(a.xcor() - b.xcor()) < 10 and abs(a.ycor() - b.ycor()) < 10
if is_collided_with(t, n):
print("Oh no the game is over")
print(f"Your Final Score:", {score})
break
# breaking out top or bottom
if not CURSOR_SIZE - 300 <= ty <= 300 - CURSOR_SIZE:
index += 1
t.color('pink')
angleCurr = t.heading()
if 0 < angleCurr < 180:
tDirection(0 - angleCurr)
else:
tDirection(360 - angleCurr)
t.forward(2)
tx = t.xcor()
# breaking out left or right
if not CURSOR_SIZE - 300 <= tx <= 300 - CURSOR_SIZE:
index += 1
t.color('blue')
angleCurr = t.heading()
if 0 < angleCurr < 180:
tDirection(180 - angleCurr)
else:
tDirection(540 - angleCurr)
t.forward(2)
if is_collided_with(t, pen):
print("Collision!")
score += 1
score_turtle.clear()
score_turtle.write("Your score: {}".format(score), font=20)
continue
I am also wondering if I should add multiple balls too to make it more interesting but for now I just want to one

Making Turtle Graphics Python game start only after main menu button has been clicked

so i'm currently making a python turtle graphics game, it's a pong game where there is a 2 paddles and they pass the ball to each other, once ball touches each side the player gets a point, the game is fully functional in 1920x1080 res, the thing is i made a main menu type of thing where the game starts after the Start button has been clicked, thing is the menu works exactly as intended but even before i press start the main game loop starts once canvas is opened, not when start button is clicked, so it looks like this: https://imgur.com/tRWc6q9, this is what it looks like after menu button has been pressed: https://imgur.com/FwW9dO2
Here is the whole code, sorry if it's quite long i have been trying to solve it for 2 days
import turtle
import time
pong = turtle.Screen()
pong.title("My First Project")
pong.bgcolor("black")
pong.setup(width=1920,height=1080)
pong.tracer(0)
#Step 1: Create Both Padles and Ball(Done)
#Step 2: Movement Functions and Linking them to Keyboard(Done)
#Step 3: Scoring System and Updating it(Done)
#Step 4: Winning Screen(Done)
#Step 5: Collision with Paddles and Balls(Done)
#Step 5: Main Menu(In Progress)
#Creating Menu
mainCanvas = turtle.Turtle()
mainCanvas.color("cyan")
mainCanvas.hideturtle
mainCanvas.speed(0)
mainCanvas.begin_fill()
#list to make the main menu bg
fd_list1=[(90, 540), (0, 960), (270, 1080),(180,1920),(90,1080),(0,960)]
for hd1, fwd1 in fd_list1:
mainCanvas.setheading(hd1)
mainCanvas.fd(fwd1)
mainCanvas.end_fill()
#Creating Title
mainTitle = turtle.Turtle()
mainTitle.penup()
mainTitle.speed(0)
mainTitle.hideturtle()
mainTitle.goto(0,270)
mainTitle.write("Main Menu",align="center",font=("a Absolute Empire",60,"normal"))
#Create Start Option
startGame = turtle.Turtle()
startGame.speed(0)
startGame.penup()
startGame.goto(0,100)
pong.register_shape("start_400x150.gif") #PNG for start button
startGame.shape("start_400x150.gif") #Applying the PNG
startGame.shapesize(stretch_len=20,stretch_wid=20)
#Start Click Function
def clickStart(x,y):
if x > startGame.xcor() - 200 and x < startGame.xcor() + 200 and y > startGame.ycor() - 75 and y < startGame.ycor() + 75:
time.sleep(0.5)
mainTitle.clear()
pong.bgcolor("black")
startGame.hideturtle()
#Get rid of Main Menu
mainCanvas.color("black")
mainCanvas.goto(0,0)
mainCanvas.begin_fill()
#List to change canvas to desired color once clicked
fd_list=[(90, 540), (0, 960), (270, 1080),(180,1920),(90,1080),(0,960)]
for hd, fwd in fd_list:
mainCanvas.setheading(hd)
mainCanvas.fd(fwd)
mainCanvas.end_fill()
#Hide Button
startGame.goto(0,5000)
#Key Binding Main Menu
pong.listen()
pong.onscreenclick(clickStart,1)
#creating Paddle A, Player 1(left Side)
paddleA = turtle.Turtle()
paddleA.speed(0)
paddleA.shape("square")
paddleA.shapesize(9,1)
paddleA.color("white")
paddleA.penup()
paddleA.goto(-900,0)
#creating Paddle B, Player 2(Right Side)
paddleB = turtle.Turtle()
paddleB.speed(0)
paddleB.shape("square")
paddleB.shapesize(9,1)
paddleB.color("white")
paddleB.penup()
paddleB.goto(900,0)
#creating Ball makes it move in diagonal direction in fixed speed with ball.dx = 3 and ball.dy = 3
#Speed depends on the power of your PC or Mac, you can change dx and dy according to ur computer speed(mine sucks so 3 is pretty fast for me)
ball = turtle.Turtle()
ball.speed(0)
ball.shape("square")
ball.color("white")
ball.shapesize(1.8,1.8)
ball.penup()
ball.goto(0,0)
ball.x = 5
ball.y = 5
#Middle Line
#Creates multiple lines, enumerates them then checks for each enumerate with idx
#t variable is number of turtles, a line that goes down the middle
turtles = [turtle.Turtle() for _ in range(100)]
for idx, t in enumerate(turtles):
t.speed(0)
t.shape("square")
t.shapesize(2,0.1)
t.color("white")
t.penup()
t.goto(0,540-10*idx)
#Movings Functions
#Paddle A Up with (w) key for player A(1)
def paddleAUp():
y = paddleA.ycor()
y += 20
paddleA.sety(y)
#Paddle A Down with (s) Key for player A(1)
def paddleADown():
y = paddleA.ycor()
y -= 20
paddleA.sety(y)
#Paddle B Up with (Up_Arrow) Key for player B(2)
def paddleBUp():
y = paddleB.ycor()
y += 20
paddleB.sety(y)
#Paddle B Down (Down_Arrow) key for player B(2)
def paddleBDown():
y = paddleB.ycor()
y -= 20
paddleB.sety(y)
#Binding the Moving functions to Keyboard
#Pong.listen, listens to keyboard input
pong.listen()
#Moves Paddle A(1) up and down the y axis with (w) and (s) key respectively
pong.onkeypress(paddleAUp,"w")
pong.onkeypress(paddleADown,"s")
#Moves Paddle B(2) up and down the y axis with (Up_Arrow) and (Down_Arrow) key respectively
pong.onkeypress(paddleBUp,"Up")
pong.onkeypress(paddleBDown,"Down")
#Scores
AScore = 0
BScore = 0
#Scoring System for Player A(1)
ScoreSystemA = turtle.Turtle()
ScoreSystemA.pencolor("White")
ScoreSystemA.penup()
ScoreSystemA.setposition(-950,480)
ScoreSystemA.pendown()
ScoreSystemA.write(f"P1 Score: {AScore}",font=("courier",30,"normal"))
ScoreSystemA.hideturtle()
#Scoring System for Player B(2)
ScoreSystemB = turtle.Turtle()
ScoreSystemB.pencolor("White")
ScoreSystemB.penup()
ScoreSystemB.setposition(680,480)
ScoreSystemB.pendown
ScoreSystemB.write(f"P2 Score: {BScore}", font=("courier",30,"normal"))
ScoreSystemB.hideturtle()
#Main Loop To check canvas and update it each millisecond
while True:
pong.update()
#Makes Ball Move
ball.setx(ball.xcor() + ball.x)
ball.sety(ball.ycor() + ball.y)
#Ball Bouncing when it hits edge, Edge Detection at +y
if ball.ycor() > 510:
ball.sety(510)
ball.y = ball.y * -1
#Ball Bouncing when it hits edge, Edge Detection at -y
if ball.ycor() < -510:
ball.sety(-510)
ball.y = ball.y * -1
#Counting scoring system, when Ball goes Behind Paddle the Ball Resets and Goes in the Other Direction
#Which will then update the Score Text at the upper Left Corner
if ball.xcor() > 950:
ball.goto(0,0)
ball.x *= -1
AScore = AScore + 1
ScoreSystemA.clear()
ScoreSystemA.write(f"P1 Score: {AScore}",font=("courier",30,"normal"))
#Same As Above but for Paddle B
if ball.xcor() < -950:
ball.goto(0,0)
ball.x *= -1
BScore = BScore + 1
ScoreSystemB.clear()
ScoreSystemB.write(f"P2 Score: {BScore}", font=("courier",30,"normal"))
#Winning Condition writes Player 1(A) Won and stops the Game
if AScore == 3:
AWin = turtle.Turtle()
AWin.pencolor("White")
AWin.hideturtle()
AWin.penup()
AWin.setposition(0,0)
AWin.pendown()
AWin.write("P1 Wins!",align="center",font=("courier",60,"normal"))
turtles.clear()
turtle.done()
#Same As Above but for Paddle B
if BScore == 3:
BWin = turtle.Turtle()
BWin.pencolor("White")
BWin.hideturtle()
BWin.penup()
BWin.setposition(0,0)
BWin.pendown()
BWin.write("P2 Wins!",align="center",font=("courier",60,"normal"))
turtles.clear()
turtle.done()
#Colission System first 2 conditions are for ball x coordinate to check if it's at the same x value as the paddle
#Third if ball is less than paddle y cor for top half of the paddle
#Fourth if ball is greater than paddle y cor for bottom half of the paddle
if ball.xcor() < -900 and ball.xcor() > -950 and (ball.ycor() < paddleA.ycor() + 72 and ball.ycor() > paddleA.ycor() - 72):
ball.x = ball.x * -1
#Same As Above but for Paddle B
if ball.xcor() > 900 and ball.xcor() < 950 and (ball.ycor() < paddleB.ycor() + 72 and ball.ycor() > paddleB.ycor() - 72):
ball.x = ball.x * -1
here is the bit i'm struggling with:
#Start Click Function
def clickStart(x,y):
if x > startGame.xcor() - 200 and x < startGame.xcor() + 200 and y > startGame.ycor() - 75 and y < startGame.ycor() + 75:
time.sleep(0.5)
mainTitle.clear()
pong.bgcolor("black")
startGame.hideturtle()
#Get rid of Main Menu
mainCanvas.color("black")
mainCanvas.goto(0,0)
mainCanvas.begin_fill()
#List to change canvas to desired color once clicked
fd_list=[(90, 540), (0, 960), (270, 1080),(180,1920),(90,1080),(0,960)]
for hd, fwd in fd_list:
mainCanvas.setheading(hd)
mainCanvas.fd(fwd)
mainCanvas.end_fill()
#Hide Button
startGame.goto(0,5000)
#Key Binding Main Menu
pong.listen()
pong.onscreenclick(clickStart,1)
any help is appreciated
i tried: making a condition where if click happens then the game loop starts, when i did that and i clicked the canvas just turned all black, i think it returned none
i tried: making a second game loop, that stops when clicking the start button, that just broke
What i expected: i run code, game opens and does nothing, till i press the start button then the game starts and it plays.
You've made this difficult to implement by putting too much functionality at the top level of the code instead of encapsulated in functions. I've reworked your code to shoehorn in this functionality below. Your "start" button is just a giant circle as I don't have your artwork:
from turtle import Screen, Turtle
SMALL_FONT = ("Courier", 30, "normal")
BIG_FONT = ("Courier", 60, "normal")
MENU_FONT = ("a Absolute Empire", 60, "normal")
WINDOW_WIDTH, WINDOW_HEIGHT = 1920, 1080
fd_list = [(90, WINDOW_HEIGHT/2), (0, WINDOW_WIDTH/2), (270, WINDOW_HEIGHT), (180, WINDOW_WIDTH), (90, WINDOW_HEIGHT), (0, WINDOW_WIDTH/2)]
def clickStart(x, y):
if startGame.xcor() - 200 < x < startGame.xcor() + 200 and startGame.ycor() - 75 < y < startGame.ycor() + 75:
screen.onscreenclick(None)
mainTitle.clear()
screen.bgcolor("black")
startGame.hideturtle()
# Get rid of Main Menu
mainCanvas.clear()
startGame.hideturtle()
# Moves Paddle A(1) up and down the y axis with (w) and (s) key respectively
screen.onkeypress(paddleAUp, "w")
screen.onkeypress(paddleADown, "s")
paddleA.showturtle()
# Moves Paddle B(2) up and down the y axis with (Up_Arrow) and (Down_Arrow) key respectively
screen.onkeypress(paddleBUp, "Up")
screen.onkeypress(paddleBDown, "Down")
paddleB.showturtle()
screen.listen()
play()
# Movings Functions
# Paddle A Up with (w) key for player A(1)
def paddleAUp():
paddleA.forward(20)
# Paddle A Down with (s) Key for player A(1)
def paddleADown():
paddleA.backward(20)
# Paddle B Up with (Up_Arrow) Key for player B(2)
def paddleBUp():
paddleB.forward(20)
# Paddle B Down (Down_Arrow) key for player B(2)
def paddleBDown():
paddleB.backward(20)
# Scores
AScore = 0
BScore = 0
# Main Loop To check canvas and update it each millisecond
def play():
global AScore, BScore
# Make Ball Move
ball.setx(ball.xcor() + ball.x)
ball.sety(ball.ycor() + ball.y)
# Ball Bouncing when it hits edge
if ball.ycor() > 510:
ball.sety(510)
ball.y *= -1
elif ball.ycor() < -510:
ball.sety(-510)
ball.y *= -1
# Counting scoring system, when Ball goes Behind Paddle the Ball Resets and Goes in the Other Direction
# Which will then update the Score Text at the upper Left Corner
if ball.xcor() > 950:
ball.goto(0, 0)
ball.x *= -1
AScore += 1
ScoreSystemA.clear()
ScoreSystemA.write(f"P1 Score: {AScore}", align='center', font=SMALL_FONT)
elif ball.xcor() < -950:
ball.goto(0, 0)
ball.x *= -1
BScore += 1
ScoreSystemB.clear()
ScoreSystemB.write(f"P2 Score: {BScore}", align='center', font=SMALL_FONT)
# Winning Condition writes Player 1(A) Won and stops the Game
if AScore == 3:
midLine.clear()
ball.hideturtle()
AWin = Turtle()
AWin.hideturtle()
AWin.pencolor("White")
AWin.write("P1 Wins!", align="center", font=BIG_FONT)
screen.update()
return
if BScore == 3:
midLine.clear()
ball.hideturtle()
BWin = Turtle()
BWin.hideturtle()
BWin.pencolor("White")
BWin.write("P2 Wins!", align="center", font=BIG_FONT)
screen.update()
return
# Colission System first 2 conditions are for ball x coordinate to check if it's at the same x value as the paddle
# Third if ball is less than paddle y cor for top half of the paddle
# Fourth if ball is greater than paddle y cor for bottom half of the paddle
if -950 < ball.xcor() < -900 and paddleA.ycor() - 72 < ball.ycor() < paddleA.ycor() + 72:
ball.x *= -1
elif 900 < ball.xcor() < 950 and paddleB.ycor() - 72 < ball.ycor() < paddleB.ycor() + 72:
ball.x = ball.x * -1
screen.update()
screen.ontimer(play)
screen = Screen()
screen.title("My First Project")
screen.bgcolor("black")
screen.setup(WINDOW_WIDTH, WINDOW_HEIGHT)
# screen.register_shape("start_400x150.gif") # PNG for start button
screen.tracer(0)
# Create Paddle A, Player 1 (left Side)
paddleA = Turtle()
paddleA.hideturtle()
paddleA.shape("square")
paddleA.shapesize(1, 9)
paddleA.color("white")
paddleA.penup()
paddleA.setheading(90)
paddleA.setx(-900)
# Create Paddle B, Player 2 (Right Side)
paddleB = paddleA.clone()
paddleB.setx(900)
# Create Ball making it move in diagonal direction in fixed speed
ball = Turtle()
ball.shape("square")
ball.color("white")
ball.shapesize(1.8)
ball.penup()
ball.x = 5 # user properties
ball.y = 5
# Middle Line
midLine = Turtle()
midLine.hideturtle()
midLine.color("white")
midLine.pensize(2)
midLine.penup()
midLine.sety(-WINDOW_HEIGHT/2)
midLine.pendown()
midLine.sety(WINDOW_HEIGHT/2)
# Scoring System for Player A(1)
ScoreSystemA = Turtle()
ScoreSystemA.hideturtle()
ScoreSystemA.color("White")
ScoreSystemA.penup()
ScoreSystemA.setposition(-480, 420)
ScoreSystemA.write(f"P1 Score: {AScore}", align='center', font=SMALL_FONT)
# Scoring System for Player B(2)
ScoreSystemB = Turtle()
ScoreSystemB.hideturtle()
ScoreSystemB.color("White")
ScoreSystemB.penup()
ScoreSystemB.setposition(480, 420)
ScoreSystemB.write(f"P2 Score: {BScore}", align='center', font=SMALL_FONT)
# Create Menu
mainCanvas = Turtle()
mainCanvas.hideturtle()
mainCanvas.color("cyan")
mainCanvas.begin_fill()
for heading, distance in fd_list:
mainCanvas.setheading(heading)
mainCanvas.forward(distance)
mainCanvas.end_fill()
# Create Title
mainTitle = Turtle()
mainTitle.hideturtle()
mainTitle.penup()
mainTitle.sety(270)
mainTitle.write("Main Menu", align="center", font=MENU_FONT)
# Create Start Option
startGame = Turtle()
startGame.penup()
startGame.sety(100)
# startGame.shape("start_400x150.gif")
startGame.shape('circle')
startGame.shapesize(20)
# Key Binding Main Menu
screen.onscreenclick(clickStart)
screen.update()
screen.mainloop()
As you can see, I've also made a number of other changes to streamline your code and make it behave properly in an event-driven environment.

One object gets stuck with two while loops

I'm trying to add physics to the paddle object whenever I press "w", but the ball object gets stuck until the while loop is finished in the paddle_up() function. How do I make the ball keep moving while the paddle is jumping?
The point of this game is avoiding the ball coming from the right. Whenever the user avoids the ball he or she gets one point, but whenever it collide's with the paddle, they lose one point:
import random
import turtle
# window setup
wn = turtle.Screen()
wn.title("Jump Game")
wn.bgcolor("black")
wn.setup(width=1000, height=750)
wn.tracer(0)
# Key Biding Functions
def paddle_up():
paddle_a.dy = -0.1
while paddle_a.ycor() < 0:
paddle_a.sety(paddle_a.ycor() - paddle_a.dy)
wn.update()
def paddle_down():
y = paddle_a.ycor()
y -= 10
paddle_a.sety(y)
def paddle_left():
x = paddle_a.xcor()
x -= 10
paddle_a.setx(x)
def paddle_right():
x = paddle_a.xcor()
x += 10
paddle_a.setx(x)
# Paddle A
paddle_a = turtle.Turtle()
paddle_a.speed(1)
paddle_a.shape("square")
paddle_a.color("white")
paddle_a.shapesize(stretch_wid=2, stretch_len=2, outline=1)
paddle_a.penup()
paddle_a.goto(-480, -350)
# Ball
ball = turtle.Turtle()
ball.shape("circle")
ball.color("white")
ball.penup()
ball.goto(-350, 0)
ball_speed = random.random()
ball_position_y = random.randint(-350, -300)
# Score System
Score = 0
Scr = turtle.Turtle()
Scr.goto(0, 360)
Scr.color("white")
Scr.penup()
Scr.hideturtle()
wn.listen()
wn.onkeypress(paddle_up, "w")
wn.onkeypress(paddle_down, "s")
wn.onkeypress(paddle_left, "a")
wn.onkeypress(paddle_right, "d")
# Main Game Loop
while True:
wn.update()
ball.setx(ball.xcor() - ball_speed * 0.3)
if ball_speed <= 0.3:
ball_speed = 0.3
if ball.xcor() < paddle_a.xcor() + 20 and ball.xcor() > paddle_a.xcor() - 20 and ball.ycor() < paddle_a.ycor() +20 and ball.ycor() > paddle_a.ycor() - 20:
Score = Score - 1
Scr.clear()
Scr.write("Score{}".format(Score), align="center", font=("Courier", 24))
ball.setx(490)
ball_position_y = random.randint(-350, -300)
ball.sety(ball_position_y)
elif ball.xcor() <= -490 and ball.ycor() != paddle_a.ycor():
Score = Score + 1
Scr.clear()
Scr.write("Score{}".format(Score), align="center", font=("Courier", 24))
ball.setx(490)
ball_position_y = random.randint(-350, -300)
ball.sety(ball_position_y)
You're using while loops where you should be using timer events. SO is full of Python turtle game answers that say, "while True: has no business in an event-driven world like turtle." How could you miss them?
I've reworked your code below to make it basically playable:
from random import randint
from turtle import Screen, Turtle
FONT = ('Courier', 24)
# Key Biding Functions
def paddle_up():
def paddle_rise():
if paddle.ycor() < 0:
paddle.sety(paddle.ycor() + paddle.dy)
screen.update()
screen.ontimer(paddle_rise, 10)
paddle_rise()
def paddle_down():
paddle.sety(paddle.ycor() - 10)
def paddle_left():
paddle.setx(paddle.xcor() - 10)
def paddle_right():
paddle.setx(paddle.xcor() + 10)
def update_score():
global ball_speed
scorer.clear()
scorer.write("Score: {}".format(score), align='center', font=FONT)
ball.setposition(490, randint(-350, -300))
ball_speed = randint(1, 3)
# Main Game Loop
def play():
global score
ball.setx(ball.xcor() - ball_speed)
if ball.distance(paddle) <= 20:
score -= 1
update_score()
elif ball.xcor() <= -490 and ball.ycor() != paddle.ycor():
score += 1
update_score()
screen.update()
screen.ontimer(play, 10)
# window setup
screen = Screen()
screen.title("Jump Game")
screen.bgcolor('black')
screen.setup(width=1000, height=800)
screen.tracer(0)
# Paddle A
paddle = Turtle()
paddle.shape('square')
paddle.color('white')
paddle.shapesize(stretch_wid=2, stretch_len=2, outline=1)
paddle.speed('slowest')
paddle.penup()
paddle.goto(-480, -350)
paddle.dy = 2
# Ball
ball = Turtle()
ball.shape('circle')
ball.color('white')
ball.penup()
ball.setposition(490, randint(-350, -300))
ball_speed = randint(1, 3)
# Score System
score = 0
scorer = Turtle()
scorer.hideturtle()
scorer.sety(360)
scorer.color('white')
scorer.penup()
scorer.write("Score: {}".format(score), align='center', font=FONT)
screen.onkeypress(paddle_up, 'w')
screen.onkeypress(paddle_down, 's')
screen.onkeypress(paddle_left, 'a')
screen.onkeypress(paddle_right, 'd')
screen.listen()
play()
screen.mainloop()

Pong Created in Python Turtle

I'm new to Python but I've coded in other languages, mainly for hardware. I made pong in Python using turtle but it's a little glitchy. I was wondering if any of you could check it out and give advice. I also want to add a start screen and end screen but am a little unsure how to. Also if you have any advice on how to improve collision detection, please let me know.
I'm pasting code below
import os
import math
import random
import time
#set up screen
screen = turtle.Screen()
screen.bgcolor("green")
screen.title("Pong")
# set up border
border_pen = turtle.Turtle()
border_pen.speed(0)
border_pen.color("white")
border_pen.penup()
border_pen.setposition(-300,-300)
border_pen.pendown()
border_pen.pensize(3)
for side in range(4):
border_pen.fd(600)
border_pen.lt(90)
border_pen.hideturtle()
#set score to 0
score = 0
#set time to zero
time = 0
seconds = 0
#Draw score
score_pen = turtle.Turtle()
score_pen.speed(0)
score_pen.color("white")
score_pen.penup()
score_pen.setposition(-290, 310)
scorestring = "Score %s" %score
score_pen.write(scorestring, False, align="left", font= ("Arial", 14, "normal"))
score_pen.hideturtle()
#Draw timer
time_pen = turtle.Turtle()
time_pen.speed(0)
time_pen.color("white")
time_pen.penup()
time_pen.setposition(260, 310)
timestring = "Time %s" %time
time_pen.write(timestring, False, align="left", font= ("Arial", 14, "normal"))
time_pen.hideturtle()
#create the player turtle
player = turtle.Turtle()
player.color("blue")
player.shape("square")
player.shapesize(0.5, 4)
player.penup()
player.speed(0)
player.setposition(-280,-250)#(x,y)
player.setheading(90)
playerspeed = 15
#create the AIplayer turtle
AIplayer = turtle.Turtle()
AIplayer.color("black")
AIplayer.shape("square")
AIplayer.shapesize(0.5, 4)
AIplayer.penup()
AIplayer.speed(0)
AIplayer.setposition(280,250)#(x,y)
AIplayer.setheading(90)
AIplayerspeed = 15
#create the pong
pong = turtle.Turtle()
pong.color("red")
pong.shape("circle")
pong.shapesize(0.5, 0.5)
pong.penup()
pong.speed(10)
pong.setposition(0,0)#(x,y)
pongspeed = 15
pong.goto(0, 265)
pong.dy = -5
pong.dx = 5
#Move player up and down
def move_up():
y = player.ycor()
y += playerspeed
if y > 265:
y = 260
player.sety(y)
def move_down():
y = player.ycor()
y -= playerspeed
if y < -265:
y = -260
player.sety(y)
#keyboard bindings
turtle.listen()
turtle.onkey(move_up, "Up")
turtle.onkey(move_down, "Down")
#turtle.onkey(fire_bullet, "space")
def isCollision(t1, t2):
distance = math.sqrt(math.pow(t1.xcor()- t2.xcor(),2)+math.pow(t1.ycor()-t2.ycor(),2))
if distance < 20:
return True
else:
return False
#main game loop
while True:
#move pong ball
pong.sety(pong.ycor() +pong.dy)
pong.setx(pong.xcor() +pong.dx)
#check for bounce and redirect it
if pong.ycor() < -300:
pong.dy *= -1
if pong.ycor() > 300:
pong.dy *= -1
if pong.xcor() < -300:
pong.dx *= -1
print("Game Over")
exit()
if pong.xcor() > 300:
pong.dx *= -1
#move AI paddle (might speed up pong movement)
y = pong.ycor()
y += AIplayerspeed
AIplayer.sety(y)
if AIplayer.ycor() > 265:
AIplayerspeed *= -1
if AIplayer.ycor() < -250:
AIplayerspeed *= -1
#collision pong and player
if isCollision(pong, player):
pong.dy *= -1
pong.dx *= -1
#Update the score
score += 10
scorestring = "Score: %s" %score
score_pen.clear()
score_pen.write(scorestring, False, align="left", font=("Arial", 14, "normal"))
#collision pong and AIplayer
if isCollision(pong, AIplayer):
pong.dy *= -1
pong.dx *= -1
#updates timer and increases ball speed
if seconds > 29:
pong.dy *= -2
pong.dx *= -2
if seconds > 59:
pong.dy *= -3
pong.dx *= -3
#displays timer but makes game laggy
# seconds += 0.1
# time = seconds
# timestring = "Time: %s" %time
# time_pen.clear()
# time_pen.write(timestring, False, align="Left", font=("Arial", 14, "normal"))
Some issues I see:
You've stretched a 20 by 20 square to be 10 by 80:
AIplayer.shapesize(0.5, 4)
But your collision distance is only 20 from center, so your ball can
cross over the bottom or top of the paddle without actually
colliding.
Your isCollision() function is mostly redundant with turtle's own
.collision() method.
You shouldn't have while True: in an event-driven world like turtle
as it potentially prevents some events from firing. Better to
replace it with a timer event.
Some of your collisions change both x and y deltas when they should
only change one of them.
You should avoid redundant queries of the turtle in your main loop as well as checking conditions that are negated by other logic clauses. I.e. do as little as you can get away with in the main loop.
Below is my rework of your game along the above lines, plus lots of other style and logic changes:
from turtle import Turtle, Screen
FONT = ("Arial", 16, "normal")
def isCollision(t1, t2):
return t1.distance(t2) < 15
# set up screen
screen = Screen()
screen.bgcolor("darkgreen")
screen.title("Pong")
# set up border
border_pen = Turtle(visible=False)
border_pen.speed('fastest')
border_pen.color('white')
border_pen.pensize(3)
border_pen.penup()
border_pen.setposition(-300, -300)
border_pen.pendown()
for _ in range(4):
border_pen.forward(600)
border_pen.left(90)
# set score to 0
score = 0
# set time to zero
seconds = 0
# Draw score
score_pen = Turtle(visible=False)
score_pen.color("white")
score_pen.penup()
score_pen.setposition(-290, 310)
score_pen.write("Score {}".format(score), False, align="left", font=FONT)
# Draw timer
time_pen = Turtle(visible=False)
time_pen.color("white")
time_pen.penup()
time_pen.setposition(260, 310)
time_pen.write("Time {}".format(int(seconds)), False, align="left", font=FONT)
# create the player turtle
player = Turtle("square", visible=False)
player.shapesize(0.5, 3)
player.speed('fastest')
player.setheading(90)
player.color("blue")
player.penup()
player.setposition(-280, -250) # (x,y)
player.showturtle()
playerspeed = 15
# create the AIplayer turtle
AIplayer = Turtle("square", visible=False)
AIplayer.shapesize(0.5, 3)
AIplayer.speed('fastest')
AIplayer.setheading(90)
AIplayer.color("black")
AIplayer.penup()
AIplayer.setposition(280, 250) # (x,y)
AIplayer.showturtle()
AIplayerspeed = 15
# create the pong
pong = Turtle("circle", visible=False)
pong.shapesize(0.5, 0.5)
pong.speed('fast')
pong.color("red")
pong.penup()
pong.sety(265)
pong.showturtle()
pongspeed = 15
pong_dx, pong_dy = 5, -5
# Move player up and down
def move_up():
player.forward(playerspeed)
y = player.ycor()
if y > 265:
y = 260
player.sety(y)
screen.update()
def move_down():
player.backward(playerspeed)
y = player.ycor()
if y < -265:
y = -260
player.sety(y)
screen.update()
# keyboard bindings
screen.onkey(move_up, "Up")
screen.onkey(move_down, "Down")
screen.listen()
# main game loop
def move():
global pong_dx, pong_dy, AIplayerspeed, seconds, score
# move pong ball
x, y = pong.position()
x += pong_dx
y += pong_dy
pong.setposition(x, y)
if isCollision(pong, player): # collision pong and player
pong_dx *= -1
# Update the score
score += 10
score_pen.undo()
score_pen.write("Score: {}".format(score), align="left", font=FONT)
elif isCollision(pong, AIplayer): # collision pong and AIplayer
pong_dx *= -1
elif y < -300 or y > 300: # check for bounce and redirect it
pong_dy *= -1
elif x > 300:
pong_dx *= -1
elif x < -300:
print("Game Over")
screen.bye()
return
# move AI paddle (might speed up pong movement)
AIplayer.forward(AIplayerspeed)
y = AIplayer.ycor()
if y < -250 or y > 250:
AIplayerspeed *= -1
# display timer
seconds += 0.05
time_pen.undo()
time_pen.write("Time: {}".format(int(seconds)), False, align="Left", font=FONT)
screen.ontimer(move, 50)
screen.update()
screen.tracer(False)
move()
screen.mainloop()
I couldn't make sense of your "increases ball speed" logic so I left it out. I used a darker background color as the original was difficult to look at on my screen.
The issue related to the pong/ball speed can be due to your CPU. The ball moves 5 pixels in both (x and y) direction every screen update and the update rate depends on the CPU speed. The processes being executed by the CPU aren't constant and thus the CPU speed fluctuates. Also because we have screen.tracer() set to False it doesn't show animations. Thus, due to lack of animation and inconsistency of CPU speed, we see the speed of pong/ball increasing/decreasing.
For screen.tracer() explanation refer https://youtu.be/LH8WgrUWG_I?t=123
For "the increasing speed" explanation refer https://youtu.be/Hw1H3rG3POM?t=40
To get a bit more consistent behavior, you could add a long enough pause into your loop in order to offset the variability of the computing times and to avoid drawing useless frames. You can do this using time
import time
# Initialization
...
while True:
time.sleep(1 / 60)
# The rest of your game logic
...

Categories

Resources