How to make the object bounce back with turtle - python

I want to make the yellow block jump on the line when there are other shapes. Also, how could I make the screen to close when the yellow block touches the other blocks.
I have though about using the time module and put time.sleep for 1 seconds after blockself.goto(0,30) then make the block go down to (0,0). However, when I did that the whole screen frozed for 1 second. This means that the time.sleep was not aimed for the blockself(yellow block) it self. Is there any way to fix this.
from turtle import Screen, Turtle
import random
WIDTH, HEIGHT = 800, 400
CURSOR_SIZE = 20
BASELINE = -CURSOR_SIZE/2
def move_block(block):
block.x -= CURSOR_SIZE/2
block.setx(block.x)
if block.x <= CURSOR_SIZE/2 - WIDTH/2:
block.x += WIDTH + CURSOR_SIZE
block.setx(block.x)
screen.update()
screen.ontimer(lambda: move_block(block), 40) # delay in milliseconds
screen = Screen()
screen.title("Jump over!")
screen.setup(WIDTH, HEIGHT)
screen.tracer(False)
marker = Turtle()
marker.hideturtle()
marker.penup()
marker.goto(WIDTH/2, BASELINE)
marker.pendown()
marker.goto(-WIDTH/2, BASELINE)
#three blocks
shape1=["square","triangle","circle"]
block_1 = Turtle(shape=(random.choice(shape1)))
block_1.up()
block_1.color('black')
block_1.x = WIDTH/2 + CURSOR_SIZE # user defined property
block_1.setx(block_1.x)
shape2=["square","triangle","circle"]
block_2 = Turtle(shape=(random.choice(shape2)))
block_2.up()
block_2.color('black')
block_2.x = block_1.x + 300
block_2.setx(block_2.x)
shape3=["square","triangle","circle"]
block_3 = Turtle(shape=(random.choice(shape3)))
block_3.up()
block_3.color('black')
block_3.x = block_2.x + 300
block_3.setx(block_3.x)
move_block(block_1)
move_block(block_2)
move_block(block_3)
# self
blockself= Turtle(shape="square")
blockself.color('yellow')
blockself.setx(0)
blockself.direction="Stop"
def goup():
blockself.up()
blockself.goto(0,30)
screen.listen ()
screen.onkey ( goup , "w" )
screen.mainloop()

We can make the yellow block vertical jump independent of the other motion using the same mechanism I gave you for the horizontal block motion, ontimer(). The jump() method below is invoked by the keyboard event, moving the yellow block up and down as a series of timer events:
from turtle import Screen, Turtle
from random import choice
SHAPES = ['square', 'triangle', 'circle']
WIDTH, HEIGHT = 800, 400
CURSOR_SIZE = 20
BASELINE = -CURSOR_SIZE/2
def move_block(block):
block.x -= CURSOR_SIZE/2
block.setx(block.x)
if block.x <= CURSOR_SIZE/2 - WIDTH/2:
block.x += WIDTH + CURSOR_SIZE
block.setx(block.x)
screen.update()
if block.distance(block_self) < CURSOR_SIZE:
screen.bye()
else:
screen.ontimer(lambda: move_block(block), 65) # delay in milliseconds
def jump(direction):
screen.onkey(None, 'w') # disable jumping while jumping
y = block_self.ycor()
if direction > 0 and y >= 40:
direction = -1
elif direction < 0 and y <= 0:
block_self.sety(0)
direction = 0
if direction:
block_self.sety(y + direction)
screen.ontimer(lambda: jump(direction), 25)
else:
screen.onkey(lambda: jump(1), 'w') # jump over, reenable jumping
screen.update()
screen = Screen()
screen.title("Jump over!")
screen.setup(WIDTH, HEIGHT)
screen.tracer(False)
marker = Turtle()
marker.hideturtle()
marker.penup()
marker.goto(WIDTH/2, BASELINE)
marker.pendown()
marker.goto(-WIDTH/2, BASELINE)
block_1 = Turtle(shape=choice(SHAPES))
block_1.penup()
block_1.color('black')
block_1.x = WIDTH/2 + CURSOR_SIZE # user defined property
block_1.setx(block_1.x)
block_2 = Turtle(shape=choice(SHAPES))
block_2.penup()
block_2.color('black')
block_2.x = block_1.x + 266
block_2.setx(block_2.x)
block_3 = Turtle(shape=choice(SHAPES))
block_3.penup()
block_3.color('black')
block_3.x = block_2.x + 266
block_3.setx(block_3.x)
block_self = Turtle(shape='square')
block_self.penup()
block_self.color('yellow')
move_block(block_1)
move_block(block_2)
move_block(block_3)
screen.onkey(lambda: jump(1), 'w')
screen.listen()
screen.mainloop()
Also, how could I make the screen to close when the yellow block
touches the other blocks.
It seems a severe response to user miscalculation but I put it in the code above by adding the following conditional to the move_block() function:
if block.distance(block_self) < CURSOR_SIZE:
screen.bye()
Finally, as you're discovering yourself, time.sleep() has no business being used in an event-driven world like turtle.

