pygame學(xué)習(xí)筆記(6):完成一個(gè)簡(jiǎn)單的游戲
學(xué)了這么長(zhǎng)時(shí)間的Pygame,一直想寫個(gè)游戲?qū)崙?zhàn)一下??雌饋?lái)很簡(jiǎn)單的游戲,寫其來(lái)怎么這么難。最初想寫個(gè)俄羅斯方塊,想了很長(zhǎng)時(shí)間如何實(shí)現(xiàn),想來(lái)想去,也沒(méi)寫出來(lái),于是干脆下載別人的代碼來(lái)讀。后來(lái),要想寫一個(gè)幫助記憶的挖寶箱的游戲,結(jié)果也沒(méi)完成。唯一完成了就是下面這個(gè)小人接金幣的游戲,超級(jí)簡(jiǎn)單,通過(guò)左右鍵控制小人移動(dòng)去接空中下來(lái)的金幣,接住金幣得5分,接不住游戲結(jié)束,金幣速度會(huì)隨著level的關(guān)數(shù)而越來(lái)越快。完成這段代碼后,我依然覺(jué)得這段代碼寫得很差,確實(shí)也是自己對(duì)pygame只是掌握了皮毛,對(duì)surface、sprite這些理解的還不透徹。這里把代碼寫出來(lái),有時(shí)間的大牛們可以幫助指點(diǎn)一下,讓我也有所提高。
# -*- coding: cp936 -*- ''' 一個(gè)超級(jí)簡(jiǎn)單的游戲 左右鍵控制小人移動(dòng)去接空中下來(lái)的金幣,接住金幣得5分,接不住游戲結(jié)束,金幣速度會(huì)隨著level的關(guān)數(shù) 而越來(lái)越快 ''' import pygame,sys,os,random pygame.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):#繪出成績(jī)、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 #level scorenum=0 #得分 highscore=gethighscore()#最高分 ileft=1 #記錄向左移動(dòng)步數(shù),用來(lái)控制圖片 iright=10 #記錄向右移動(dòng)步數(shù),用來(lái)控制圖片 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):#當(dāng)?shù)梅质?0的倍數(shù)時(shí)修改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:#判斷金幣是否著地,一但著地,游戲結(jié)束 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, 訪問(wèn)地址:http://www.kuaipan.cn/file/id_16699292408348719.htm
- pygame游戲之旅 游戲中添加顯示文字
- pygame游戲之旅 python和pygame安裝教程
- python和pygame實(shí)現(xiàn)簡(jiǎn)單俄羅斯方塊游戲
- Python使用pygame模塊編寫俄羅斯方塊游戲的代碼實(shí)例
- Python基于pygame實(shí)現(xiàn)的font游戲字體(附源碼)
- python基于pygame實(shí)現(xiàn)響應(yīng)游戲中事件的方法(附源碼)
- 用Python寫飛機(jī)大戰(zhàn)游戲之pygame入門(4):獲取鼠標(biāo)的位置及運(yùn)動(dòng)
- pygame實(shí)現(xiàn)雷電游戲雛形開(kāi)發(fā)
相關(guān)文章
利用Python實(shí)現(xiàn)微信找房機(jī)器人實(shí)例教程
這篇文章主要給大家介紹了關(guān)于如何利用Python實(shí)現(xiàn)微信找房機(jī)器人的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用python具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03python進(jìn)程池的簡(jiǎn)單實(shí)現(xiàn)
本文主要介紹了python進(jìn)程池的簡(jiǎn)單實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03python實(shí)現(xiàn)鍵盤輸入的實(shí)操方法
在本篇文章里小編給各位分享了關(guān)于python怎么實(shí)現(xiàn)鍵盤輸入的圖文步驟以及相關(guān)知識(shí)點(diǎn)內(nèi)容,需要的朋友們參考下。2019-07-07pycharm配置Anaconda虛擬環(huán)境全過(guò)程
這篇文章主要介紹了pycharm配置Anaconda虛擬環(huán)境全過(guò)程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01pycharm實(shí)現(xiàn)設(shè)置自動(dòng)的參數(shù)注釋標(biāo)識(shí)
這篇文章主要介紹了pycharm實(shí)現(xiàn)設(shè)置自動(dòng)的參數(shù)注釋標(biāo)識(shí),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02Python裝飾器的應(yīng)用場(chǎng)景及實(shí)例用法
在本篇內(nèi)容里小編給大家整理了一篇關(guān)于Python裝飾器的應(yīng)用場(chǎng)景及實(shí)例用法,有興趣的朋友們可以學(xué)習(xí)下。2021-04-04深入了解和應(yīng)用Python 裝飾器 @decorator
在編程過(guò)程中,經(jīng)常遇到這樣的場(chǎng)景:登錄校驗(yàn),權(quán)限校驗(yàn),日志記錄等,這些功能代碼在各個(gè)環(huán)節(jié)都可能需要,但又十分雷同,通過(guò)裝飾器來(lái)抽象、剝離這部分代碼可以很好解決這類場(chǎng)景,這篇文章主要介紹了Python的裝飾器 @decorator,探討了使用的方式,需要的朋友可以參考下2019-04-04tensorflow使用CNN分析mnist手寫體數(shù)字?jǐn)?shù)據(jù)集
這篇文章主要介紹了tensorflow使用CNN分析mnist手寫體數(shù)字?jǐn)?shù)據(jù)集,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06