gpt4 book ai didi

python - 主函数中没有调用函数

转载 作者:行者123 更新时间:2023-12-05 01:22:38 24 4
gpt4 key购买 nike

我是 python 的新手,我正在尝试使用 Pygame 构建游戏。我一直遇到无法识别碰撞的问题。这是我试过的代码

import pygame
pygame.init()

WIDTH, HEIGHT = (900, 500)
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('Bong Pong')

FPS = 60
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
BORDER = pygame.Rect(WIDTH// 2 -5, 0, 10, HEIGHT)
VEL = 3
PLAYER_HEIGHT = 50
PLAYER_WIDTH = 15
BALL_HEIGHT = 15
BALL_WIDTH = 15


def draw_window(player_1, player_2, game_ball):
WIN.fill(WHITE)
pygame.draw.rect(WIN, BLACK, BORDER)
pygame.draw.rect(WIN, BLACK, player_1)
pygame.draw.rect(WIN, BLACK, player_2)
pygame.draw.rect(WIN, RED, game_ball)
pygame.display.update()


def player_1_movement(keys_pressed, player_1):
if keys_pressed[pygame.K_w] and player_1.y - VEL > 0:
player_1.y -= VEL
if keys_pressed [pygame.K_s] and player_1.y + PLAYER_HEIGHT + VEL < 500:
player_1.y += VEL

def player_2_movement(keys_pressed, player_2):
if keys_pressed[pygame.K_UP] and player_2.y - VEL > 0:
player_2.y -= VEL
if keys_pressed [pygame.K_DOWN] and player_2.y + PLAYER_HEIGHT + VEL < 500:
player_2.y += VEL


def player_collision(player_1, player_2, game_ball, ball_vel_x):
if game_ball.colliderect(player_1) or game_ball.colliderect(player_2):
ball_vel_x *= -1






def main():
clock = pygame.time.Clock()
run = True

player_1 = pygame.Rect(50, HEIGHT//2 - PLAYER_HEIGHT// 2, PLAYER_WIDTH, PLAYER_HEIGHT)
player_2 = pygame.Rect(850, HEIGHT//2 - PLAYER_HEIGHT// 2, PLAYER_WIDTH, PLAYER_HEIGHT)
game_ball = pygame.Rect(50 + PLAYER_WIDTH, HEIGHT//2 - BALL_HEIGHT// 2, BALL_WIDTH, BALL_HEIGHT)
ball_vel_y = 2
ball_vel_x = 2
while run:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.QUIT()

keys_pressed = pygame.key.get_pressed()
player_1_movement(keys_pressed, player_1)
player_2_movement(keys_pressed, player_2)
draw_window(player_1, player_2, game_ball)

if game_ball.y - BALL_HEIGHT - VEL <= 0 or game_ball.y + BALL_HEIGHT + VEL >= 500:
ball_vel_y *= -1

game_ball.y -= ball_vel_y


player_collision(player_1, player_2, game_ball, ball_vel_x)


game_ball.x += ball_vel_x


main()

if __name__ == '__main__':
main()

我也试过把

game_ball.x += ball_vel_x

在 player_collsion 函数中,但它没有正确地反转它。

我已经通过像这样将整个 player_collision 函数代码放在 main 函数中解决了这个问题


def main():
clock = pygame.time.Clock()
run = True

player_1 = pygame.Rect(50, HEIGHT//2 - PLAYER_HEIGHT// 2, PLAYER_WIDTH, PLAYER_HEIGHT)
player_2 = pygame.Rect(850, HEIGHT//2 - PLAYER_HEIGHT// 2, PLAYER_WIDTH, PLAYER_HEIGHT)
game_ball = pygame.Rect(50 + PLAYER_WIDTH, HEIGHT//2 - BALL_HEIGHT// 2, BALL_WIDTH, BALL_HEIGHT)
ball_vel_y = 2
ball_vel_x = 2
while run:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.QUIT()

keys_pressed = pygame.key.get_pressed()
player_1_movement(keys_pressed, player_1)
player_2_movement(keys_pressed, player_2)
draw_window(player_1, player_2, game_ball)

if game_ball.y - BALL_HEIGHT - VEL <= 0 or game_ball.y + BALL_HEIGHT + VEL >= 500:
ball_vel_y *= -1

game_ball.y -= ball_vel_y

if game_ball.colliderect(player_1) or game_ball.colliderect(player_2):
ball_vel_x *= -1


game_ball.x += ball_vel_x


main()

这完全符合我的要求我只是想澄清一下我对为什么如果只在主函数中调用而不是直接将其放入该函数将无法正常工作的理解。

最佳答案

Python 没有输入输出参数的概念。参数按值传递。如果您在 player_collision 函数中更改 ball_vel_x ,只有参数发生变化,但自变量保持不变。您必须从函数返回 ball_vel_x 的新值:

def player_collision(player_1, player_2, game_ball, ball_vel_x): 
if game_ball.colliderect(player_1) or game_ball.colliderect(player_2):
ball_vel_x *= -1
return ball_vel_x
def main():
# [...]

while run:
# [...]

ball_vel_x = player_collision(player_1, player_2, game_ball, ball_vel_x)

# [...]

另一种可能性是将速度存储在对象中(例如 gyame.math.Vector2)。变量存储对对象的引用,因此如果变量是函数调用的参数,您可以在函数中更改对象的属性:

def player_collision(player_1, player_2, game_ball, ball_vel): 
if game_ball.colliderect(player_1) or game_ball.colliderect(player_2):
ball_vel.x *= -1


def main():
# [...]

ball_vel = pygame.math.Vector2(2, 2)

while run:
# [...]

if game_ball.y - BALL_HEIGHT - VEL <= 0 or game_ball.y + BALL_HEIGHT + VEL >= 500:
ball_vel.y *= -1

game_ball.y -= ball_vel.y
player_collision(player_1, player_2, game_ball, ball_vel)
game_ball.x += ball_vel.x

关于python - 主函数中没有调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73626888/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com