簡體   English   中英

我在做一個 Python 乒乓球比賽,比賽中的球不動怎么辦?

[英]I'm making a Python pong game and the ball in the game won't move what should I do?

我是 Python 編碼的新手,我正在使用 PyCharm 作為我的 Python 閱讀器我編寫了這個簡單的乒乓球游戲應該做什么的教程(它是嘗試過的海龜代碼,但不會移動 Pygame)就像改變球的速度一樣,但這也無濟於事,無論我把它放在什么數字上,球都不會移動。

'''乒乓球比賽'''

import turtle as t
playerAscore = 0
playerBscore = 0

window = t.Screen()
window.title('PONG GAME')
window.bgcolor('green')
window.setup(width=800, height=600)
window.tracer(0)


# creating left paddle
leftpaddle = t.Turtle()
leftpaddle.speed(0)
leftpaddle.shape('square')
leftpaddle.color('white')
leftpaddle.shapesize(stretch_wid=5, stretch_len=1)
leftpaddle.penup()
leftpaddle.goto(-350, 0)

# creating right paddle
rightpaddle = t.Turtle()
rightpaddle.speed(0)
rightpaddle.shape("square")
rightpaddle.color("white")
rightpaddle.shapesize(stretch_wid=5, stretch_len=1)
rightpaddle.penup()
rightpaddle.goto(350, 0)

# creating ball
ball = t.Turtle()
ball.speed(0)
ball.shape("circle")
ball.color("red")
ball.penup()
ball.goto(5, 5)
ballxdirection = 0.2
ballydirection = 0.2

# creating pen for scorecard update
pen = t.Turtle()
pen.speed(0)
pen.color("blue")
pen.penup()
pen.hideturtle()
pen.goto(0, 260)
pen.write("score", align="center", font=('Arial', 24, 'normal'))


# moving the left paddle
**def** leftpaddleup():
    y = leftpaddle.ycor()
    y = y+90
    leftpaddle.sety(y)

    # moving the left paddle
**def** leftpaddledown():
    y = leftpaddle.ycor()
    y = y - 90
    leftpaddle.sety(y)


# moving the right paddle
**def** rightpaddleup():
    y = rightpaddle.ycor()
    y = y+90
    rightpaddle.sety(y)

# moving the right paddle
**def** rightpaddledown():
    y = rightpaddle.ycor()
    y = y-90
    rightpaddle.sety(y)

# Assign keys to play
window.listen()
window.onkeypress(leftpaddleup, 'w')
window.onkeypress(leftpaddledown, 's')
window.onkeypress(rightpaddleup, 'Up')
window.onkeypress(rightpaddledown, 'Down')

**while True**:
    window.update()

    # moving the ball
ball.setx(ball.xcor()+ballxdirection)
ball.sety((ball.ycor()+ballydirection))

# settingup border
**if** ball.ycor()>290:
    ball.sety(290)
    ballydirection = ballydirection*-1

**if** ball.ycor() < -290:
        ball.sety(-290)
        ballydirection = ballydirection*1

**if** ball.xcor() > 390:
    ball.goto(0, 0)
    ballxdirection= ballxdirection
    playerAscore = playerAscore+1
    pen.clear()
    pen.write("player A :{}   player B :{}".format(playerAscore, playerBscore)), align =='center', font== ('Arial')

#Handling the Collisions
**if** (ball.xcor()>340)and(ball.xcor()<350)and(ball.ycor()<rightpaddle.ycor()+40 and ball.ycor()>rightpaddle.ycor()-40):
    ball.setx(340)
    ballxdirection=ballxdirection*-1

**if** (ball.xcor()<-340)and(ball.xcor()>-350)and(ball.ycor()<leftpaddle.ycor()+40 and ball.ycor() > leftpaddle.ycor() - 40):
    ball.setx(-340)
    ballxdirection = ballxdirection *-1

你的主循環只是

while True:
    window.update()

那里沒有任何東西在移動球。 如果后面的行應該是主循環的一部分,它們也應該縮進。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM