python - 惩罚游戏如何加速元素
python - penalty game how to speed up element
我创建了简单的惩罚游戏并且一切正常,但我想稍微改进一下。目标本身移动,我想做的是在得分时加快速度。我知道当 dx 为负时它必须为 += 1,如果 dx 为正则它必须相反所以 += -1。我考虑了 for loop for dx in range(-270, 0, -270) 和第二个用于正变量的循环。我是 python 和编程本身的初学者,所以我很感激任何建议。我想加快 SL、SP 和 P。这些对象创建了目标。
import turtle
import time
sd = 0.1
wn = turtle.Screen()
wn.bgcolor("black")
wn.title("karne")
wn.setup(width=800, height=600)
wn.tracer(0)
#pilka
ball = turtle.Turtle()
ball.shape("circle")
ball.color("green")
ball.speed(0)
ball.penup()
ball.goto(0, -275)
ball.direction = "stop"
score = 0
miss = 0
#scoring
pen = turtle.Turtle()
pen.speed(0)
pen.shape("square")
pen.color("white")
pen.penup()
pen.hideturtle()
pen.goto(0, 0)
#slupek lewy
sl = turtle.Turtle()
sl.shape("square")
sl.color("white")
sl.shapesize(stretch_wid=3, stretch_len=1)
sl.speed(0)
sl.penup()
sl.goto(-80, 270)
sl.dx = sd
#slupek prawy
sp = turtle.Turtle()
sp.shape("square")
sp.color("white")
sp.shapesize(stretch_wid=3, stretch_len=1)
sp.speed(0)
sp.penup()
sp.goto(80, 270)
sp.dx = sd
#poprzeczka
p = turtle.Turtle()
p.shape("square")
p.color("white")
p.shapesize(stretch_wid=7, stretch_len=1)
p.speed(0)
p.seth(90)
p.penup()
p.goto(0, 290)
p.dx = sd
score = 0
miss = 0
#function
def right():
x = ball.xcor()
x +=20
ball.setx(x)
def left():
x = ball.xcor()
x -=20
ball.setx(x)
def shoot():
ball.direction = "up"
def shoot2():
ball.direction = "stop"
def shoot1():
if ball.direction == "up":
y = ball.ycor()
ball.sety(y+0.5)
#binds
wn.listen()
wn.onkeypress(right, "d")
wn.onkeypress(left, "a")
wn.onkeypress(shoot, "space")
while True:
#goal moving
sl.setx(sl.xcor() + sl.dx)
sp.setx(sp.xcor() + sp.dx)
p.setx(p.xcor() + p.dx)
#goal borders check
if sl.xcor() > 250:
sl.setx(250)
sl.dx *= -1
if sl.xcor() < -390:
sl.setx(-390)
sl.dx *= -1
if sp.xcor() > 390:
sp.setx(390)
sp.dx *= -1
if sp.xcor() < -250:
sp.setx(-250)
sp.dx *= -1
if p.xcor() > 320:
p.setx(320)
p.dx *= -1
if p.xcor() < -320:
p.setx(-320)
p.dx *= -1
#ball and goal check
if (ball.ycor() > 270 and ball.ycor() < 280) and (ball.xcor() < p.xcor() + 50 and ball.xcor() > p.xcor() -40):
score += 1
pen.clear()
pen.write("Score:{} Miss:{} ".format(score, miss), align="center", font=("Courier", 24, "normal"))
shoot2()
ball.goto(0, -275)
if ball.ycor() > 295:
miss += 1
ball.goto(0, -275)
score = 0
pen.clear()
pen.write("Score:{} Miss:{} ".format(score, miss), align="center", font=("Courier", 24, "normal"))
shoot2()
shoot1()
wn.update()
使用这样的程序,您尝试实时移动的对象越少越好。您的目标是必须同时移动三块棋子。下面的主要修复是将目标定义为乌龟形状,因此目标只是一个需要操作的部分。下面的第二个修复是用定时事件替换 while True:
,它在事件驱动的世界中没有位置:
from turtle import Screen, Turtle
sd = 1.0
FONT = ("Courier", 24, "normal")
def right():
ball.forward(20)
def left():
ball.backward(20)
def shoot():
ball.direction = "up"
def shoot2():
ball.direction = "stop"
def shoot1():
if ball.direction == "up":
ball.sety(ball.ycor() + sd * 2)
wn = Screen()
wn.bgcolor("black")
wn.title("karne")
wn.setup(width=800, height=600)
wn.tracer(False)
wn.register_shape("goal", ((-90, 30), (90, 30), (90, -30), (70, -30), (70, 10), (-70, 10), (-70, -30), (-90, -30)))
# pilka
ball = Turtle("circle")
ball.color("green")
ball.speed('fastest')
ball.penup()
ball.sety(-275)
ball.direction = "stop"
# scoring
pen = Turtle(visible=False)
pen.speed('fastest')
pen.color("white")
pen.penup()
# poprzeczka
p = Turtle("goal")
p.color("white")
p.speed('fastest')
p.seth(90)
p.penup()
p.sety(270)
p.dx = sd
score = 0
miss = 0
# binds
wn.onkeypress(right, "d")
wn.onkeypress(left, "a")
wn.onkeypress(shoot, "space")
wn.listen()
def move():
global score, miss
# goal moving
p.setx(p.xcor() + p.dx)
# goal borders check
if p.xcor() > 330:
p.setx(330)
p.dx *= -1
elif p.xcor() < -330:
p.setx(-330)
p.dx *= -1
# ball and goal check
if 270 < ball.ycor() < 280 and ball.distance(p) < 50:
score += 1
pen.clear()
pen.write("Score:{} Miss:{} ".format(score, miss), align="center", font=FONT)
shoot2()
ball.goto(0, -275)
elif ball.ycor() > 295:
miss += 1
score = 0
pen.clear()
pen.write("Score:{} Miss:{} ".format(score, miss), align="center", font=FONT)
shoot2()
ball.goto(0, -275)
shoot1()
wn.update()
wn.ontimer(move, 25)
move()
wn.tracer(True)
wn.mainloop()
还有许多其他小修复来简化代码。
我创建了简单的惩罚游戏并且一切正常,但我想稍微改进一下。目标本身移动,我想做的是在得分时加快速度。我知道当 dx 为负时它必须为 += 1,如果 dx 为正则它必须相反所以 += -1。我考虑了 for loop for dx in range(-270, 0, -270) 和第二个用于正变量的循环。我是 python 和编程本身的初学者,所以我很感激任何建议。我想加快 SL、SP 和 P。这些对象创建了目标。
import turtle
import time
sd = 0.1
wn = turtle.Screen()
wn.bgcolor("black")
wn.title("karne")
wn.setup(width=800, height=600)
wn.tracer(0)
#pilka
ball = turtle.Turtle()
ball.shape("circle")
ball.color("green")
ball.speed(0)
ball.penup()
ball.goto(0, -275)
ball.direction = "stop"
score = 0
miss = 0
#scoring
pen = turtle.Turtle()
pen.speed(0)
pen.shape("square")
pen.color("white")
pen.penup()
pen.hideturtle()
pen.goto(0, 0)
#slupek lewy
sl = turtle.Turtle()
sl.shape("square")
sl.color("white")
sl.shapesize(stretch_wid=3, stretch_len=1)
sl.speed(0)
sl.penup()
sl.goto(-80, 270)
sl.dx = sd
#slupek prawy
sp = turtle.Turtle()
sp.shape("square")
sp.color("white")
sp.shapesize(stretch_wid=3, stretch_len=1)
sp.speed(0)
sp.penup()
sp.goto(80, 270)
sp.dx = sd
#poprzeczka
p = turtle.Turtle()
p.shape("square")
p.color("white")
p.shapesize(stretch_wid=7, stretch_len=1)
p.speed(0)
p.seth(90)
p.penup()
p.goto(0, 290)
p.dx = sd
score = 0
miss = 0
#function
def right():
x = ball.xcor()
x +=20
ball.setx(x)
def left():
x = ball.xcor()
x -=20
ball.setx(x)
def shoot():
ball.direction = "up"
def shoot2():
ball.direction = "stop"
def shoot1():
if ball.direction == "up":
y = ball.ycor()
ball.sety(y+0.5)
#binds
wn.listen()
wn.onkeypress(right, "d")
wn.onkeypress(left, "a")
wn.onkeypress(shoot, "space")
while True:
#goal moving
sl.setx(sl.xcor() + sl.dx)
sp.setx(sp.xcor() + sp.dx)
p.setx(p.xcor() + p.dx)
#goal borders check
if sl.xcor() > 250:
sl.setx(250)
sl.dx *= -1
if sl.xcor() < -390:
sl.setx(-390)
sl.dx *= -1
if sp.xcor() > 390:
sp.setx(390)
sp.dx *= -1
if sp.xcor() < -250:
sp.setx(-250)
sp.dx *= -1
if p.xcor() > 320:
p.setx(320)
p.dx *= -1
if p.xcor() < -320:
p.setx(-320)
p.dx *= -1
#ball and goal check
if (ball.ycor() > 270 and ball.ycor() < 280) and (ball.xcor() < p.xcor() + 50 and ball.xcor() > p.xcor() -40):
score += 1
pen.clear()
pen.write("Score:{} Miss:{} ".format(score, miss), align="center", font=("Courier", 24, "normal"))
shoot2()
ball.goto(0, -275)
if ball.ycor() > 295:
miss += 1
ball.goto(0, -275)
score = 0
pen.clear()
pen.write("Score:{} Miss:{} ".format(score, miss), align="center", font=("Courier", 24, "normal"))
shoot2()
shoot1()
wn.update()
使用这样的程序,您尝试实时移动的对象越少越好。您的目标是必须同时移动三块棋子。下面的主要修复是将目标定义为乌龟形状,因此目标只是一个需要操作的部分。下面的第二个修复是用定时事件替换 while True:
,它在事件驱动的世界中没有位置:
from turtle import Screen, Turtle
sd = 1.0
FONT = ("Courier", 24, "normal")
def right():
ball.forward(20)
def left():
ball.backward(20)
def shoot():
ball.direction = "up"
def shoot2():
ball.direction = "stop"
def shoot1():
if ball.direction == "up":
ball.sety(ball.ycor() + sd * 2)
wn = Screen()
wn.bgcolor("black")
wn.title("karne")
wn.setup(width=800, height=600)
wn.tracer(False)
wn.register_shape("goal", ((-90, 30), (90, 30), (90, -30), (70, -30), (70, 10), (-70, 10), (-70, -30), (-90, -30)))
# pilka
ball = Turtle("circle")
ball.color("green")
ball.speed('fastest')
ball.penup()
ball.sety(-275)
ball.direction = "stop"
# scoring
pen = Turtle(visible=False)
pen.speed('fastest')
pen.color("white")
pen.penup()
# poprzeczka
p = Turtle("goal")
p.color("white")
p.speed('fastest')
p.seth(90)
p.penup()
p.sety(270)
p.dx = sd
score = 0
miss = 0
# binds
wn.onkeypress(right, "d")
wn.onkeypress(left, "a")
wn.onkeypress(shoot, "space")
wn.listen()
def move():
global score, miss
# goal moving
p.setx(p.xcor() + p.dx)
# goal borders check
if p.xcor() > 330:
p.setx(330)
p.dx *= -1
elif p.xcor() < -330:
p.setx(-330)
p.dx *= -1
# ball and goal check
if 270 < ball.ycor() < 280 and ball.distance(p) < 50:
score += 1
pen.clear()
pen.write("Score:{} Miss:{} ".format(score, miss), align="center", font=FONT)
shoot2()
ball.goto(0, -275)
elif ball.ycor() > 295:
miss += 1
score = 0
pen.clear()
pen.write("Score:{} Miss:{} ".format(score, miss), align="center", font=FONT)
shoot2()
ball.goto(0, -275)
shoot1()
wn.update()
wn.ontimer(move, 25)
move()
wn.tracer(True)
wn.mainloop()
还有许多其他小修复来简化代码。