Apple Catch Game With Python
- Jackhammer
- Jan 12, 2020
- 1 min read
import turtle
import time
import random
import sys
# misc
caught = 0
miss = 0
win = turtle.Screen()
win.bgcolor("sky blue")
win.title("Apple Catch")
win.setup(600,600)
char = turtle.Turtle()
char.shape("square")
char.penup()
char.shapesize(2,13)
char.color("dark green")
char.hideturtle()
char.goto(0,-250)
char.showturtle()
pen = turtle.Turtle()
pen.color("red")
pen.penup()
pen.hideturtle()
pen.goto(0 , 200)
pen.speed(0)
pen.pensize(6)
def lt():
char.backward(20)
def rt():
char.fd(20)
def block():
if (char.xcor() <= -250):
char.setx(-250)
def rblock():
if (char.xcor() >= 250):
char.setx(250)
win.listen()
win.onkeypress(lt , "a")
win.onkeypress(rt , "d")
#game loop
while True:
block()
rblock()
posx = (random.randint(-220,220))
app = turtle.Turtle()
app.rt(90)
app.penup()
app.color("red")
app.shape("circle")
app.shapesize(2)
app.hideturtle()
app.goto(posx , 250)
app.showturtle()
app.fd(500)
if(app.xcor() > char.xcor() - 130 and app.xcor() < char.xcor() + 130 and app.ycor() < char.ycor() + 20 and app.ycor() > char.ycor() - 20):
caught += 1
pen.undo()
pen.write("SCORE: " + str(caught) + " " + " MISSED: " + str(miss) , align = "center" , font = ("Arial" , 24 , "bold"))
app.hideturtle()
else:
miss += 1
app.hideturtle()
pen.undo()
pen.write("SCORE: " + str(caught) + " " + " MISSED: " + str(miss) , align = "center" , font = ("Arial" , 24 , "bold"))
if (miss == 5):
pen.undo()
pen.write("GAME OVER", align = "center" , font = ("Arial" , 24 , "bold"))
break
sys.exit(3)
Kommentare