Pygame Study Notes (6): Complete a simple game

Source: Internet
Author: User
After learning such a long time, Pygame always wanted to write a game. It looks like a very simple game, and it's so hard to write. Originally wanted to write a Tetris, think for a long time how to achieve, want to go, also did not write out, so simply download someone else's code to read. Later, to write a help memory of the digging chest of the game, the result is not finished. The only complete is the following this small person to receive gold coins game, super simple, through the key control around the small person move to pick up the air down the gold, catch Gold 5 points, can not catch the end of the game, gold coins with the level of speed will be more and more quickly. After the completion of this code, I still feel that this code is poorly written, it is also my own to pygame just mastered the fur, surface, Sprite These understanding is not thorough. Here to write the code, the time of the Daniel can help guide, let me also improved.

#-*-coding:cp936-*-"A super simple game around keys control the villain move to pick up the air down the gold, catch Gold 5 points, can not catch the end of the game, gold coins speed will be with the level of the number and more and faster" import Pygame,sys,os, Randompygame.init () class rect (): #画出小人 def __init__ (self,filename,initial_position): Self.image=pygame.image.load ( FileName) self.rect=self.image.get_rect () Self.rect.topleft=initial_position class Goldrect (Pygame.sprite.Sprite) : #绘出金币 def __init__ (self,gold_position,speed): pygame.sprite.sprite.__init__ (self) self.image=pygame.image.load (' I Mage\\gold.png ') self.rect=self.image.get_rect () self.rect.topleft=gold_position self.speed=speed def move (self) : Self.rect=self.rect.move (Self.speed) def drawback (): #绘出背景图片 my_back=pygame.image.load (' image\\qi3.jpg ') BA Kscreen.blit (my_back,[0,0]) def loadtext (Levelnum,score,highscore): #绘出成绩, level, highest score 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)) scores  Tr= ' score: ' +str (Score) Text_screen=my_font.render (Scorestr, True, (255, 0, 0)) Bakscreen.blit (Text_screen, (650,110)) def loadgameover (Scorenum,highscore): #绘出GAME over My_font=pygame.font.sysfont (none,50) levelstr= ' GAME over ' over_ Screen=my_font.render (Levelstr, True, (255, 0, 0)) Bakscreen.blit (Over_screen, (300,240)) highscorestr= ' YOUR SCORE is ' + STR (scorenum) Over_screen=my_font.render (Highscorestr, True, (255, 0, 0)) Bakscreen.blit (Over_screen, (280,290)) if SCO    Renum>int (highscore): #写入最高分 highscorestr= ' YOUR have GOT the highest score! ' Text_screen=my_font.render (Highscorestr, True, (255, 0, 0)) Bakscreen.blit (Text_screen, (100,340)) Highfile=open (' Hi Ghscore ', ' W ') Highfile.writelines (str (scorenum)) Highfile.close () def gethighscore (): #读取最高分 if Os.path.isfile (' Highscore '): Highfile=open (' Highscore ', ' R ') highscore=highfile.ReadLine () highfile.close () else:highscore=0 return highscore bakscreen=pygame.display.set_mode ([800,60 0]) Bakscreen.fill ([0,160,233]) pygame.display.set_caption (' dig! dig! ') Drawback () levelnum=1 #levelscorenum =0 #得分highscore =gethighscore () #最高分ileft =1 #记录向左移动步数 to control the image iright=10 #记录向右移动步数, Used to control the picture x=100y=450filename= ' 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 () while True:if scorenum>0 and Scorenum/50.0==int (scorenum/50.0 ): #当得分是50的倍数时修改level levelnum=scorenum/50+1 Speed=[0,levelnum] for event in Pygame.event.get (): If event.type= =pygame. QUIT:sys.exit () #make Gold Pressed_keys = pygame.key.get_pressed () if Pressed_keys[pygame.    K_left]: #按下左键 drawback () LoadText (levelnum,scorenum,highscore) if IRight > 14:iright=10 iright=iright+1 Filename= ' ImagE\\ ' +str (iright) + '. png ' if x<50:x=50 else:x=x-10 backimg_surface=rect (filename,[x,y]) bakscree N.blit (Backimg_surface.image,backimg_surface.rect) if Pressed_keys[pygame. K_right]: #按下右键 drawback () LoadText (levelnum,scorenum,highscore) if ILeft > 4:ileft=0 ileft=ileft+1 file    Name= ' image\\ ' +str (ileft) + '. png ' if x>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) If mygold.rect.top>600: #判断金币是否着地, one but the ground, game over Loadgameover (Scorenum, Highscore) if Mygold.rect.colliderect (backimg_surface.rect): #判断金币是否与小人碰撞 If the collision means that the villain receives the gold coins scorenum+=5 loadtext (levelnu M,scorenum,highscore) Goldx=random.randint (50,580) mygold=goldrect ([Goldx,100],speed)  Pygame.display.update () 

The resources used in the program can be downloaded from here: File name: gold.7z, Access address: http://www.kuaipan.cn/file/id_16699292408348719.htm

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.