gpt4 book ai didi

python - 如何将球反射到 Racket 上

转载 作者:行者123 更新时间:2023-12-01 06:53:36 25 4
gpt4 key购买 nike

我第一次尝试 pygame,现在我已经非常熟悉 Python 作为一门语言,并且我一直在尝试重新创建 Pong,就像带有 2 个 Racket 和一个球的老式学校游戏一样。我似乎不知道如何让球从 Racket 上反射。我还没有在寻找角度之类的东西,因为我自己能够解决这个问题。

Buttt我想到的是得到一个坐标范围,即 Racket 的X和Y以及x和y+宽度和高度,如果球进入这些,它只是像在一个边界。我尝试过执行多个 if 语句,如下面的代码所示,但我也尝试过将其作为单个语句执行,但这不起作用。我没有实际打印任何调试打印,但是当我用 print 测试坐标范围时,它们看起来很好: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 声明为是全局

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

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

关于python - 如何将球反射到 Racket 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58898773/

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