2020年9月12日星期六

88 《零基礎入門學習Python》筆記 第088講:Pygame:摩擦摩擦

《零基礎入門學習Python》第088講:Pygame:摩擦摩擦



Play The Ball這個小遊戲現在已經有了背景音樂,有了小球,有了碰撞檢測,接下來我們要做的就是摩擦摩擦。
我們有一塊玻璃面板的圖片,如下圖所示:為了方便,我把文字也打上去了。(是透明的哦)
(還是老樣子,有需要素材的可以在評論區留下郵箱或者其他的啥)
 
現在我們的要求就是把這塊玻璃面板加載到程序底部的中央位置。
  1. import pygame
  2. import sys
  3. from pygame.locals import *
  4. from random import *
  5. # 球类继承自Spirte类
  6. class Ball(pygame.sprite.Sprite):
  7. def __init__(self, image, position, speed, bg_size):
  8. # 初始化动画精灵
  9. pygame.sprite.Sprite.__init__(self)
  10. self.image = pygame.image.load(image).convert_alpha()
  11. self.rect = self.image.get_rect()
  12. # 将小球放在指定位置
  13. self.rect.left, self.rect.top = position
  14. self.speed = speed
  15. self.width, self.height = bg_size[0], bg_size[1]
  16. self.radius = self.rect.width / 2 #增加半径属性
  17. def move(self):
  18. self.rect = self.rect.move(self.speed)
  19. # 如果小球的左侧出了边界,那么将小球左侧的位置改为右侧的边界
  20. # 这样便实现了从左边进入,右边出来的效果
  21. if self.rect.right < 0:
  22. self.rect.left = self.width
  23. elif self.rect.left > self.width:
  24. self.rect.right = 0
  25. elif self.rect.bottom < 0:
  26. self.rect.top = self.height
  27. elif self.rect.top > self.height:
  28. self.rect.bottom = 0
  29. ##############################################################
  30. class Glass(pygame.sprite.Sprite):
  31. def __init__(self, glass_image, bg_size):
  32. #初始化动画精灵
  33. pygame.sprite.Sprite.__init__(self)
  34. self.glass_image = pygame.image.load(glass_image).convert_alpha()
  35. self.glass_rect = self.glass_image.get_rect()
  36. self.glass_rect.left, self.glass_rect.top = \
  37. (bg_size[0] - self.glass_rect.width) // 2, \
  38. bg_size[1] - self.glass_rect.height
  39. ##############################################################
  40. def main():
  41. pygame.init()
  42. ball_image = "gray_ball.png"
  43. bg_image = "background.png"
  44. glass_image = "glass.png"
  45. running = True
  46. #添加背景音乐
  47. pygame.mixer.music.load('bg_music.ogg')
  48. pygame.mixer.music.set_volume(0.2)#设置音量
  49. pygame.mixer.music.play(1, 160)#播放
  50. #加载音效
  51. winner_sound = pygame.mixer.Sound("winner.wav")
  52. winner_sound.set_volume(0.2)
  53. loser_sound = pygame.mixer.Sound("loser.wav")
  54. loser_sound.set_volume(0.2)
  55. laugh_sound = pygame.mixer.Sound("laugh.wav")
  56. laugh_sound.set_volume(0.2)
  57. hole_sound = pygame.mixer.Sound("hole.wav")
  58. hole_sound.set_volume(0.2)
  59. #背景音乐会贯穿游戏的始终,背景音乐完整播放一次我们视为游戏的时间,
  60. #音乐播放完时,游戏结束
  61. GAMEOVER = USEREVENT
  62. pygame.mixer.music.set_endevent(GAMEOVER)
  63. # 根据背景图片指定游戏界面尺寸
  64. bg_size = width, height = 1024, 681
  65. screen = pygame.display.set_mode(bg_size)
  66. pygame.display.set_caption("Play the ball - Python Demo")
  67. background = pygame.image.load(bg_image).convert_alpha()
  68. # 用来存放小球对象的列表
  69. balls = []
  70. group = pygame.sprite.Group()
  71. # 创建五个小球
  72. for i in range(5):
  73. # 位置随机,速度随机
  74. position = randint(0, width-100), randint(0, height-100)
  75. speed = [randint(-10, 10), randint(-10, 10)]
  76. ball = Ball(ball_image, position, speed, bg_size)
  77. while pygame.sprite.spritecollide(ball, group, False, pygame.sprite.collide_circle):#在创建小球这里必须进行一下碰撞检测
  78. ball.rect.left, ball.rect.top = randint(0, width-100), randint(0, height-100)
  79. balls.append(ball)
  80. group.add(ball)
  81. ###########################################################
  82. glass = Glass(glass_image, bg_size)
  83. ###########################################################
  84. clock = pygame.time.Clock()
  85. while running:
  86. for event in pygame.event.get():
  87. if event.type == QUIT:
  88. sys.exit()
  89. elif event.type == GAMEOVER: #判断事件是否为我们自定义的GAMEOVER事件
  90. loser_sound.play()
  91. pygame.time.delay(2000)#暂停2秒
  92. laugh_sound.play()
  93. running = False
  94. screen.blit(background, (0, 0))
  95. #############################################
  96. screen.blit(glass.glass_image, glass.glass_rect)
  97. #############################################
  98. for each in balls:
  99. each.move()
  100. screen.blit(each.image, each.rect)
  101. for each in group:
  102. group.remove(each) #把自身拿出来
  103. if pygame.sprite.spritecollide(each, group, False, pygame.sprite.collide_circle):#把自己和别的球进行碰撞检测
  104. each.speed[0] = -each.speed[0]
  105. each.speed[1] = -each.speed[1]
  106. group.add(each)#还要把自己放进去
  107. pygame.display.flip()
  108. clock.tick(30)
  109. if __name__ == "__main__":
  110. main()
