gpt4 book ai didi

python - 如何让球像打乒乓球一样保持移动?

转载 作者:行者123 更新时间:2023-12-01 07:41:06 26 4
gpt4 key购买 nike

我正在创建一款网球游戏,类似于乒乓球,但更类似于真正的网球(即控制击球方向和类型的能力)。我想我已经完成了与球的碰撞检测,并且玩家已经弄清楚了,但是当玩家与球碰撞时,它移动了几个像素然后停止。我需要它像乒乓球一样继续前进。不确定问题是否出在碰撞检测上,或者只是代码中的其他问题。

import pygame

pygame.init()

# Define some colors
BLACK = (0, 0, 0)
OUT = (193, 58, 34)
COURT = (69, 150, 81)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
SKIN = (232, 214, 162)

ballspeed = 2

# Create the screen
windowsize = (700, 500)
screen = pygame.display.set_mode(windowsize)
pygame.display.set_caption('Tennis')

# Player Sprites
class Player(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("Robert_tennis.png")
self.rect = self.image.get_rect()
self.rect.center = (360, 480)
self.speedx = 0
self.speedy = 0

def update(self):
self.speedx = 0
self.speedy = 0
keystate = pygame.key.get_pressed()
if keystate[pygame.K_LEFT]:
self.speedx = -4
if keystate[pygame.K_RIGHT]:
self.speedx = 4
self.rect.x += self.speedx
if self.rect.right > 700:
self.rect.right = 700
if self.rect.right < 0:
self.rect.left = 0
if keystate[pygame.K_UP]:
self.speedy = -5
if keystate[pygame.K_DOWN]:
self.speedy = 3
self.rect.y += self.speedy
if self.rect.y < 235:
self.rect.y = 235
if self.rect.y < 0:
self.rect.y = 0

class Ball(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("tennisball.png")
self.rect = self.image.get_rect()
self.rect.center = (360, 325)

def update(self):
if tennisball.rect.colliderect(robert):
self.rect.y -= 2

#Add myself
all_sprites = pygame.sprite.Group()
robert = Player()
tennisball = Ball()
all_sprites.add(robert)
all_sprites.add(tennisball)

carryOn = True
clock = pygame.time.Clock()

while carryOn:
for event in pygame.event.get():
if event.type == pygame.QUIT:
carryOn = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_x:
carryOn = False

all_sprites.update()

# Fill the screen with a sky color
screen.fill(OUT)

# Draw the court
pygame.draw.rect(screen, COURT, [175, 0, 350, 500])
pygame.draw.line(screen, WHITE, (170,500), (170,0), 10)
pygame.draw.line(screen, WHITE, (520,500), (520,0), 10)
pygame.draw.line(screen, WHITE, (170,130), (520,130), 10)
pygame.draw.line(screen, WHITE, (170,370), (520,370), 10)
pygame.draw.line(screen, WHITE, (340,130), (340,370), 10)
pygame.draw.line(screen, BLACK, (170,250), (520,250), 10)

# Update
all_sprites.draw(screen)
pygame.display.flip()
clock.tick(60)

pygame.quit()

最佳答案

当你这样做时

def update(self):
if tennisball.rect.colliderect(robert):
self.rect.y -= 2

那么球只移动一次。在球员击球并且条件满足的那个“时刻”。小球移动后,条件不再满足,小球停止。

当 Racket 击中球时,您必须改变球的速度而不是其位置。
添加存储球速的属性(.speedx.speedy)。将属性初始化为0。在方法.update()中根据球的速度不断改变球的位置。当 Racket 击球时改变速度:

例如

class Ball(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("tennisball.png")
self.rect = self.image.get_rect()
self.rect.center = (360, 325)
self.speedx = 0
self.speedy = 0

def update(self):
if tennisball.rect.colliderect(robert):
self.speedy = -2
self.rect = self.rect.move(self.speedx, self.speedy)

请注意,如果球场另一侧有球员击球,则速度会再次改变。该速度还可以具有用于对角线运动的侧向分量。

关于python - 如何让球像打乒乓球一样保持移动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56713112/

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