如何反映球离桨

How to reflect the ball off paddle

我第一次尝试 pygame,现在我已经非常熟悉 python 作为一种语言,并且我一直在尝试重新创建 Pong,就像使用 2 个球拍和一个球。我似乎无法弄清楚如何让球从桨上反射出来。我还没有寻找角度之类的东西,因为我自己无法弄清楚。

不过我想到的是获取一个坐标范围,就是球拍的x&y和x&y+宽高,如果小球进入这些,就简单的反映出来在边界处做。正如您在下面的代码中看到的那样,我尝试执行多个 if 语句,但我也尝试将其作为单个语句执行,但这不起作用。 None 我实际打印的调试打印,但是当我用打印测试坐标范围时,它们看起来很好 :D

我会把我的代码贴在这里,这样你们就可以 运行 我的游戏了。

非常感谢你们的帮助!

import pygame
pygame.init()
win = pygame.display.set_mode((500,500))
pygame.display.set_caption("First Game")

x = 50
y = 50
width = 10                                      #sets variables for the main paddle
height = 60
vel = 5

ballx = 250
bally = 250
radius = 5
direction = True                                #True is left, False is right                                                            
bvel = 4                                        #sets variables for the ball
angle = 0

coordxGT = 0
coordxLT = 0                                    #sets variables for the coordinate ranges of the first paddle for collision
coordyGT = 0
coordyLT = 0

def setCoords():
    coordxGT = x
    coordxLT = x + width
    coordyGT = y                                #This function updates the coords throughout the main loop
    coordyLT = y + height
    coordxLT += radius
    coordyLT += radius

run = True
while run == True:
    pygame.time.delay(20)
    for event in pygame.event.get():            #on quit, quit the game and dont throw up an error :)
        if event.type == pygame.QUIT:
            run = False
    setCoords()

    if direction == True:                       #Ball movement
        ballx -= bvel
    else:
        ballx += bvel

    if ballx<0:
        ballx += bvel
        direction = False
    elif bally<0:
        bally += bvel
    elif ballx>495:                             #if the ball hits an edge
        ballx -= bvel
        direction = True
    elif bally>495:
        bally -= bvel

    if ballx<coordxLT and ballx>coordxGT:
        print("S1")
        if bally<coordyLT and bally>coordyGT:                                   #THE PART I CANT FIGURE OUT. If the ball lands within these ranges of coordinates, reflect it and change its direction
            print("S2")
            if direction == True:
                print("YES")
                ballx += bvel
                direction = False

    keys = pygame.key.get_pressed()                         #gets the keys pressed at that current time

    if keys[pygame.K_DOWN]:
        bally += bvel                                       #Ball control (For debugging)
    if keys[pygame.K_UP]:
        bally -= bvel

    if keys[pygame.K_w]:
        y -= vel
    if keys[pygame.K_a]:
        x -= vel
    if keys[pygame.K_s]:                                    #Paddle controls
        y += vel
    if keys[pygame.K_d]:
        x += vel

    if x<0:
        x += vel
    if y<0:
        y += vel
    if x>80:
        x -= vel                                        #Stops the paddle from moving if it hits a boundary
    if y>440:
                                                        #440 because window height - height of cube
        y -= vel

    win.fill((0,0,0))
    pygame.draw.circle(win, (255, 255, 255), (ballx, bally), radius)      #refreshes the screen
    pygame.draw.rect(win,(255,255,255),(x, y, width, height))
    pygame.display.update()
pygame.quit()

您很接近,但是您错过了将变量 coordxGTcoordxLTcoordxLTcoordyLT 声明为 global

def setCoords():
    global coordxGT, coordxLT, coordxLT, coordyLT
    coordxGT = x
    coordxLT = x + width
    coordyGT = y  
    coordyLT = y + height
    coordxLT += radius
    coordyLT += radius

注意,如果要在函数中写入全局命名空间中的变量,则该变量已被解释为全局变量。否则将创建并设置函数范围内的新变量。参见 global statement