接下來我們就需要對鼠標動手了,大家也看到了,Pygame 默認的鼠標非常難看,又小又黑又土,而作為一個遊戲的話,漂亮的光標往往就是決勝的關鍵,遊戲開發注重的就是細節,現在我們就用自定義的光標圖片替換原來又小又黑又土的光標。如圖·所示:
接下來要做的事就是自定義光標,然後把這個小手(光標)限制在玻璃面板內。
關於自定義鼠標的光標:
我們的做法就是使用set_visible() 方法來將光標設為不可見,然後使用get_pos() 方法獲取鼠標的實時位置,獲取之後,我們實時的把hand.png 圖片畫到這個位置上去,這樣子就相當於實現了自定義光標。
  • pygame.mouse.set_visible() —— 隱藏或顯示鼠標光標
  • pygame.mouse.get_pos() —— 獲取鼠標光標的位置
我們來代碼實現:
  1. import pygame
  2. import sys
  3. from pygame.locals import *
  4. from random import *
  5. # 球类继承自Spirte类
  6. class Ball(pygame.sprite.Sprite):
  7. def __init__(self, image, position, speed, bg_size):
  8. # 初始化动画精灵
  9. pygame.sprite.Sprite.__init__(self)
  10. self.image = pygame.image.load(image).convert_alpha()
  11. self.rect = self.image.get_rect()
  12. # 将小球放在指定位置
  13. self.rect.left, self.rect.top = position
  14. self.speed = speed
  15. self.width, self.height = bg_size[0], bg_size[1]
  16. self.radius = self.rect.width / 2 #增加半径属性
  17. def move(self):
  18. self.rect = self.rect.move(self.speed)
  19. # 如果小球的左侧出了边界,那么将小球左侧的位置改为右侧的边界
  20. # 这样便实现了从左边进入,右边出来的效果
  21. if self.rect.right < 0:
  22. self.rect.left = self.width
  23. elif self.rect.left > self.width:
  24. self.rect.right = 0
  25. elif self.rect.bottom < 0:
  26. self.rect.top = self.height
  27. elif self.rect.top > self.height:
  28. self.rect.bottom = 0
  29. ##############################################################
  30. class Glass(pygame.sprite.Sprite):
  31. def __init__(self, glass_image, mouse_image, bg_size):
  32. #初始化动画精灵
  33. pygame.sprite.Sprite.__init__(self)
  34. self.glass_image = pygame.image.load(glass_image).convert_alpha()
  35. self.glass_rect = self.glass_image.get_rect()
  36. self.glass_rect.left, self.glass_rect.top = \
  37. (bg_size[0] - self.glass_rect.width) // 2, \
  38. bg_size[1] - self.glass_rect.height
  39. self.mouse_image = pygame.image.load(mouse_image).convert_alpha()#加载小手光标
  40. self.mouse_rect = self.mouse_image.get_rect()#获取小手光标矩形位置
  41. self.mouse_rect.left, self.mouse_rect.top = \
  42. self.glass_rect.left, self.glass_rect.top #小手光标初始在玻璃面板左上角
  43. pygame.mouse.set_visible(False)#设置Pygame 光标不可见
  44. ##############################################################
  45. def main():
  46. pygame.init()
  47. ball_image = "gray_ball.png"
  48. bg_image = "background.png"
  49. glass_image = "glass.png"
  50. mouse_image = "hand.png"
  51. running = True
  52. #添加背景音乐
  53. pygame.mixer.music.load('bg_music.ogg')
  54. pygame.mixer.music.set_volume(0.2)#设置音量
  55. pygame.mixer.music.play(1, 160)#播放
  56. #加载音效
  57. winner_sound = pygame.mixer.Sound("winner.wav")
  58. winner_sound.set_volume(0.2)
  59. loser_sound = pygame.mixer.Sound("loser.wav")
  60. loser_sound.set_volume(0.2)
  61. laugh_sound = pygame.mixer.Sound("laugh.wav")
  62. laugh_sound.set_volume(0.2)
  63. hole_sound = pygame.mixer.Sound("hole.wav")
  64. hole_sound.set_volume(0.2)
  65. #背景音乐会贯穿游戏的始终,背景音乐完整播放一次我们视为游戏的时间,
  66. #音乐播放完时,游戏结束
  67. GAMEOVER = USEREVENT
  68. pygame.mixer.music.set_endevent(GAMEOVER)
  69. # 根据背景图片指定游戏界面尺寸
  70. bg_size = width, height = 1024, 681
  71. screen = pygame.display.set_mode(bg_size)
  72. pygame.display.set_caption("Play the ball - Python Demo")
  73. background = pygame.image.load(bg_image).convert_alpha()
  74. # 用来存放小球对象的列表
  75. balls = []
  76. group = pygame.sprite.Group()
  77. # 创建五个小球
  78. for i in range(5):
  79. # 位置随机,速度随机
  80. position = randint(0, width-100), randint(0, height-100)
  81. speed = [randint(-10, 10), randint(-10, 10)]
  82. ball = Ball(ball_image, position, speed, bg_size)
  83. while pygame.sprite.spritecollide(ball, group, False, pygame.sprite.collide_circle):#在创建小球这里必须进行一下碰撞检测
  84. ball.rect.left, ball.rect.top = randint(0, width-100), randint(0, height-100)
  85. balls.append(ball)
  86. group.add(ball)
  87. ###########################################################
  88. glass = Glass(glass_image, mouse_image, bg_size)
  89. ###########################################################
  90. clock = pygame.time.Clock()
  91. while running:
  92. for event in pygame.event.get():
  93. if event.type == QUIT:
  94. sys.exit()
  95. elif event.type == GAMEOVER: #判断事件是否为我们自定义的GAMEOVER事件
  96. loser_sound.play()
  97. pygame.time.delay(2000)#暂停2秒
  98. laugh_sound.play()
  99. running = False
  100. screen.blit(background, (0, 0))
  101. #############################################
  102. screen.blit(glass.glass_image, glass.glass_rect)
  103. #############################################
  104. #将小手光标画在Pygame 默认光标位置上
  105. glass.mouse_rect.left, glass.mouse_rect.top = pygame.mouse.get_pos()
  106. #限制光标只能在玻璃面板范围内移动(摩擦摩擦)
  107. if glass.mouse_rect.left < glass.glass_rect.left:
  108. glass.mouse_rect.left = glass.glass_rect.left
  109. if glass.mouse_rect.left > glass.glass_rect.right - glass.mouse_rect.width:
  110. glass.mouse_rect.left = glass.glass_rect.right - glass.mouse_rect.width
  111. if glass.mouse_rect.top < glass.glass_rect.top:
  112. glass.mouse_rect.top = glass.glass_rect.top
  113. if glass.mouse_rect.top > glass.mouse_rect.bottom - glass.mouse_rect.height:
  114. glass.mouse_rect.top = glass.mouse_rect.bottom - glass.mouse_rect.height
  115. screen.blit(glass.mouse_image, glass.mouse_rect)
  116. #############################################
  117. for each in balls:
  118. each.move()
  119. screen.blit(each.image, each.rect)
  120. for each in group:
  121. group.remove(each) #把自身拿出来
  122. if pygame.sprite.spritecollide(each, group, False, pygame.sprite.collide_circle):#把自己和别的球进行碰撞检测
  123. each.speed[0] = -each.speed[0]
  124. each.speed[1] = -each.speed[1]
  125. group.add(each)#还要把自己放进去
  126. pygame.display.flip()
  127. clock.tick(30)
  128. if __name__ == "__main__":
  129. main()
