pygame學習筆記(6):完成一個簡單的遊戲

來源:互聯網
上載者:User
學了這麼長時間的Pygame,一直想寫個遊戲實戰一下。看起來很簡單的遊戲,寫其來怎麼這麼難。最初想寫個俄羅斯方塊,想了很長時間如何?,想來想去,也沒寫出來,於是乾脆下載別人的代碼來讀。後來,要想寫一個協助記憶的挖寶箱的遊戲,結果也沒完成。唯一完成了就是下面這個小人接金幣的遊戲,超級簡單,通過左右鍵控制小人移動去接空中下來的金幣,接住金幣得5分,接不住遊戲結束,金幣速度會隨著level的關數而越來越快。完成這段代碼後,我依然覺得這段代碼寫得很差,確實也是自己對pygame只是掌握了皮毛,對surface、sprite這些理解的還不透徹。這裡把代碼寫出來,有時間的大牛們可以協助指點一下,讓我也有所提高。

# -*- coding: cp936 -*-'''一個超級簡單的遊戲左右鍵控制小人移動去接空中下來的金幣,接住金幣得5分,接不住遊戲結束,金幣速度會隨著level的關數而越來越快'''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('image\\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')   bakscreen.blit(my_back,[0,0])    def loadtext(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))  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 scorenum>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('highscore','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,600])bakscreen.fill([0,160,233])pygame.display.set_caption('Dig!Dig!')drawback()levelnum=1 #levelscorenum=0 #得分highscore=gethighscore()#最高分ileft=1 #記錄向左移動步數,用來控製圖片iright=10 #記錄向右移動步數,用來控製圖片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])    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    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:#判斷金幣是否著地,一但著地,遊戲結束    loadgameover(scorenum,highscore)  if mygold.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

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.