Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我的python游戏在运行时没有响应_Python - Fatal编程技术网

我的python游戏在运行时没有响应

我的python游戏在运行时没有响应,python,Python,所以我一直试图用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

所以我一直试图用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"')
导入海龟
随机输入
输入数学
导入时间
导入操作系统
播放=正确
#窗口
window=turtle.Screen()
window.bgcolor(“#4287f5”)
window.title(“框抓取!”)
#边界
外层=海龟。海龟()
外部速度(0)
外部颜色(“000000”,“000000”)
外
外。后藤(250250)
外翻
外部。开始填充()
对于范围(4)中的i:
外部。rt(90)
外部fd(500)
outer.ht()
#盒子
box1=海龟。海龟()
box2=海龟。海龟()
box3=海龟。海龟()
box4=海龟。海龟()
box5=海龟。海龟()
box6=海龟。海龟()
box1n=2
box2n=2
box3n=2
box4n=2
box5n=2
box6n=2
框1.penup()
箱1.速度(0)
框1.颜色(“darkorange”)
框1.形状(“方形”)
框2.penup()
箱2.速度(0)
框2.颜色(“darkorange”)
框2.形状(“方形”)
框3.penup()
框3.速度(0)
框3.颜色(“darkorange”)
框3.形状(“方形”)
框4.penup()
箱4.速度(0)
方框4.颜色(“达克朗格”)
方框4.形状(“方形”)
框5.penup()
箱5.速度(0)
方框5.颜色(“达克朗格”)
方框5.形状(“方形”)
框6.penup()
方框6.速度(0)
方框6.颜色(“达克朗格”)
方框6.形状(“方形”)
框1.setpos(random.randint(-250250),random.randint(-250250))
框2.setpos(random.randint(-250250),random.randint(-250250))
框3.setpos(random.randint(-250250),random.randint(-250250))
框4.setpos(random.randint(-250250),random.randint(-250250))
框5.setpos(random.randint(-250250),random.randint(-250250))
框6.setpos(random.randint(-250250),random.randint(-250250))
#玩家
玩家=乌龟。乌龟()
player.penup()
播放器颜色(“蓝色”)
玩家形状(“圆”)
#定义运动
def down():
玩家设置标题(270)
球员前锋(20)
def right():
播放器设置标题(0)
球员前锋(20)
def left():
游戏者.设置标题(180)
球员前锋(20)
def up():
玩家设置标题(90)
球员前锋(20)
#碰撞
def聚合(t1、t2):
d=math.sqrt(math.pow(t1.xcor()-t2.xcor(),2)+math.pow(t1.ycor()-t2.ycor(),2))
如果d<20:
返回真值
其他:
返回错误
如果是集合(播放器,框1):
框1.hideturtle()
框1N-1
如果是集合(播放器,框2):
框2.hideturtle()
box2n-1
如果是集合(播放器,第3框):
框3.hideturtle()
box3n-1
如果是集合(播放器,第4框):
框4.hideturtle()
box4n-1
如果是集合(播放器,第5框):
框5.hideturtle()
box5n-1
如果是集合(播放器,第6框):
框6.hideturtle()
box6n-1
#运动
乌龟,听着
乌龟。安基(向上,“w”)
乌龟。上键(向下,“s”)
乌龟。安基(右,“d”)
乌龟。安基(左,“a”)
#时间
def mainTime():
x=60
对于范围(60+1)内的i:
时间。睡眠(1)
打印(格式化时间(x))
x-=1
def格式化时间(x):
分钟=整数(x/60)
秒\u rem=int(x%60)
如果(秒/雷姆<10):
返回(str(分钟)+“:0”+str(秒)
其他:
返回(str(分钟)+“:”+str(秒)
如果x==0:
播放=错误
mainTime()
#得分
def分数():
分数=0
对于范围(60)内的s:
t=60
时间。睡眠(1)
t-=1
如果box1或box2或box3或box4或box5或box6==1:
分数=分数+(秒×100)
分数()
#游戏结束
如果播放==假:
打印(“游戏结束”)
重启=输入(“重启?是/否”)
如果重新启动==“y”:
os.system('python“C:\\Users\\lyons\\AppData\\Local\\Programs\\python\\Python37-32\\Projects\\failed to make game.py')

而且,当它过去工作时,盒子在玩家击中后不会消失。如何修复此问题?

您是否尝试将turtle.mainloop()或turtle.done()放在代码中的某个位置?我不熟悉turtle模块,但一般来说,启动某种类型的事件循环需要窗口模块,在尝试之前,您可以对mainloop()和done()调用进行更多的研究。您应该编写一个主循环,在其中调用所有函数,然后您可以更好地阅读代码。现在调用main_time()并一直运行到循环结束,因此您不需要注册任何用户输入,因为您卡在了该循环中。另一件事:使用main函数,您可以调用main()来运行程序,然后再次调用main()来重新启动程序,而不用输入控制台命令谢谢!