下一步,讓小球響應光標的移動頻率。
我們需要讓小球響應光標一定頻率的摩擦摩擦而停下來變成綠色,就是說,鼠標在玻璃面板上摩擦,達到一定頻率的時候,某一個匹配的小球會停下來,而不是所有小球,停下來的小球就任你操作了。
我們知道,鼠標的一定會產生事件,我們可以利用這一點。假設單位時間是一秒,我們檢測一秒內鼠標在玻璃面板內產生多少事件,從而判定每一秒產生的事件數是否能夠匹配到某一個小球需要的要求。
步驟如下:
1、為每個小球設定一個不同的目標;
2、創建一個motion 變量來記錄每一秒鐘產生事件數量;
3、為小球添加一個check() 方法,用於判斷鼠標在1秒鐘內產生的事件數量是否匹配此目標;
4、添加一個自定義事件,每一秒鐘觸發一次。調用每個小球的check() 檢測motion 的值是否匹配某一個小球的目標,並將motion重新初始化,以便記錄下1秒鼠標事件數量;
5、小球應該添加一個control 屬性,用於記錄當前的狀態(綠色-> 玩家控制or 灰色-> 隨機移動)。
6、通過檢查control 屬性決定繪製什麼顏色的小球。
  1. import pygame
  2. import sys
  3. from pygame.locals import *
  4. from random import *
  5. # 球类继承自Spirte类
  6. class Ball(pygame.sprite.Sprite):
  7. def __init__(self, grayball_image, greenball_image, position, speed, bg_size, target):
  8. # 初始化动画精灵
  9. pygame.sprite.Sprite.__init__(self)
  10. self.grayball_image = pygame.image.load(grayball_image).convert_alpha() #加载灰色小球
  11. self.greenball_image = pygame.image.load(greenball_image).convert_alpha() #加载绿色小球
  12. self.rect = self.grayball_image.get_rect() #两颜色小球矩形位置是一样的
  13. # 将小球放在指定位置
  14. self.rect.left, self.rect.top = position
  15. self.speed = speed
  16. ############################################
  17. #1、为每个小球设定一个不同的目标;
  18. self.target = target
  19. #############################################
  20. #5、小球应该添加一个 control 属性,用于记录当前的状态(绿色 -> 玩家控制 or 灰色 -> 随机移动)。
  21. self.control = False
  22. ##################################################
  23. self.width, self.height = bg_size[0], bg_size[1]
  24. self.radius = self.rect.width / 2 #增加半径属性
  25. def move(self):
  26. self.rect = self.rect.move(self.speed)
  27. # 如果小球的左侧出了边界,那么将小球左侧的位置改为右侧的边界
  28. # 这样便实现了从左边进入,右边出来的效果
  29. if self.rect.right < 0:
  30. self.rect.left = self.width
  31. elif self.rect.left > self.width:
  32. self.rect.right = 0
  33. elif self.rect.bottom < 0:
  34. self.rect.top = self.height
  35. elif self.rect.top > self.height:
  36. self.rect.bottom = 0
  37. ############################################################################
  38. #3、为小球添加一个 check() 方法,用于判断鼠标在1秒钟内产生的事件数量是否匹配此目标;
  39. def check(self, motion):
  40. if self.target < motion < self.target + 5:
  41. return True
  42. else:
  43. return False
  44. ############################################################################
  45. class Glass(pygame.sprite.Sprite):
  46. def __init__(self, glass_image, mouse_image, bg_size):
  47. #初始化动画精灵
  48. pygame.sprite.Sprite.__init__(self)
  49. self.glass_image = pygame.image.load(glass_image).convert_alpha()
  50. self.glass_rect = self.glass_image.get_rect()
  51. self.glass_rect.left, self.glass_rect.top = \
  52. (bg_size[0] - self.glass_rect.width) // 2, \
  53. bg_size[1] - self.glass_rect.height
  54. self.mouse_image = pygame.image.load(mouse_image).convert_alpha()#加载小手光标
  55. self.mouse_rect = self.mouse_image.get_rect()#获取小手光标矩形位置
  56. self.mouse_rect.left, self.mouse_rect.top = \
  57. self.glass_rect.left, self.glass_rect.top #小手光标初始在玻璃面板左上角
  58. pygame.mouse.set_visible(False)#设置Pygame 光标不可见
  59. def main():
  60. pygame.init()
  61. grayball_image = "gray_ball.png"
  62. greenball_image = "green_ball.png"
  63. bg_image = "background.png"
  64. glass_image = "glass.png"
  65. mouse_image = "hand.png"
  66. running = True
  67. #添加背景音乐
  68. pygame.mixer.music.load('bg_music.ogg')
  69. pygame.mixer.music.set_volume(0.2)#设置音量
  70. pygame.mixer.music.play()#播放
  71. #加载音效
  72. winner_sound = pygame.mixer.Sound("winner.wav")
  73. winner_sound.set_volume(0.2)
  74. loser_sound = pygame.mixer.Sound("loser.wav")
  75. loser_sound.set_volume(0.2)
  76. laugh_sound = pygame.mixer.Sound("laugh.wav")
  77. laugh_sound.set_volume(0.2)
  78. hole_sound = pygame.mixer.Sound("hole.wav")
  79. hole_sound.set_volume(0.2)
  80. #背景音乐会贯穿游戏的始终,背景音乐完整播放一次我们视为游戏的时间,
  81. #音乐播放完时,游戏结束
  82. GAMEOVER = USEREVENT
  83. pygame.mixer.music.set_endevent(GAMEOVER)
  84. # 根据背景图片指定游戏界面尺寸
  85. bg_size = width, height = 1024, 681
  86. screen = pygame.display.set_mode(bg_size)
  87. pygame.display.set_caption("Play the ball - Python Demo")
  88. background = pygame.image.load(bg_image).convert_alpha()
  89. # 用来存放小球对象的列表
  90. balls = []
  91. group = pygame.sprite.Group()
  92. # 创建五个小球
  93. for i in range(5):
  94. # 位置随机,速度随机
  95. position = randint(0, width-100), randint(0, height-100)
  96. speed = [randint(-10, 10), randint(-10, 10)]
  97. ball = Ball(grayball_image, greenball_image, position, speed, bg_size, 5 * (i + 1)) #target 设为5-30,比较适中
  98. while pygame.sprite.spritecollide(ball, group, False, pygame.sprite.collide_circle):#在创建小球这里必须进行一下碰撞检测
  99. ball.rect.left, ball.rect.top = randint(0, width-100), randint(0, height-100)
  100. balls.append(ball)
  101. group.add(ball)
  102. glass = Glass(glass_image, mouse_image, bg_size)
  103. #########################################
  104. #2、创建一个motion 变量来记录每一秒钟产生事件数量;
  105. motion = 0
  106. #########################################
  107. #4.1、添加一个自定义事件,每一秒钟触发一次。
  108. MYTIMER = USEREVENT + 1 #自定义事件的知识点可以查看上一节课的末尾注解
  109. pygame.time.set_timer(MYTIMER, 1000)
  110. ##############################################################
  111. clock = pygame.time.Clock()
  112. while running:
  113. for event in pygame.event.get():
  114. if event.type == QUIT:
  115. sys.exit()
  116. elif event.type == GAMEOVER: #判断事件是否为我们自定义的GAMEOVER事件
  117. loser_sound.play()
  118. pygame.time.delay(2000)#暂停2秒
  119. laugh_sound.play()
  120. running = False
  121. ##############################################
  122. #4.2、 调用每个小球的 check() 检测 motion 的值是否匹配某一个小球的目标,并将motion重新初始化,以便记录下1秒鼠标事件数量;
  123. elif event.type == MYTIMER:
  124. if motion:
  125. for each in group:
  126. if each.check(motion):
  127. each.speed = [0, 0]
  128. each.control = True
  129. motion = 0
  130. ##############################################
  131. #需要计算一下motion
  132. elif event.type == MOUSEMOTION:
  133. motion += 1
  134. ##############################################
  135. screen.blit(background, (0, 0))
  136. screen.blit(glass.glass_image, glass.glass_rect)
  137. #将小手光标画在Pygame 默认光标位置上
  138. glass.mouse_rect.left, glass.mouse_rect.top = pygame.mouse.get_pos()
  139. #限制光标只能在玻璃面板范围内移动(摩擦摩擦)
  140. if glass.mouse_rect.left < glass.glass_rect.left:
  141. glass.mouse_rect.left = glass.glass_rect.left
  142. if glass.mouse_rect.left > glass.glass_rect.right - glass.mouse_rect.width:
  143. glass.mouse_rect.left = glass.glass_rect.right - glass.mouse_rect.width
  144. if glass.mouse_rect.top < glass.glass_rect.top:
  145. glass.mouse_rect.top = glass.glass_rect.top
  146. if glass.mouse_rect.top > glass.mouse_rect.bottom - glass.mouse_rect.height:
  147. glass.mouse_rect.top = glass.mouse_rect.bottom - glass.mouse_rect.height
  148. screen.blit(glass.mouse_image, glass.mouse_rect)
  149. for each in balls:
  150. each.move()
  151. ##############################
  152. #如果小球的 control 属性为 True,就画绿球
  153. if each.control:
  154. #画绿色小球
  155. screen.blit(each.greenball_image, each.rect)
  156. else:
  157. #画灰色小球
  158. screen.blit(each.grayball_image, each.rect)
  159. ##############################
  160. for each in group:
  161. group.remove(each) #把自身拿出来
  162. if pygame.sprite.spritecollide(each, group, False, pygame.sprite.collide_circle):#把自己和别的球进行碰撞检测
  163. each.speed[0] = -each.speed[0]
  164. each.speed[1] = -each.speed[1]
  165. group.add(each)#还要把自己放进去
  166. pygame.display.flip()
  167. clock.tick(30)
  168. if __name__ == "__main__":
  169. main()

0 留言:

發佈留言