我在制作乒乓球时遇到问题

问题描述 投票:0回答:1

我正在尝试用 python 制作乒乓球,但我不知道如何使图形或角色移动工作。 我尝试在谷歌上查找,但找不到相关来源,有人可以帮助我吗? 我是 python 编程新手。

python input graphics game-development physics
1个回答
0
投票

我认为制作 Pong 游戏的最佳方法是使用 Python Turtle。它的基本原理是这样的:

设置方法:

import turtle
t = turtle.Pen()   #Naming the turtle "t"

向前推进:

t.forward(number_of_pixels)

向左或向右转动:

t.left(number_of_degrees)
t.right(number_of_degrees)

写一个字

t.write(word)   #You can change the font and size with more parameters.

很多人都用乌龟做了乒乓球游戏,如此搜索所示。我见过的完整、有效的乒乓球游戏的最好例子是 cdlane。他们的代码是这样的:

from turtle import Turtle, Screen

FONT = ("Arial", 16, "normal")

def isCollision(t1, t2):
    return t1.distance(t2) < 15

# set up screen
screen = Screen()
screen.bgcolor("darkgreen")
screen.title("Pong")

# set up border
border_pen = Turtle(visible=False)
border_pen.speed('fastest')
border_pen.color('white')
border_pen.pensize(3)

border_pen.penup()
border_pen.setposition(-300, -300)
border_pen.pendown()

for _ in range(4):
    border_pen.forward(600)
    border_pen.left(90)

# set score to 0
score = 0

# set time to zero
seconds = 0

# Draw score
score_pen = Turtle(visible=False)
score_pen.color("white")
score_pen.penup()
score_pen.setposition(-290, 310)

score_pen.write("Score {}".format(score), False, align="left", font=FONT)

# Draw timer
time_pen = Turtle(visible=False)
time_pen.color("white")
time_pen.penup()
time_pen.setposition(260, 310)

time_pen.write("Time {}".format(int(seconds)), False, align="left", font=FONT)

# create the player turtle
player = Turtle("square", visible=False)
player.shapesize(0.5, 3)
player.speed('fastest')
player.setheading(90)
player.color("blue")
player.penup()

player.setposition(-280, -250)  # (x,y)
player.showturtle()

playerspeed = 15

# create the AIplayer turtle
AIplayer = Turtle("square", visible=False)
AIplayer.shapesize(0.5, 3)
AIplayer.speed('fastest')
AIplayer.setheading(90)
AIplayer.color("black")
AIplayer.penup()

AIplayer.setposition(280, 250)  # (x,y)
AIplayer.showturtle()

AIplayerspeed = 15

# create the pong
pong = Turtle("circle", visible=False)
pong.shapesize(0.5, 0.5)
pong.speed('fast')
pong.color("red")
pong.penup()

pong.sety(265)
pong.showturtle()

pongspeed = 15
pong_dx, pong_dy = 5, -5

# Move player up and down
def move_up():
    player.forward(playerspeed)

    y = player.ycor()

    if y > 265:
        y = 260
        player.sety(y)

    screen.update()

def move_down():
    player.backward(playerspeed)

    y = player.ycor()

    if y < -265:
        y = -260
        player.sety(y)

    screen.update()

# keyboard bindings
screen.onkey(move_up, "Up")
screen.onkey(move_down, "Down")

screen.listen()

# main game loop
def move():
    global pong_dx, pong_dy, AIplayerspeed, seconds, score

    # move pong ball
    x, y = pong.position()
    x += pong_dx
    y += pong_dy
    pong.setposition(x, y)

    if isCollision(pong, player): # collision pong and player
        pong_dx *= -1
        # Update the score
        score += 10
        score_pen.undo()
        score_pen.write("Score: {}".format(score), align="left", font=FONT)
    elif isCollision(pong, AIplayer):  # collision pong and AIplayer
        pong_dx *= -1
    elif y < -300 or y > 300:  # check for bounce and redirect it
        pong_dy *= -1
    elif x > 300:
        pong_dx *= -1
    elif x < -300:
        print("Game Over")
        screen.bye()
        return

    # move AI paddle (might speed up pong movement)
    AIplayer.forward(AIplayerspeed)

    y = AIplayer.ycor()

    if y < -250 or y > 250:
        AIplayerspeed *= -1

    # display timer
    seconds += 0.05
    time_pen.undo()
    time_pen.write("Time: {}".format(int(seconds)), False, align="Left", font=FONT)
    screen.ontimer(move, 50)

    screen.update()

screen.tracer(False)
move()
screen.mainloop()

您可以在此处找到完整答案。 注意:如果您想将代码归功于某人,请归功于 cdlane,而不是我。

© www.soinside.com 2019 - 2024. All rights reserved.