Continuous background music with winsound module - python

So, I have been working on this game called Space Invader. Everything is doing great except for one thing which is the sound. As in the below code, I've imported winsound module and it is not doing well. When I fire the bullet it makes the fire noise which I wanted but the problem is that I've also added background music, and whenever I hit fire it stops the background music and starts making fire noises until it hits the target. I want background music to run continuously. I've also seen many Youtube videos but couldn't get any help. Can anyone tell me the mistake which I've made in the below code?
import turtle
import random
import math
import winsound
score = 0
highscore = 0
targets = []
live_remaining = 3
screen = turtle.Screen()
screen.title("Space War")
screen.bgcolor("Black")
screen.setup(width=800, height=770)
screen.bgpic("Space.gif")
screen.addshape("Spaceship.gif")
screen.addshape("Missile.gif")
screen.addshape("ufo.gif")
screen.addshape("gameover.gif")
screen.addshape("youwin.gif")
screen.tracer(0)
winsound.PlaySound("bgmusic.wav", winsound.SND_ASYNC)
spaceship = turtle.Turtle()
spaceship.speed(0)
spaceship.shape("Spaceship.gif")
spaceship.penup()
spaceship.ht()
spaceship.goto(0, -320)
spaceship.st()
spaceship.direction = "stop"
spaceshipspeed = 15
num_of_targets = 50
for i in range(num_of_targets):
targets.append(turtle.Turtle())
target_start_x = -250
target_start_y = 275
target_number = 0
for target in targets:
target.shape("ufo.gif")
target.penup()
target.speed(0)
x=target_start_x + (50 * target_number)
y=target_start_y
target.goto(x, y)
target_number += 1
if target_number == 10:
target_start_y -= 20
target_number = 0
targetspeed = 0.5
fires=turtle.Turtle()
fires.shape("Missile.gif")
fires.penup()
fires.ht()
fires.speed(0)
fires.goto(0, -340)
fires.direction = "stop"
firesspeed=3
fire = turtle.Turtle()
fire.shape("Missile.gif")
fire.penup()
fire.ht()
fire.speed(0)
fire.goto(0, -340)
fire.direction = "stop"
firespeed = 4
firestate = "ready"
sb=turtle.Turtle()
sb.shape("square")
sb.color("white")
sb.ht()
sb.penup()
sb.goto(0,320)
sb.write("Score: 0 | High Score: 0", align="center", font=("courier", 30, "bold"))
life = turtle.Turtle()
life.shape("square")
life.ht()
life.penup()
life.goto(-350,-350)
life.color("white")
life.write("Life remaining: 3", font=("courier",10,"bold"))
def go_left():
x = spaceship.xcor()
x -= spaceshipspeed
if x < -340:
x = -340
spaceship.setx(x)
def go_right():
x = spaceship.xcor()
x += spaceshipspeed
if x > 340:
x = 340
spaceship.setx(x)
def go_forward():
global firestate
if firestate == "ready":
firestate = "fire"
x = spaceship.xcor()
y = spaceship.ycor()
fire.setposition(x, y)
fire.st()
winsound.PlaySound("fire.wav", winsound.SND_ASYNC)
def move():
pass
screen.listen()
screen.onkeypress(go_left, "a")
screen.onkeypress(go_right, "d")
screen.onkeypress(go_forward, "space")
while True:
screen.update()
for target in targets:
x = target.xcor()
x += targetspeed
target.setx(x)
if target.xcor() > 340:
targetspeed *= -1
for t in targets:
y = t.ycor()
y -= 20
t.sety(y)
if target.xcor() < -340:
targetspeed *= -1
for t in targets:
y = t.ycor()
y -= 20
t.sety(y)
if fire.distance(target) < 20:
fire.direction = "stop"
fire.ht()
fire.clear()
firestate = "ready"
fire.goto(10000000, 1000000)
target.goto(0, 10000000)
score += 10
winsound.PlaySound("explosion.wav", winsound.SND_ASYNC)
if score > highscore:
highscore = score
sb.clear()
sb.write("Score: {} | High Score: {}".format(score,highscore), align="center",
font=("courier", 30, "bold"))
if score == 500:
screen.clear()
screen.bgcolor("black")
gf = turtle.Turtle()
gf.shape("youwin.gif")
gf.goto(0, 0)
winsound.PlaySound("gamewin.wav",winsound.SND_ASYNC)
if spaceship.distance(target) < 60:
spaceship.goto(0, -320)
target.goto(-320, 300)
live_remaining -= 1
winsound.PlaySound("explosion.wav",winsound.SND_ASYNC)
life.clear()
life.write("Life remaining: {}".format(live_remaining), font=("courier",10,"bold"))
if live_remaining == 0:
screen.clear()
screen.bgcolor("black")
gover=turtle.Turtle()
gover.shape("gameover.gif")
gover.goto(0, 0)
winsound.PlaySound("gamelose.wav", winsound.SND_ASYNC)
replay = input("Press r to retry",)
y = fire.ycor()
y += firespeed
fire.sety(y)
if fire.ycor() > 300:
fire.ht()
firestate = "ready"
move()
screen.mainloop()

