乒乓球游戏,但球出了窗户

huangapple go评论69阅读模式
英文:

Pong game, but the ball goes out of the window

问题

我编写了一个乒乓球游戏,但是球会飞出窗口。

import pygame
pygame.init()
WIDTH, HEIGHT = 900, 500
WIN = pygame.display.set_mode((WIDTH, HEIGHT))


#颜色
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)


#游戏变量
VEL = 5
FPS = 60
CENTER = (WIDTH/2, HEIGHT/2)
DOTRAD = 10
DOT_speed_x = 2
DOT_speed_y = 0


DOT = pygame.Rect(CENTER[0], CENTER[1], DOTRAD, DOTRAD)

#玩家
PLAYER1 = pygame.Rect(50, HEIGHT/2-75, 10, 150)
PLAYER2 = pygame.Rect(850, HEIGHT/2-75, 10, 150)



def draw_window():
    WIN.fill(WHITE)
    pygame.draw.rect(WIN, BLACK, PLAYER1)
    pygame.draw.rect(WIN, BLACK, PLAYER2)
    pygame.draw.circle(WIN, BLACK, DOT[0:2], DOTRAD)
    pygame.display.update()


def player1_handle_movement(keys_pressed):
    if keys_pressed[pygame.K_w] and PLAYER1.y > 0:      #上移
        PLAYER1.y -= VEL
    if keys_pressed[pygame.K_s] and PLAYER1.y + PLAYER1.height < HEIGHT:    #下移
        PLAYER1.y += VEL

def player2_handle_movement(keys_pressed):
    if keys_pressed[pygame.K_UP] and PLAYER2.y > 0:      #上移
        PLAYER2.y -= VEL
    if keys_pressed[pygame.K_DOWN] and PLAYER2.y + PLAYER2.height < HEIGHT:    #下移
        PLAYER2.y += VEL

def DOT_handle_movement(DOT_speed_x, DOT_speed_y):
    DOT.x += DOT_speed_x
    DOT.y += DOT_speed_y
    
    if DOT.top <= 0 or DOT.bottom >= HEIGHT:
        DOT_speed_y *= -1
    
    if DOT.left <= 0 or DOT.right >= WIDTH:
        DOT_speed_x *= -1
    
    print(DOT_speed_x, DOT_speed_y)
        

def main():
    clock = pygame.time.Clock()
    run = True
    while run:
        clock.tick(FPS)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False        


        
        keys_pressed = pygame.key.get_pressed()
        player1_handle_movement(keys_pressed)
        player2_handle_movement(keys_pressed)
        DOT_handle_movement(DOT_speed_x, DOT_speed_y)
        
        draw_window()
    pygame.quit()
    
if __name__ == "__main__":
    main()
英文:

I coded a Pong game but the ball goes out of the window.

import pygame
pygame.init()
WIDTH, HEIGHT = 900, 500
WIN = pygame.display.set_mode((WIDTH, HEIGHT))


#Colors
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLACK = (0, 0, 0)


#Game Variables
VEL = 5
FPS = 60
CENTER = (WIDTH/2, HEIGHT/2)
DOTRAD = 10
DOT_speed_x = 2
DOT_speed_y = 0



DOT = pygame.Rect(CENTER[0], CENTER[1], DOTRAD, DOTRAD)

#Player
PLAYER1 = pygame.Rect(50, HEIGHT/2-75, 10, 150)
PLAYER2 = pygame.Rect(850, HEIGHT/2-75, 10, 150)



def draw_window():
    WIN.fill(WHITE)
    pygame.draw.rect(WIN, BLACK, PLAYER1)
    pygame.draw.rect(WIN, BLACK, PLAYER2)
    pygame.draw.circle(WIN, BLACK, DOT[0:2], DOTRAD)
    pygame.display.update()


def player1_handle_movement(keys_pressed):
    if keys_pressed[pygame.K_w] and PLAYER1.y &gt; 0:      #Up
        PLAYER1.y -= VEL
    if keys_pressed[pygame.K_s] and PLAYER1.y + PLAYER1.height &lt; HEIGHT:    #Down
        PLAYER1.y += VEL

