Python 3.x 我的海龟屏风一打开就关上了
我正在通过tkinter和turtle图形制作一个游戏。我的双人模式工作得很好,但是在单人模式下,屏幕一打开就会关闭。我尝试了exitonclick方法,但使用这种方法,我的乌龟不会移动。我面临的另一个问题是,我希望在图片中有一个标签来显示获奖者,所以我如何才能做到这一点Python 3.x 我的海龟屏风一打开就关上了,python-3.x,tkinter,turtle-graphics,Python 3.x,Tkinter,Turtle Graphics,我正在通过tkinter和turtle图形制作一个游戏。我的双人模式工作得很好,但是在单人模式下,屏幕一打开就会关闭。我尝试了exitonclick方法,但使用这种方法,我的乌龟不会移动。我面临的另一个问题是,我希望在图片中有一个标签来显示获奖者,所以我如何才能做到这一点 from tkinter import * from turtle import * #Single Player def single(): Name_1 = inputname.get().strip()
from tkinter import *
from turtle import *
#Single Player
def single():
Name_1 = inputname.get().strip()
opl.destroy()
windows = Screen()
windows.title("Bounce Game by TechLord")
windows.bgcolor("white")
windows.setup(width=1200, height=760)
windows.tracer(0)
# Score
score = 0
# paddle a
paddle = Turtle()
paddle.speed(0)
paddle.shape("square")
paddle.color("blue")
paddle.shapesize(stretch_wid=1, stretch_len=5)
paddle.penup()
paddle.goto(0, -330)
# ball
ball = Turtle()
ball.speed(0)
ball.shape("circle")
ball.color("red")
ball.shapesize(stretch_wid=1.2, stretch_len=1.2)
ball.penup()
ball.goto(0, 0)
ball.dx = 1
ball.dy = 1
# Pen
pen = Turtle()
pen.speed(0)
pen.color("black")
pen.penup()
pen.hideturtle()
pen.goto(0, 330)
pen.write("{}: 0".format(Name_1), align="center", font=("Bebas Neue", 24, "italic"))
# Function
def paddle_right():
x = paddle.xcor()
x += 20
paddle.setx(x)
def paddle_left():
x = paddle.xcor()
x -= 20
paddle.setx(x)
# keyboard binding
windows.listen()
windows.onkeypress(paddle_right, "Right")
windows.onkeypress(paddle_left, "Left")
# Main loop
while score < 10:
windows.update()
# moving ball
ball.setx(ball.xcor() + ball.dx)
ball.sety(ball.ycor() + ball.dy)
# border checking
# Top and Side borders
if ball.ycor() > 380:
ball.sety(380)
ball.dy *= -1
if ball.xcor() > 600:
ball.setx(600)
ball.dx *= -1
if ball.xcor() < -600:
ball.setx(-600)
ball.dx *= -1
# paddle and ball collisions
if ball.ycor() < -320 and ball.ycor() > -330 and (ball.xcor() < paddle.xcor() + 55 and ball.xcor() > paddle.xcor() - 55):
ball.setx(-330)
ball.dx *= -1
else:
score += 1
Screen().bye()
#Double Player
def double():
name_1 = inputname1.get().strip()
name_2 = inputname2.get().strip()
pl.destroy()
windows = Screen()
windows.title("Bounce Game by TechLord")
windows.bgcolor("white")
windows.setup(width=1200, height=760)
windows.tracer(0)
# Score
score_a = 0
score_b = 0
# paddle a
pad_a = Turtle()
pad_a.speed(0)
pad_a.shape("square")
pad_a.color("blue")
pad_a.shapesize(stretch_wid=5, stretch_len=1)
pad_a.penup()
pad_a.goto(-550, 0)
# paddle b
pad_b = Turtle()
pad_b.speed(0)
pad_b.shape("square")
pad_b.color("blue")
pad_b.shapesize(stretch_wid=5, stretch_len=1)
pad_b.penup()
pad_b.goto(550, 0)
# ball
ball = Turtle()
ball.speed(0)
ball.shape("circle")
ball.color("red")
ball.shapesize(stretch_wid=1.2, stretch_len=1.2)
ball.penup()
ball.goto(0, 0)
ball.dx = 1
ball.dy = -1
# Pen
pen = Turtle()
pen.speed(0)
pen.color("black")
pen.penup()
pen.hideturtle()
pen.goto(0, 330)
pen.write("{}: 0 {}: 0".format(name_1, name_2), align="center", font=("Bebas Neue", 24, "italic"))
# Function
def pad_a_up():
y = pad_a.ycor()
y += 20
pad_a.sety(y)
def pad_a_down():
y = pad_a.ycor()
y -= 20
pad_a.sety(y)
def pad_b_up():
y = pad_b.ycor()
y += 20
pad_b.sety(y)
def pad_b_down():
y = pad_b.ycor()
y -= 20
pad_b.sety(y)
# keyboard binding
windows.listen()
windows.onkeypress(pad_a_up, "w")
windows.onkeypress(pad_a_down, "s")
windows.onkeypress(pad_b_up, "Up")
windows.onkeypress(pad_b_down, "Down")
# Main loop
while score_a < 10 and score_b < 10:
windows.update()
# moving ball
ball.setx(ball.xcor() + ball.dx)
ball.sety(ball.ycor() + ball.dy)
# border checking
# Top and Bottom
if ball.ycor() > 370:
ball.sety(370)
ball.dy *= -1
if ball.ycor() < -370:
ball.sety(-370)
ball.dy *= -1
if ball.xcor() > 610:
ball.goto(0, 0)
ball.dx *= -1
score_a += 1
pen.clear()
pen.write("{}: {} {}: {}".format(name_1, score_a, name_2, score_b), align="center",
font=("Bebas Neue", 24, "italic"))
if ball.xcor() < -610:
ball.goto(0, 0)
ball.dx *= -1
score_b += 1
pen.clear()
pen.write("{}: {} {}: {}".format(name_1, score_a, name_2, score_b), align="center",
font=("Bebas Neue", 24, "italic"))
# paddle and ball collisions
if ball.xcor() > 540 and ball.xcor() < 550 and (
ball.ycor() < pad_b.ycor() + 55 and ball.ycor() > pad_b.ycor() - 55):
ball.setx(540)
ball.dx *= -1
if ball.xcor() < -540 and ball.xcor() > -550 and (
ball.ycor() < pad_a.ycor() + 55 and ball.ycor() > pad_a.ycor() - 55):
ball.setx(-540)
ball.dx *= -1
if score_a == 10:
res = Tk()
pic = PhotoImage(file=r"F:\hassan\winner.png")
picture = Label(res, image=pic).grid(row=0, column=0)
winner = Label(res, In=picture, text=name_1, justify=CENTER).pack(side=BOTTOM)
elif score_b == 10:
res = Tk()
pic = PhotoImage(file=r"F:\hassan\winner.png")
picture = Label(res, image=pic).grid(row=0, column=0)
winner = Label(res, In=picture, text=name_2, justify=CENTER).pack(side=BOTTOM)
Screen().bye()
def twoplayer():
global inputname1
global inputname2
global pl
win.destroy()
pl = Tk()
pl.title("Name Entry")
inputname1 = StringVar()
inputname2 = StringVar()
pl1 = Label(pl, text="Player 1", fg="black", font=("arial", 20), justify=LEFT).grid(row=0, column=0, padx=10, pady=10)
pl2 = Label(pl, text="Player 2", fg="black", font=("arial", 20), justify=LEFT).grid(row=1, column=0, padx=10, pady=10)
name1 = Entry(pl, width=20, font=("bebas neue", 20, "italic"), bg="white", textvariable=inputname1, justify=LEFT)
name1.grid(row=0, column=1, padx=5, pady=10)
name2 = Entry(pl, width=20, font=("bebas neue", 20, "italic"), bg="white", textvariable=inputname2, justify=LEFT)
name2.grid(row=1, column=1, padx=5, pady=10)
start = Button(pl, text="start", fg="white", bg="black", bd=3, font=("bebas neue", 20), cursor="hand2", width=8, height=1, command=double).grid(row=2, column=1, padx=10, pady=10)
pl.mainloop()
def oneplayer():
global inputname
global opl
win.destroy()
opl = Tk()
opl.title("Name Entry")
inputname = StringVar()
pl1 = Label(opl, text="Player 1", fg="black", font=("arial", 20), justify=LEFT).grid(row=0, column=0, padx=10, pady=10)
name1 = Entry(opl, width=20, font=("bebas neue", 20, "italic"), bg="white", textvariable=inputname, justify=LEFT)
name1.grid(row=0, column=1, padx=5, pady=10)
start = Button(opl, text="start", fg="white", bg="black", bd=3, font=("bebas neue", 20), cursor="hand2", width=8, height=1, command=single).grid(row=1, column=1, padx=10, pady=10)
opl.mainloop()
#Defining game start menu
win = Tk()
win.title("Start Menu")
win.geometry("350x500")
img = PhotoImage(file=r"F:\hassan\First year\Programming Fundamentals(PL)\bounce.png")
image = Label(win, image=img).grid(row=0, column=0)
name = Label(win, text="BOUNCE!!", fg="red", font=("arial black", 30, "italic")).grid()
byline = Label(win, text="By TechLord", fg="brown", font=("arial black", 10, "italic")).grid()
one_player = Button(win, text="1 Player", fg="black", bg="blue", font=("calibri", 20, "bold"), cursor="hand2", command=oneplayer).grid(row=3, column=0, rowspan=1, columnspan=1, padx=10, pady=10)
two_player = Button(win, text="2 Player", fg="black", bg="turquoise", font=("calibri", 20, "bold"), cursor="hand2", command=twoplayer).grid(row=4, column=0, rowspan=1, columnspan=1, padx=10, pady=10)
win.mainloop()
从tkinter导入*
从海龟进口*
#单人
def single():
Name_1=inputname.get().strip()
opl.destroy()
windows=屏幕()
标题(“TechLord的弹跳游戏”)
windows.bgcolor(“白色”)
windows.setup(宽度=1200,高度=760)
windows.tracer(0)
#得分
分数=0
#划桨
桨=乌龟()
划桨速度(0)
桨形(“方形”)
桨。颜色(“蓝色”)
桨叶形状尺寸(拉伸宽度=1,拉伸长度=5)
划桨
桨。后藤(0,-330)
#球
球=乌龟()
球速(0)
球形(“圆”)
球。颜色(“红色”)
球形尺寸(拉伸宽度=1.2,拉伸长度=1.2)
ball.penup()
球。转到(0,0)
ball.dx=1
ball.dy=1
#笔
笔=乌龟()
笔速(0)
钢笔颜色(“黑色”)
pen.penup()
pen.hideturtle()
后藤(0330)
pen.write(“{}:0.format(Name_1),align=“center”,font=(“Bebas Neue”,24,“italic”))
#作用
def挡板右()
x=桨.xcor()
x+=20
桨组x(x)
def挡板左()
x=桨.xcor()
x-=20
桨组x(x)
#键盘装订
windows.listen()
windows.onkeypress(右划“右”)
windows.onkeypress(拨动左键,“左键”)
#主回路
分数<10分时:
windows.update()
#移动球
ball.setx(ball.xcor()+ball.dx)
ball.sety(ball.ycor()+ball.dy)
#边境检查
#顶边和侧边
如果ball.ycor()大于380:
鲍尔·塞蒂(380)
ball.dy*=-1
如果ball.xcor()大于600:
球.setx(600)
ball.dx*=-1
如果ball.xcor()小于-600:
球。setx(-600)
ball.dx*=-1
#桨球碰撞
如果ball.ycor()<-320和ball.ycor()>-330和(ball.xcor()blade.xcor()-55):
球。setx(-330)
ball.dx*=-1
其他:
分数+=1
Screen().bye()
#双人
def double():
name_1=inputname1.get().strip()
name_2=inputname2.get().strip()
pl.销毁
windows=屏幕()
标题(“TechLord的弹跳游戏”)
windows.bgcolor(“白色”)
windows.setup(宽度=1200,高度=760)
windows.tracer(0)
#得分
得分a=0
得分b=0
#划桨
pad_a=海龟()
焊盘a.速度(0)
垫块形状(“方形”)
衬垫颜色(“蓝色”)
焊盘形状尺寸(拉伸宽度=5,拉伸长度=1)
pad_a.penup()
pad_a.goto(-550,0)
#桨b
pad_b=海龟()
垫块速度(0)
垫块形状(“方形”)
衬垫颜色(“蓝色”)
焊盘形状尺寸(拉伸宽度=5,拉伸长度=1)
pad_b.penup()
后藤岛(550,0)
#球
球=乌龟()
球速(0)
球形(“圆”)
球。颜色(“红色”)
球形尺寸(拉伸宽度=1.2,拉伸长度=1.2)
ball.penup()
球。转到(0,0)
ball.dx=1
ball.dy=-1
#笔
笔=乌龟()
笔速(0)
钢笔颜色(“黑色”)
pen.penup()
pen.hideturtle()
后藤(0330)
pen.write(“{}:0{}:0.”格式(name_1,name_2),align=“center”,font=(“Bebas Neue”,24,“italic”))
#作用
def pad_a_up():
y=pad_a.ycor()
y+=20
垫块a.sety(y)
def pad_a_down():
y=pad_a.ycor()
y-=20
垫块a.sety(y)
def pad_b_up():
y=pad_b.ycor()
y+=20
垫块设置(y)
def pad_b_down():
y=pad_b.ycor()
y-=20
垫块设置(y)
#键盘装订
windows.listen()
windows.on按键(键盘a向上,“w”)
windows.on按键(键盘向下“s”)
windows.on按键(键盘向上,“向上”)
windows.on按键(键盘向下,“向下”)
#主回路
当a分<10分和b分<10分时:
windows.update()
#移动球
ball.setx(ball.xcor()+ball.dx)
ball.sety(ball.ycor()+ball.dy)
#边境检查
#上下
如果ball.ycor()大于370:
鲍尔·塞蒂(370)
ball.dy*=-1
如果ball.ycor()小于-370:
球。赛蒂(-370)
ball.dy*=-1
如果ball.xcor()大于610:
球。转到(0,0)
ball.dx*=-1
得分a+=1
pen.clear()
pen.write(“{}:{}:{}.”格式(name_1,score_a,name_2,score_b),align=“center”,
字体=(“贝巴斯新”,24,“斜体”))
如果ball.xcor()小于-610:
球。转到(0,0)
ball.dx*=-1
得分b+=1
pen.clear()
pen.write(“{}:{}:{}.”格式(name_1,score_a,name_2,score_b),align=“center”,
字体=(“贝巴斯新”,24,“斜体”))
#桨球碰撞
如果ball.xcor()大于540且ball.xcor()小于550且(
ball.ycor()pad_b.ycor()-55):
球。setx(540)
ball.dx*=-1
如果ball.xcor()<-540和ball.xcor()>-550和(
ball.ycor()pad_a.ycor()-55):
球。setx(-540)
ball.dx*=-1
如果得分a==10:
res=Tk()
pic=PhotoImage(文件=r“F:\hassan\winner.png”)
图片=标签(分辨率,图像=图片)。网格(行=0,列=0)
赢家=标签(分辨率,In=图片,text=名称,justify=中间)。包装(侧面=底部)
elif得分_b==10:
res=Tk()
pic=PhotoImage(文件=r“F:\hassan\winner.png”)
图片=标签(分辨率,图像=图片)。网格(行=0,列=0)
赢家=标签(分辨率,In=图片,text=名称,justify=中间)。包装(侧面=底部)
Screen().bye()
def twoplayer():
全局输入名称1
全局输入名称2
全球损益
战胜
pl=Tk()
pl.title(“名称条目”)
inputname1=StringVar()
inputname2=StringVar()
pl1=标签(pl,text=“Player 1”,fg=“black”,font=(“arial”,20),justify=左)。网格(行=0,列=0,padx=10,pady=10)
pl2=标签(pl,text=“Player 2”,fg=“black”,font=(“arial”,20),justify=左)。网格(行=1,列=0,padx=10,pady=10)
name1=条目(pl,width=20,font=(“bebas neue”,20,“italic”),bg=“white”,textvariable=inputname1,justify=LEFT)
名称1.网格(行=0,列=1,padx=5,pady=10)
名称2=入口(pl,宽度=20,