所以我一直试图仅使用标准库在python中制作游戏,因为我不知道如何链接库(我是绝对的初学者),但是每当我运行该程序时,它都不会响应。这是代码:
import turtle
import random
import math
import time
import os
play = True
#window
window = turtle.Screen()
window.bgcolor("#4287f5")
window.title("Box Grab!")
#border
outer = turtle.Turtle()
outer.speed(0)
outer.color("#000000","#000000")
outer.up()
outer.goto(250, 250)
outer.down
outer.begin_fill()
for i in range(4):
outer.rt(90)
outer.fd(500)
outer.ht()
#boxes
box1 = turtle.Turtle()
box2 = turtle.Turtle()
box3 = turtle.Turtle()
box4 = turtle.Turtle()
box5 = turtle.Turtle()
box6 = turtle.Turtle()
box1n = 2
box2n = 2
box3n = 2
box4n = 2
box5n = 2
box6n = 2
box1.penup()
box1.speed(0)
box1.color("darkorange")
box1.shape("square")
box2.penup()
box2.speed(0)
box2.color("darkorange")
box2.shape("square")
box3.penup()
box3.speed(0)
box3.color("darkorange")
box3.shape("square")
box4.penup()
box4.speed(0)
box4.color("darkorange")
box4.shape("square")
box5.penup()
box5.speed(0)
box5.color("darkorange")
box5.shape("square")
box6.penup()
box6.speed(0)
box6.color("darkorange")
box6.shape("square")
box1.setpos(random.randint(-250, 250), random.randint(-250, 250))
box2.setpos(random.randint(-250, 250), random.randint(-250, 250))
box3.setpos(random.randint(-250, 250), random.randint(-250, 250))
box4.setpos(random.randint(-250, 250), random.randint(-250, 250))
box5.setpos(random.randint(-250, 250), random.randint(-250, 250))
box6.setpos(random.randint(-250, 250), random.randint(-250, 250))
#player
player = turtle.Turtle()
player.penup()
player.color("blue")
player.shape("circle")
#defining movement
def down():
player.setheading(270)
player.forward(20)
def right():
player.setheading(0)
player.forward(20)
def left():
player.setheading(180)
player.forward(20)
def up():
player.setheading(90)
player.forward(20)
#collision
def isCollision(t1, t2):
d = math.sqrt(math.pow(t1.xcor()-t2.xcor(),2) + math.pow(t1.ycor()-t2.ycor(),2))
if d < 20:
return True
else:
return False
if isCollision(player, box1):
box1.hideturtle()
box1n - 1
if isCollision(player, box2):
box2.hideturtle()
box2n - 1
if isCollision(player, box3):
box3.hideturtle()
box3n - 1
if isCollision(player, box4):
box4.hideturtle()
box4n - 1
if isCollision(player, box5):
box5.hideturtle()
box5n - 1
if isCollision(player, box6):
box6.hideturtle()
box6n - 1
#movement
turtle.listen()
turtle.onkey(up,"w")
turtle.onkey(down,"s")
turtle.onkey(right,"d")
turtle.onkey(left,"a")
#time
def mainTime():
x = 60
for i in range(60 + 1):
time.sleep(1)
print(formatTime(x))
x -= 1
def formatTime(x):
minutes = int(x / 60)
seconds_rem = int(x % 60)
if (seconds_rem < 10):
return(str(minutes) + ":0" + str(seconds_rem))
else:
return(str(minutes) + ":" + str(seconds_rem))
if x == 0:
play = false
mainTime()
#score
def score():
score = 0
for s in range(60):
t = 60
time.sleep(1)
t -= 1
if box1 or box2 or box3 or box4 or box5 or box6 == 1:
score = score + (seconds_rem * 100)
score()
#game over
if play == false:
print("GAME OVER. ")
restart = input("RESTART? Y/N")
if restart == "y":
os.system('python "C:\\Users\\lyons\\AppData\\Local\\Programs\\Python\\Python37-32\\Projects\\failing to make a game.py"')
此外,当它过去正常工作时,在玩家击中盒子后它们不会消失。我该如何解决?