如何更改精灵的X和Y坐标?
我正在制作乒乓球副本,只是为了玩乐。
我正在尝试,以便如果球碰到一个精灵(称为敌人的精灵),球将移至屏幕的中心,但我不知道如何用pygame做到这一点。
#sprites
speed = [10,10]
bola = pygame.image.load("ball.png")
bola = pygame.transform.scale(bola, (27,27))
ballrect = bola.get_rect()
player1 = pygame.draw.rect(win, (255,255,255), (x, y, width, height))
player2 = pygame.draw.rect(win, (255,255,255), (650, y2, width, height))
enemy = pygame.draw.rect(win, (255,255,255), (0, 0, 3, winheight,))
ballrect = ballrect.move(speed)
#center of screen
centerX = winwidth/2
centerY = winheight/2
#collision with left wall (called enemy sprite)
if ballrect.colliderect(enemy):
puntos = puntos + 1
print(puntos)
I am making a pong game replica just to have fun.
I'm trying so that if the ball touches a sprite (called the enemy sprite), the ball will move to the center of the screen but I don't know how to do that with pygame.
#sprites
speed = [10,10]
bola = pygame.image.load("ball.png")
bola = pygame.transform.scale(bola, (27,27))
ballrect = bola.get_rect()
player1 = pygame.draw.rect(win, (255,255,255), (x, y, width, height))
player2 = pygame.draw.rect(win, (255,255,255), (650, y2, width, height))
enemy = pygame.draw.rect(win, (255,255,255), (0, 0, 3, winheight,))
ballrect = ballrect.move(speed)
#center of screen
centerX = winwidth/2
centerY = winheight/2
#collision with left wall (called enemy sprite)
if ballrect.colliderect(enemy):
puntos = puntos + 1
print(puntos)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了如何做。
因此,基本上要更改并获取Pygame中精灵的X和Y位置,您必须在Sprite Rect的名称之后添加'.x'或.y'。
I found how to do it.
So basically to change and get the x and y position of a sprite in pygame, you have to add '.x' or '.y' after the name of the sprite rect.