鱼C论坛

 找回密码
 立即注册
查看: 1600|回复: 0

很简单的pygame游戏有一步我没懂

[复制链接]
发表于 2020-9-24 22:56:31 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. import pygame,sys

  2. pygame.init()
  3. size = width,height = 600,400
  4. speed = [1,1]
  5. BLACK = 0,0,0
  6. screen = pygame.display.set_mode(size)
  7. pygame.display.set_caption("pygame壁球碰撞")
  8. ball = pygame.image.load('PYG02-ball.gif')
  9. ball_ract = ball.get_rect()
  10. fps = 300
  11. clock = pygame.time.Clock()
  12. still = False

  13. while True:
  14.     for event in pygame.event.get():
  15.         if event.type == pygame.QUIT:
  16.             sys.exit()
  17.         elif event.type == pygame.MOUSEBUTTONDOWN:
  18.             if event.button == 1:
  19.                 still = True
  20.         elif event.type == pygame.MOUSEBUTTONUP:
  21.             still = False
  22.             if event.button == 1:
  23.                 ball_ract = ball_ract.move(event.pos[0] - ball_ract.left,event.pos[1]-ball_ract.top)#就是这里,是为了防止鼠标点击到窗口旁边,卡在旁边不动,没懂为什么这么判断
  24.         elif event.type == pygame.MOUSEMOTION:
  25.             if event.buttons[0] == 1:
  26.                 ball_ract = ball_ract.move(event.pos[0]-ball_ract.left,event.pos[1]-ball_ract.top)
  27.     if pygame.display.get_active() and not still:
  28.         ball_ract = ball_ract.move(speed[0],speed[1])
  29.     if ball_ract.right > width or ball_ract.left < 0:
  30.         speed[0] = -speed[0]
  31.         if ball_ract.right > width and ball_ract.right + speed[0] > ball_ract.right:
  32.             speed[0] = -speed[0]
  33.     if ball_ract.bottom > height or ball_ract.top <0:
  34.         speed[1] = -speed[1]
  35.         if ball_ract.bottom > height and ball_ract.bottom + speed[1] > ball_ract.bottom:
  36.             speed[1] = -speed[1]

  37.     screen.fill(BLACK)
  38.     screen.blit(ball,ball_ract)
  39.     pygame.display.update()
  40.     clock.tick(fps)
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-27 14:43

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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