英文:
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 > 0: #Up
PLAYER1.y -= VEL
if keys_pressed[pygame.K_s] and PLAYER1.y + PLAYER1.height < HEIGHT: #Down
PLAYER1.y += VEL
def player2_handle_movement(keys_pressed):
if keys_pressed[pygame.K_UP] and PLAYER2.y > 0: #Up
PLAYER2.y -= VEL
if keys_pressed[pygame.K_DOWN] and PLAYER2.y + PLAYER2.height < 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 <= 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 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_x
或DOT_speed_y
,只有在函数内部的参数变量会改变,而传递给函数的参数不会改变。你必须从函数中返回DOT_speed_x
和DOT_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 <= 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()
Note that it is absolutely not necessary that the function arguments and the functions parameter variables have the same name.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论