鱼C论坛

 找回密码
 立即注册
查看: 3208|回复: 5

pygame壁球小游戏代码的疑问

[复制链接]
发表于 2022-3-28 19:15:41 | 显示全部楼层 |阅读模式
10鱼币

  1. import pygame
  2. import sys

  3. pygame.init()

  4. size = width, height = 600, 400
  5. speed = [1, 1]
  6. Black = (0, 0, 0)
  7. # screen = pygame.display.set_mode(size)
  8. screen = pygame.display.set_mode(size, pygame.RESIZABLE)
  9. pygame.display.set_caption('Pygame壁球')
  10. ball = pygame.image.load('C:\\Users\\admin\\Desktop\\PYG02-ball.gif')
  11. ballrect = ball.get_rect()
  12. fps = 300
  13. fclock = pygame.time.Clock()
  14. still = False

  15. while True:
  16.     for event in pygame.event.get():
  17.         if event.type == pygame.QUIT:
  18.             sys.exit()
  19.         elif event.type == pygame.KEYDOWN:
  20.             if event.key == pygame.K_LEFT:
  21.                 if speed[0] == 0:
  22.                     speed[0] = speed[0]
  23.                 else:
  24.                     speed[0] = (abs(speed[0]) - 1)*int(speed[0]/abs(speed[0]))
  25.             elif event.key == pygame.K_RIGHT:
  26.                 if speed[0] > 0:
  27.                     speed[0] += 1
  28.                 else:
  29.                     speed[0] -= 1
  30.             elif event.key == pygame.K_UP:
  31.                 if speed[1] > 0:
  32.                     speed[1] += 1
  33.                 else:
  34.                     speed[1] -= 1
  35.             elif event.key == pygame.K_DOWN:
  36.                 if speed[1] == 0:
  37.                     speed[1] = speed[1]
  38.                 else:
  39.                     speed[1] = (abs(speed[1]) - 1)*int(speed[1]/abs(speed[1]))
  40.             elif event.key == pygame.K_ESCAPE:
  41.                 sys.exit()
  42.         elif event.type == pygame.VIDEORESIZE:
  43.             size = width, height = event.size[0], event.size[1]
  44.             screen = pygame.display.set_mode(size, pygame.RESIZABLE)
  45.         elif event.type == pygame.MOUSEBUTTONDOWN:
  46.             if event.button == 1:
  47.                 still = True
  48.         elif event.type == pygame.MOUSEBUTTONUP:
  49.             still = False
  50.             if event.button == 1:
  51.                 ballrect = ballrect.move(event.pos[0] - ballrect.left, event.pos[1] - ballrect.top)
  52.         elif event.type == pygame.MOUSEMOTION:
  53.             if event.buttons[0] == 1:
  54.                 ballrect = ballrect.move(event.pos[0] - ballrect.left, event.pos[1] - ballrect.top)
  55.                 print(event.pos[0] - ballrect.left, event.pos[1] - ballrect.top)

  56.     if pygame.display.get_active() and not still:
  57.         ballrect = ballrect.move(speed[0], speed[1])
  58.     if ballrect.left < 0 or ballrect.right > width:
  59.         speed[0] = -speed[0]
  60.         if ballrect.right > width and ballrect.right + speed[0] > ballrect.right:
  61.             speed[0] = -speed[0]
  62.     if ballrect.top < 0 or ballrect.bottom > height:
  63.         speed[1] = -speed[1]
  64.         if ballrect.bottom > height and ballrect.bottom + speed[1] > ballrect.bottom:
  65.             speed[1] = -speed[1]

  66.     screen.fill(Black)
  67.     screen.blit(ball, ballrect)
  68.     pygame.display.update()
  69.     fclock.tick(fps)
  70.     # pygame.time.Clock().tick(fps)
复制代码


上面的代码中的pygame.MOUSEMOTION事件我不是很理解,(event.pos[0] - ballrect.left, event.pos[1] - ballrect.top)的结果不是(0,0)吗,
为什么球会跟着鼠标一起移动

PYG02-ball.gif
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-3-28 19:16:12 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-3-29 09:03:40 | 显示全部楼层
有没有人来管管我啊,卡这里了,找半天没结果
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2022-3-29 11:50:41 | 显示全部楼层
来个人啊,球球了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-8-9 14:13:54 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-8-9 14:55:46 | 显示全部楼层
在输出之前已经执行过ballrect.move(...)了,所以输出的是(0,0)
把ln58和ln59换一下输出的就不是0,0了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-28 21:02

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表