Pygame Learning Notes (6): Complete a simple game _python

Source: Internet
Author: User

After learning so long pygame, always want to write a game of combat. It looks like a very simple game, how hard it is to write it. Originally wanted to write a Tetris, think for a long time how to achieve, think about it, also did not write out, so simply download someone else's code to read. Later, to write a help to remember the treasure box of the game, the result is not completed. The only completed is the following this little villain to pick up the game of gold, super simple, through the key to control the movement of small people to pick up the sky down the gold coins, catch gold 5 points, can not catch the game, gold coins speed will be close with the level of the number and faster. Completion of this code, I still feel that this piece of code is very poor, and indeed it is their own pygame just mastered the fur, surface, Sprite These understanding is not thorough. Here the code to write out, there is time for Daniel to help enlighten me, let me also improve.

#-*-coding:cp936-*-' A super simple game around key control movement to pick up the sky down the gold coins, catch the gold coins to 5 points, can not catch the game end, the gold coin speed will be with the level of the number and faster ' import pygame,sys,os,r Andom Pygame.init () class rect (): #画出小人 def __init__ (self,filename,initial_position): Self.image=pygame.image.load (f Ilename) Self.rect=self.image.get_rect () Self.rect.topleft=initial_position class Goldrect (Pygame.sprite.Spri TE): #绘出金币 def __init__ (self,gold_position,speed): pygame.sprite.sprite.__init__ (self) self.image=pygame.image.lo AD (' Image\\gold.png ') self.rect=self.image.get_rect () self.rect.topleft=gold_position self.speed=speed def m Ove (self): Self.rect=self.rect.move (self.speed) def drawback (): #绘出背景图片 my_back=pygame.image.load (' Imag E\\qi3.jpg ') Bakscreen.blit (my_back,[0,0]) def loadtext (Levelnum,score,highscore): #绘出成绩, level, highest score My_font=pyga Me.font.SysFont (none,24) levelstr= ' Level: ' +str (levelnum) Text_screen=my_font.render (Levelstr, True, (255, 0, 0)) Bak Screen.blit (Text_screen, (650,50)) highscorestr= ' Higescore: ' +str (highscore) Text_screen=my_font.render (Highscorestr, True, (255, 0, 0)) Baks Creen.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)) def loadgameover (Scorenum,highscore): #绘出GAME over MY_FONT=PYGAME.FONT.SYSF Ont (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 Scorenum>int (highscore): #写入最高分 highscorestr= ' YOUR HAVE the GOT
    score! ' 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 () def gethighscore (): #读取最高分 if os.path.i Sfile (' HighsCore '): Highfile=open (' Highscore ', ' R ') Highscore=highfile.readline () highfile.close () else:highscore= 0 return Highscore 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 #记录向左移动步数 to control the image iright=10 #记录向右移动步数, using To control the picture 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 () while True:if scorenum>0 and Scorenum/50.0==int (scorenum/50.0): #当得分是50的 Modify level when multiple levelnum=scorenum/50+1 Speed=[0,levelnum] for event in Pygame.event.get (): If Event.type==pyga Me. 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]) Bakscreen. 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 Filename= ' image\\ ' +str (ileft) + '. png ' if x>560:x=560 else:x=x+10 (file Name,[x,y]) bakscreen.blit (backimg_surface.image,backimg_surface.rect) drawback () LoadText (Levelnum,scorenum,high Score) Mygold.move () bakscreen.blit (mygold.image,mygold.rect) backimg_surface=rect (filename,[x,y)) bakscreen.b Lit (backimg_surface.image,backimg_surface.rect) if mygold.rect.top>600: #判断金币是否着地, one to the ground, game over Loadgameover ( Scorenum,highscore) if Mygold.rect.colliderect (backimg_surface.rect): #判断金Whether the coin collides with the villain, if the collision indicates that the villain received the gold coin scorenum+=5 loadtext (levelnum,scorenum,highscore) goldx=random.randint (50,580) Mygol

 D=goldrect ([Goldx,100],speed) Pygame.display.update ()

The resources used by the

program can be downloaded from: 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.