怎么在Python中利用Pycharm编写一个猴子摘桃小游戏

  介绍

怎么在Python中利用Pycharm编写一个猴子摘桃小游戏?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

源码及注释:

import  pygame   得到sys  import 退出   得到random  import  randint   import 时间   import 操作系统      #,定义窗口分辨率   SCREEN_WIDTH =700   SCREEN_HEIGHT =600      时间=current_path  os.path.abspath (os.path.dirname (__file__))   时间=root_path  current_path [: current_path.find (“monkey-picking-peach \ \“), +, len (“monkey-picking-peach \ \“)], \   ,,+,“资源\ \ \ \“图像;   #,图片   +=BACKGROUND_IMAGE_PATH  root_path “background.jpg"   +=MONKEY_IMAGE_PATH  root_path “monkey.png"   +=APPLE_IMAGE_PATH  root_path “apple.png"   JUMP_STATUS =False   OVER_FLAG =False   时间=START_TIME 没有   offset =, {pygame.K_LEFT: 0,, pygame.K_RIGHT:, 0,, pygame.K_UP:, 0,, pygame.K_DOWN:, 0}      #,定义画面帧率   FRAME_RATE =60      #,定义动画周期(帧数)   ANIMATE_CYCLE =30      ticks =0   时间=clock  pygame.time.Clock ()         #,猴子类   class 猴子(pygame.sprite.Sprite):   ,#苹果的数量   apple_num =, 0      ,def  __init__ (mon_surface,自我,还以为;monkey_pos):   pygame.sprite.Sprite.__init__才能(自我)   self.image 才能=mon_surface   时间=self.rect 才能;self.image.get_rect ()   self.rect.topleft 才能=monkey_pos   self.speed 才能=5      ,#控制猴子的移动   ,def 移动(自我,,_offset):   global  JUMP_STATUS才能   x 才能=,self.rect.left  +, _offset [pygame.K_RIGHT],安康;_offset [pygame.K_LEFT]   y 才能=,self.rect.top  +, _offset [pygame.K_DOWN],安康;_offset [pygame.K_UP]   if 才能y  & lt;, 0:   ,,self.rect.top =0   ,,JUMP_STATUS =,真的   elif 才能;y 祝辞=,SCREEN_HEIGHT 作用;self.rect.height:   ,,self.rect.top =SCREEN_HEIGHT 作用;self.rect.height   ,,JUMP_STATUS =False   其他的才能:   ,,self.rect.top =y   ,,JUMP_STATUS =,真的      if 才能x  & lt;, 0:   ,,self.rect.left =0   elif 才能;x 祝辞,SCREEN_WIDTH 作用;self.rect.width:   ,,self.rect.left =SCREEN_WIDTH 作用;self.rect.width   其他的才能:   ,,self.rect.left =, x      ,#接苹果   ,def  picking_apple(自我,,app_group):      #才能,判断接到几个苹果   picked_apples 才能=,pygame.sprite.spritecollide(自我,,app_group,,真的)      #,才能添加分数   self.apple_num 才能+=,len (picked_apples)      #,才能接到的苹果消失   for 才能;picked_apple  picked_apples:拷贝   ,,picked_apple.kill ()         #,苹果类   class 苹果(pygame.sprite.Sprite):   ,def  __init__ (app_surface,自我,还以为;apple_pos):   pygame.sprite.Sprite.__init__才能(自我)   self.image 才能=app_surface   时间=self.rect 才能;self.image.get_rect ()   self.rect.topleft 才能=apple_pos   self.speed 才能=1      ,def 更新(自我):   global  START_TIME才能   if 才能;START_TIME  is 没有:   ,,START_TIME =, time.time ()   self.rect.top 才能+=,(self.speed  *, (1, +, (time.time(),安康;START_TIME),/, 40))   if 才能;self.rect.top 祝辞,SCREEN_HEIGHT:   ,,#,苹果落地游戏结束   ,,global  OVER_FLAG   ,,OVER_FLAG =,真的   ,,self.kill ()         #,初始化游戏   pygame.init ()   时间=screen  pygame.display.set_mode ((SCREEN_WIDTH, SCREEN_HEIGHT))   pygame.display.set_caption(“猴子接苹果“)      #,载入图片   时间=background_surface  pygame.image.load (BACKGROUND_IMAGE_PATH) .convert ()   时间=monkey_surface  pygame.image.load (MONKEY_IMAGE_PATH) .convert_alpha ()   时间=apple_surface  pygame.image.load (APPLE_IMAGE_PATH) .convert_alpha ()      #,创建猴子   monkey =,猴子(monkey_surface, (200,, 500))      #,创建苹果组   时间=apple_group  pygame.sprite.Group ()      #,分数字体   时间=score_font  pygame.font.SysFont (“arial",, 40)      #,主循环   while 真正的:      ,if  OVER_FLAG:   ,打破      ,#控制游戏最大帧率   ,clock.tick (FRAME_RATE)      ,#绘制背景   ,screen.blit (background_surface, (0, 0))      ,if  ticks 祝辞=,ANIMATE_CYCLE:   ticks 才能=0      ,#产生苹果   30,if  ticks  %,,==, 0:   时间=apple 才能;苹果(apple_surface,   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null   null

怎么在Python中利用Pycharm编写一个猴子摘桃小游戏