Related

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

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()

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
...

Python Turtle Graphics Game Lagging

My Python game is lagging a lot. I started making a new Python game and every time it came to testing it, it would always lag although I am not using any images.
import turtle
import random
#head orientation
h = [0]
#score
a = [0]
b = [0]
#food coord
fcoord = [0,0,0]
#position
pos = []
def home(x,y):
x = 0
y = 0
a[0] = 0
b[0] = 0
h[0] = 0
fcoord[2] = 0
pos[:] = []
turtle.hideturtle()
turtle.clear()
turtle.pu()
turtle.color("lime")
turtle.goto(0,0)
turtle.write("PLAY", align="center",font="Calibri")
turtle.title("Snake Game")
turtle.onscreenclick(start)
turtle.mainloop()
def level_1():
turtle.clear()
turtle.pu()
turtle.speed(0)
turtle.pensize(20)
turtle.color("grey")
turtle.goto(-220,220)
turtle.pd()
turtle.goto(220,220)
turtle.goto(220,-220)
turtle.goto(-220,-220)
turtle.goto(-220,220)
turtle.pu()
turtle.goto(0,0)
def start(x,y):
turtle.onscreenclick(None)
level_1()
tfood = turtle.Turtle()
tfood.hideturtle()
tfood.pu()
tfood.speed(0)
tfood.shape("square")
tfood.color("red")
tscore = turtle.Turtle()
tscore.hideturtle()
tscore.pu()
tscore.speed(0)
tscore.goto(100,-250)
tscore.write("Score:" + str(a[0]), align="center",font=(10))
while x > -210 and x < 210 and y > -210 and y <210:
if fcoord[2] == 0:
food(tfood)
fcoord[2] = 1
turtle.onkey(u,"Up")
turtle.onkey(l,"Left")
turtle.onkey(r,"Right")
turtle.onkey(d,"Down")
turtle.listen()
move()
x = turtle.xcor()
y = turtle.ycor()
if x > fcoord[0]*20-5 and x < fcoord[0]*20+5 and y > fcoord[1]*20-5 and y < fcoord[1]*20+5:
fcoord[2] = 0
tfood.clear()
a[0] += 1
tscore.clear()
tscore.write("Score:" + str(a[0]), align="center",font=(10))
if len(pos) > 1:
for i in range(1,len(pos)):
if x < pos[i][0]+5 and x > pos[i][0]-5 and y < pos[i][1]+5 and y > pos[i][1]-5:
tscore.clear()
tfood.clear()
gameover()
tscore.clear()
tfood.clear()
gameover()
#Food
def food(tfood):
x = random.randrange(-8,8,1)
y = random.randrange(-8,8,1)
fcoord[0] = x
fcoord[1] = y
tfood.hideturtle()
tfood.pu()
tfood.shape("circle")
tfood.color("red")
tfood.goto(x*20,y*20)
tfood.stamp()
#Up
def u():
if h[0] == 270:
pass
else:
h[0] = 90
#Down
def d():
if h[0] == 90:
pass
else:
h[0] = 270
#Left
def l():
if h[0] == 0:
pass
else:
h[0] = 180
#Right
def r():
if h[0] == 180:
pass
else:
h[0] = 0
def move():
turtle.pensize(1)
turtle.color("green")
turtle.pu()
turtle.speed(3)
turtle.setheading(h[0])
turtle.shape("square")
turtle.stamp()
turtle.fd(20)
x = turtle.xcor()
y = turtle.ycor()
if b[0] > a[0]:
turtle.clearstamps(1)
pos.insert(0,[round(x),round(y)])
pos.pop(-1)
else:
pos.insert(0,[round(x),round(y)])
b[0] += 1
def gameover():
turtle.onscreenclick(None)
turtle.speed(0)
turtle.pu()
turtle.goto(0,150)
turtle.color("red")
turtle.write("Game Over",align="center", font=(10))
turtle.goto(0,50)
turtle.write("Score:" + str(a[0]),align="center",font=(10))
turtle.goto(200,-200)
turtle.write("(Click anywhere to return to the main menu)",align="right",font=(0.0000001))
turtle.onscreenclick(home)
turtle.mainloop()
# # # # # # # # # # # # # # # # # # # # # #
# Main #
# # # # # # # # # # # # # # # # # # # # # #
if __name__ == '__main__':
home(0,0)
I'm not sure what you mean by lagging but I've addressed a couple of issues in my rewrite below. The first is that turtles are global entities that don't get garbage collected under normal circumstances so don't recreate them, reuse them instead. Second, when you have your own while loop to control the game, you potentially lock out events. I've eliminated your loop and modified the game to work with ontimer events so that turtle movement should be handled by the same event loop that's handing user input, making it slightly more responsive to the user:
from turtle import Turtle, Screen
from random import randint
FONT = ('Arial', 18, 'bold')
# Up
def u():
if h[0] != 270:
h[0] = 90
# Down
def d():
if h[0] != 90:
h[0] = 270
# Left
def l():
if h[0] != 0:
h[0] = 180
# Right
def r():
if h[0] != 180:
h[0] = 0
def gameover():
screen.onkey(None, "Up")
screen.onkey(None, "Left")
screen.onkey(None, "Right")
screen.onkey(None, "Down")
tscore.clear()
tfood.clear()
tplayer.clear()
tfood.hideturtle()
tplayer.hideturtle()
tscore.color("red")
tscore.goto(0, 150)
tscore.write("Game Over", align="center", font=FONT)
tscore.goto(0, 50)
tscore.write("Score:" + str(a[0]), align="center", font=FONT)
tscore.goto(0, -200)
tscore.write("(Click anywhere to return to the main menu)", align="center", font=FONT)
screen.onscreenclick(home)
def food(tfood):
x = randint(-160, 160)
y = randint(-160, 160)
tfood.goto(x, y)
tfood.showturtle()
def move():
x, y = tplayer.position()
if -210 < x < 210 and -210 < y < 210:
if not tfood.isvisible():
food(tfood)
tplayer.setheading(h[0])
tplayer.stamp()
tplayer.forward(20)
if b[0] > a[0]:
tplayer.clearstamps(1)
pos.insert(0, [round(x), round(y)])
pos.pop(-1)
else:
pos.insert(0, [round(x), round(y)])
b[0] += 1
if tplayer.distance(tfood) < 15:
tfood.hideturtle()
tfood.clear()
a[0] += 1
tscore.clear()
tscore.write("Score:" + str(a[0]), align="center", font=FONT)
flag = True
x, y = tplayer.position()
if len(pos) > 1:
for i in range(1, len(pos)):
if pos[i][0] - 5 < x < pos[i][0] + 5 and pos[i][1] - 5 < y < pos[i][1] + 5:
flag = False
break
if flag:
screen.ontimer(move, 25)
else:
screen.ontimer(gameover, 100)
def level_1():
tmarker.penup()
tmarker.goto(-220, 220)
tmarker.pendown()
tmarker.goto(220, 220)
tmarker.goto(220, -220)
tmarker.goto(-220, -220)
tmarker.goto(-220, 220)
tmarker.penup()
def start(x, y):
screen.onscreenclick(None)
tscore.clear()
level_1()
tplayer.home()
tplayer.setheading(h[0])
tscore.goto(100, -250)
tscore.write("Score:" + str(a[0]), align="center", font=FONT)
screen.onkey(u, "Up")
screen.onkey(l, "Left")
screen.onkey(r, "Right")
screen.onkey(d, "Down")
move()
def home(x=0, y=0):
screen.onscreenclick(None)
a[0] = 0
b[0] = 0
h[0] = 0
pos[:] = []
tscore.clear()
tscore.home()
tscore.color('lime')
tscore.write("PLAY", align="center", font=FONT)
screen.onscreenclick(start)
# head orientation
h = [0]
# score
a = [0]
b = [0]
# position
pos = []
# turtles
tfood = Turtle('circle', visible=False)
tfood.speed('fastest')
tfood.color('red')
tfood.penup()
tscore = Turtle(visible=False)
tscore.speed('fastest')
tscore.penup()
tplayer = Turtle("square", visible=False)
tplayer.speed('slow')
tplayer.color("green")
tplayer.penup()
tmarker = Turtle(visible=False)
tmarker.speed('fastest')
tmarker.pensize(20)
tmarker.color("grey")
# # # # # # # # # # # # # # # # # # # # # #
# Main #
# # # # # # # # # # # # # # # # # # # # # #
if __name__ == '__main__':
screen = Screen()
screen.title("Snake Game")
screen.listen()
home()
screen.mainloop()

Categories

Resources