如何更改精灵的X和Y坐标?

发布于 2025-02-01 11:56:48 字数 673 浏览 3 评论 0原文

我正在制作乒乓球副本,只是为了玩乐。

我正在尝试,以便如果球碰到一个精灵(称为敌人的精灵),球将移至屏幕的中心,但我不知道如何用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云裳 2025-02-08 11:56:48

我找到了如何做。

speed = [10,10]
bola = pygame.image.load("ball.png")
bola = pygame.transform.scale(bola, (27,27))
ballrect = bola.get_rect()
ballrect = ballrect.move(speed)

#change x and y of the ball

centerX = winwidth/2
centerY = winheight/2
ballrect.y = centerY
ballrect.x = centerX

因此,基本上要更改并获取Pygame中精灵的X和Y位置,您必须在Sprite Rect的名称之后添加'.x'或.y'。

I found how to do it.

speed = [10,10]
bola = pygame.image.load("ball.png")
bola = pygame.transform.scale(bola, (27,27))
ballrect = bola.get_rect()
ballrect = ballrect.move(speed)

#change x and y of the ball

centerX = winwidth/2
centerY = winheight/2
ballrect.y = centerY
ballrect.x = centerX

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文