pygame学习笔记(6):完成一个简单的游戏
学了这么长时间的Pygame,一直想写个游戏实战一下。看起来很简单的游戏,写其来怎么这么难。最初想写个俄罗斯方块,想了很长时间如何实现,想来想去,也没写出来,于是干脆下载别人的代码来读。后来,要想写一个帮助记忆的挖宝箱的游戏,结果也没完成。唯一完成了就是下面这个小人接金币的游戏,超级简单,通过左右键控制小人移动去接空中下来的金币,接住金币得5分,接不住游戏结束,金币速度会随着level的关数而越来越快。完成这段代码后,我依然觉得这段代码写得很差,确实也是自己对pygame只是掌握了皮毛,对surface、sprite这些理解的还不透彻。这里把代码写出来,有时间的大牛们可以帮助指点一下,让我也有所提高。
#-*-coding:cp936-*- ''' 一个超级简单的游戏 左右键控制小人移动去接空中下来的金币,接住金币得5分,接不住游戏结束,金币速度会随着level的关数 而越来越快 ''' importpygame,sys,os,random pygame.init() classrect():#画出小人 def__init__(self,filename,initial_position): self.image=pygame.image.load(filename) self.rect=self.image.get_rect() self.rect.topleft=initial_position classgoldrect(pygame.sprite.Sprite):#绘出金币 def__init__(self,gold_position,speed): pygame.sprite.Sprite.__init__(self) self.image=pygame.image.load('image\\gold.png') self.rect=self.image.get_rect() self.rect.topleft=gold_position self.speed=speed defmove(self): self.rect=self.rect.move(self.speed) defdrawback():#绘出背景图片 my_back=pygame.image.load('image\\qi3.jpg') bakscreen.blit(my_back,[0,0]) defloadtext(levelnum,score,highscore):#绘出成绩、level、最高分等 my_font=pygame.font.SysFont(None,24) levelstr='Level:'+str(levelnum) text_screen=my_font.render(levelstr,True,(255,0,0)) bakscreen.blit(text_screen,(650,50)) highscorestr='Higescore:'+str(highscore) text_screen=my_font.render(highscorestr,True,(255,0,0)) bakscreen.blit(text_screen,(650,80)) scorestr='Score:'+str(score) text_screen=my_font.render(scorestr,True,(255,0,0)) bakscreen.blit(text_screen,(650,110)) defloadgameover(scorenum,highscore):#绘出GAMEOVER my_font=pygame.font.SysFont(None,50) levelstr='GAMEOVER' over_screen=my_font.render(levelstr,True,(255,0,0)) bakscreen.blit(over_screen,(300,240)) highscorestr='YOURSCOREIS'+str(scorenum) over_screen=my_font.render(highscorestr,True,(255,0,0)) bakscreen.blit(over_screen,(280,290)) ifscorenum>int(highscore):#写入最高分 highscorestr='YOURHAVEGOTTHEHIGHESTSCORE!' text_screen=my_font.render(highscorestr,True,(255,0,0)) bakscreen.blit(text_screen,(100,340)) highfile=open('highscore','w') highfile.writelines(str(scorenum)) highfile.close() defgethighscore():#读取最高分 ifos.path.isfile('highscore'): highfile=open('highscore','r') highscore=highfile.readline() highfile.close() else: highscore=0 returnhighscore bakscreen=pygame.display.set_mode([800,600]) bakscreen.fill([0,160,233]) pygame.display.set_caption('Dig!Dig!') drawback() levelnum=1#level scorenum=0#得分 highscore=gethighscore()#最高分 ileft=1#记录向左移动步数,用来控制图片 iright=10#记录向右移动步数,用来控制图片 x=100 y=450 filename='image\\1.png' backimg_ren=rect(filename,[x,y]) bakscreen.blit(backimg_ren.image,backimg_ren.rect) loadtext(levelnum,scorenum,highscore) goldx=random.randint(50,580) speed=[0,levelnum] mygold=goldrect([goldx,100],speed) pygame.display.update() whileTrue: ifscorenum>0andscorenum/50.0==int(scorenum/50.0):#当得分是50的倍数时修改level levelnum=scorenum/50+1 speed=[0,levelnum] foreventinpygame.event.get(): ifevent.type==pygame.QUIT: sys.exit() #makegold pressed_keys=pygame.key.get_pressed() ifpressed_keys[pygame.K_LEFT]:#按下左键 drawback() loadtext(levelnum,scorenum,highscore) ifiright>14:iright=10 iright=iright+1 filename='image\\'+str(iright)+'.png' ifx<50: x=50 else: x=x-10 backimg_surface=rect(filename,[x,y]) bakscreen.blit(backimg_surface.image,backimg_surface.rect) ifpressed_keys[pygame.K_RIGHT]:#按下右键 drawback() loadtext(levelnum,scorenum,highscore) ifileft>4:ileft=0 ileft=ileft+1 filename='image\\'+str(ileft)+'.png' ifx>560: x=560 else: x=x+10 backimg_surface=rect(filename,[x,y]) bakscreen.blit(backimg_surface.image,backimg_surface.rect) drawback() loadtext(levelnum,scorenum,highscore) mygold.move() bakscreen.blit(mygold.image,mygold.rect) backimg_surface=rect(filename,[x,y]) bakscreen.blit(backimg_surface.image,backimg_surface.rect) ifmygold.rect.top>600:#判断金币是否着地,一但着地,游戏结束 loadgameover(scorenum,highscore) ifmygold.rect.colliderect(backimg_surface.rect):#判断金币是否与小人碰撞,如果碰撞表示小人接到金币 scorenum+=5 loadtext(levelnum,scorenum,highscore) goldx=random.randint(50,580) mygold=goldrect([goldx,100],speed) pygame.display.update()
程序中用到的资源可从这里下载:文件名:gold.7z,访问地址:http://www.kuaipan.cn/file/id_16699292408348719.htm