def player2_handle_movement(keys_pressed):
    if keys_pressed[pygame.K_UP] and PLAYER2.y &gt; 0:      #Up
        PLAYER2.y -= VEL
    if keys_pressed[pygame.K_DOWN] and PLAYER2.y + PLAYER2.height &lt; HEIGHT:    #Down
        PLAYER2.y += VEL

def DOT_handle_movement (DOT_speed_x, DOT_speed_y):
    DOT.x += DOT_speed_x
    DOT.y += DOT_speed_y
    
    if DOT.top &lt;= 0 or DOT.bottom &gt;= HEIGHT:
        DOT_speed_y *= -1
    
    if DOT.left &lt;= 0 or DOT.right &gt;= WIDTH:
        DOT_speed_x *= -1
    

    
    print(DOT_speed_x, DOT_speed_y)
        

def main():
    clock = pygame.time.Clock()
    run = True
    while run:
        clock.tick(FPS)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False        


        
        keys_pressed = pygame.key.get_pressed()
        player1_handle_movement(keys_pressed)
        player2_handle_movement(keys_pressed)
        DOT_handle_movement(DOT_speed_x, DOT_speed_y)
        
        draw_window()
    pygame.quit()
    
if __name__ == &quot;__main__&quot;:
    main()

I print the DOT_speed_x and DOT_speed_y to see if it works that the ball detects that its out of the window.
Yes it does.
But its not changing the direction.
I saw so many tutorials that say that this method should work fine but for me it doesn't.

答案1

得分: 0

在Python中,没有输入输出参数的概念。如果你改变DOT_speed_xDOT_speed_y,只有在函数内部的参数变量会改变,而传递给函数的参数不会改变。你必须从函数中返回DOT_speed_xDOT_speed_y的新值。

def DOT_handle_movement(DOT_speed_x, DOT_speed_y):
    DOT.x += DOT_speed_x
    DOT.y += DOT_speed_y
    
    if DOT.top <= 0 or DOT.bottom >= HEIGHT:
        DOT_speed_y *= -1
    
    if DOT.left <= 0 or DOT.right >= WIDTH:
        DOT_speed_x *= -1
     
    return DOT_speed_x, DOT_speed_y
def main():
    DOT_speed_x = 2
    DOT_speed_y = 0

    clock = pygame.time.Clock()
    run = True
    while run:
        clock.tick(FPS)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False        

        keys_pressed = pygame.key.get_pressed()
        player1_handle_movement(keys_pressed)
        player2_handle_movement(keys_pressed)
        
        DOT_speed_x, DOT_speed_y = DOT_handle_movement(DOT_speed_x, DOT_speed_y)
        
        draw_window()
    pygame.quit()
    

请注意,函数的参数和函数内部的参数变量没有必须具有相同的名称。

英文:

In Python, there is no concept of in-out parameters. If you change DOT_speed_x or DOT_speed_y, only the parameter variable that is local to the function changes, but not the arguments that are passed to the function. You have to return the new values of DOT_speed_x and DOT_speed_y from the function.

def DOT_handle_movement(DOT_speed_x, DOT_speed_y):
    DOT.x += DOT_speed_x
    DOT.y += DOT_speed_y
    
    if DOT.top &lt;= 0 or DOT.bottom &gt;= HEIGHT:
        DOT_speed_y *= -1
    
    if DOT.left &lt;= 0 or DOT.right &gt;= WIDTH:
        DOT_speed_x *= -1
     
    return DOT_speed_x, DOT_speed_y
def main():
    DOT_speed_x = 2
    DOT_speed_y = 0

    clock = pygame.time.Clock()
    run = True
    while run:
        clock.tick(FPS)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False        

        keys_pressed = pygame.key.get_pressed()
        player1_handle_movement(keys_pressed)
        player2_handle_movement(keys_pressed)
        
        DOT_speed_x, DOT_speed_y = DOT_handle_movement(DOT_speed_x, DOT_speed_y)
        
        draw_window()
    pygame.quit()
    

Note that it is absolutely not necessary that the function arguments and the functions parameter variables have the same name.

huangapple
  • 本文由 发表于 2023年2月7日 03:44:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75365871.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定