Related

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.

I have problem with my game, when the ball goes over the block, it is not deleting from my screen

Someone can help me please?
I spent many hours to understand the problem.. (Without success 🤣)
I tried to built a 80s hit game.
The problem is that when the ball goes on one of the stones, I catch the event (shows me in the console) and update the list of blocks but the block is not deleted from the screen as if nothing happened and I did not update any list
this is my code:
import time
from turtle import Screen, Turtle
import random
screen = Screen()
screen.bgcolor("black")
screen.setup(width=800, height=600)
screen.tracer(0)
player = Turtle("square")
player.shapesize(1.0, 4.0, 0)
player.color("blue")
player.penup()
player.goto(0, -250)
screen.tracer(0)
blue_turtle = []
blue_positions = []
for i in range(7):
turtle = Turtle("square")
turtle.shapesize(2.0 ,4.0, 0)
turtle.color("blue")
turtle.penup()
blue_positions.append((-300 + i * 100, 200))
blue_turtle.append(turtle)
def renderBlocks(b):
for i in range(len(b) - 1):
if b[i] == None:
print("Skipped")
continue
else:
x, y= blue_positions[i]
b[i].goto(x=x, y=y)
# total += 100
ball = Turtle("circle")
ball.color("white")
game_on = True
def move():
x = ball.xcor() + 10
y = ball.ycor()+ 10
ball.goto(x=x, y=y)
while game_on:
move()
renderBlocks(blue_turtle)
for i in range(len(blue_turtle) - 1):
if (blue_turtle[i] != None) and ball.distance(blue_turtle[i]) < 20:
blue_turtle[i] = (None)
time.sleep(0.1)
screen.update()
screen.exitonclick()
I believe the problem is here:
blue_turtle[i] = (None)
You tell your code that the turtle has been eliminated but you don't tell turtle graphics that it should be removed. Instead consider:
blue_turtle[i].hideturtle()
blue_turtle[i] = None
My rework of your code addressing this issue and some others:
from turtle import Screen, Turtle
def renderBlocks(b):
for i in range(len(b) - 1):
if b[i]:
x, y = blue_positions[i]
b[i].goto(x, y)
def move():
if game_on:
x = ball.xcor() + 10
y = ball.ycor() + 10
ball.goto(x, y)
renderBlocks(blue_turtles)
for i in range(len(blue_turtles) - 1):
if blue_turtles[i] and ball.distance(blue_turtles[i]) < 20:
blue_turtles[i].hideturtle()
blue_turtles[i] = None
screen.update()
screen.ontimer(move, 100) # milliseconds
screen = Screen()
screen.setup(width=800, height=600)
screen.bgcolor('black')
screen.tracer(0)
player = Turtle('square')
player.shapesize(1, 4, 0)
player.color('blue')
player.penup()
player.sety(-250)
blue_turtles = []
blue_positions = []
for i in range(7):
turtle = Turtle('square')
turtle.shapesize(2, 4, 0)
turtle.color('blue')
turtle.penup()
blue_positions.append((-300 + i * 100, 200))
blue_turtles.append(turtle)
ball = Turtle('circle')
ball.color('white')
game_on = True
move()
screen.exitonclick()

Trying to get actor to wrap around screen (go from one side to the other at edge of screen) also trying to get animation to work:

import pgzrun
import pygame
WIDTH = 850
HEIGHT = 425
#sprites
title = Actor('title.png') #calling sprites and saying their pos
title.pos = 400, 212
cont = Actor('cont.png')
cont.pos = 470, 300
player = Actor('ship.png')
player.pos = 100, 56
foe = Actor('foe.png')
foe.pos = 200, 112
vel = 5
music.play("temp.mp3")
#controls
def draw():
screen.clear()
screen.fill((0, 0, 0))
title.draw()
cont.draw()
foe.draw()
player.draw()
def update():
keys = pygame.key.get_pressed()
x, y = player.pos
x += (keys[pygame.K_d] - keys[pygame.K_a]) * vel
y += (keys[pygame.K_s] - keys[pygame.K_w]) * vel
player.pos = x, y
if player.left > WIDTH: #if player hits right side of screen it takes the player to the other side of the screen
player.right = 0
def otherimage(): #animation of ship
player.image = 'ship.png'
imagey()
def imagey():
player.image = 'ship1.png'
clock.schedule_unique(otherimage, 1.0)
imagey()
pgzrun.go()
the animation section is the bit commented with "animation of ship". The wrapping bit is the bit labled "if player hits right side of screen it takes the player to the other side of the screen" as you can see i have go it working when the player hits the screen on the right side but every time i have tried getting it to work on the other sides it doesn't work. Please help! Thanks! Im new to coding and i am trying to understand this language. thanks again.

Detect collision of two Turtles using 'if' statements

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.

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