惩罚游戏如何加快元素

时间:2019-03-06 12:42:03

标签: python turtle-graphics

我已经创建了简单的罚分游戏,并且一切正常,但是我想对其进行一些改进。球门本身就移动,我想做的就是在得分时加快速度。我了解dx为负时,它必须为+ = 1;如果dx为正,则它必须相反,所以+ = -1。我考虑过dx在范围(-270,0,-270)中的for循环,而第二个用于正变量。我是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()

1 个答案:

答案 0 :(得分:0)

使用这样的程序,您尝试实时移动的对象越少越好。您的目标是必须同时移动三个部分。下面的主要解决方法是将目标定义为乌龟形状,因此目标只是需要操纵的一件。下面的第二个解决方法是用定时事件替换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()

加上许多其他小修正,以简化代码。