最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

pygame學(xué)習(xí)筆記(6):完成一個(gè)簡單的游戲

 更新時(shí)間:2015年04月15日 09:44:26   投稿:junjie  
這篇文章主要介紹了pygame學(xué)習(xí)筆記(6):完成一個(gè)簡單的游戲,本文綜合了學(xué)習(xí)過的知識,完成一個(gè)簡單的游戲開發(fā),是本系列文章的最后一篇,需要的朋友可以參考下

學(xué)了這么長時(shí)間的Pygame,一直想寫個(gè)游戲?qū)崙?zhàn)一下。看起來很簡單的游戲,寫其來怎么這么難。最初想寫個(gè)俄羅斯方塊,想了很長時(shí)間如何實(shí)現(xiàn),想來想去,也沒寫出來,于是干脆下載別人的代碼來讀。后來,要想寫一個(gè)幫助記憶的挖寶箱的游戲,結(jié)果也沒完成。唯一完成了就是下面這個(gè)小人接金幣的游戲,超級簡單,通過左右鍵控制小人移動去接空中下來的金幣,接住金幣得5分,接不住游戲結(jié)束,金幣速度會隨著level的關(guān)數(shù)而越來越快。完成這段代碼后,我依然覺得這段代碼寫得很差,確實(shí)也是自己對pygame只是掌握了皮毛,對surface、sprite這些理解的還不透徹。這里把代碼寫出來,有時(shí)間的大牛們可以幫助指點(diǎn)一下,讓我也有所提高。

 

# -*- coding: cp936 -*-
'''
一個(gè)超級簡單的游戲
左右鍵控制小人移動去接空中下來的金幣,接住金幣得5分,接不住游戲結(jié)束,金幣速度會隨著level的關(guān)數(shù)
而越來越快
'''
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):#繪出成績、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 #記錄向左移動步數(shù),用來控制圖片
iright=10 #記錄向右移動步數(shù),用來控制圖片
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, 訪問地址:http://www.kuaipan.cn/file/id_16699292408348719.htm

相關(guān)文章

  • python PyQt5中QRadioButton的詳細(xì)使用教程與應(yīng)用實(shí)戰(zhàn)

    python PyQt5中QRadioButton的詳細(xì)使用教程與應(yīng)用實(shí)戰(zhàn)

    PyQt5是一個(gè)跨平臺的GUI工具包,用于創(chuàng)建具有Python綁定的Qt應(yīng)用程序,在PyQt5中,QRadioButton是一個(gè)非常有用的控件,用于在用戶界面上提供單選選項(xiàng),本文將詳細(xì)介紹QRadioButton的基本用法、常用屬性和方法,需要的朋友可以參考下
    2024-08-08
  • python3使用騰訊企業(yè)郵箱發(fā)送郵件的實(shí)例

    python3使用騰訊企業(yè)郵箱發(fā)送郵件的實(shí)例

    今天小編就為大家分享一篇python3使用騰訊企業(yè)郵箱發(fā)送郵件的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-06-06
  • 跟老齊學(xué)Python之Import 模塊

    跟老齊學(xué)Python之Import 模塊

    本文主要講解了基本的import模塊方法,首先從模塊的定義入手,接著講訴如何自己編寫模塊,非常簡單實(shí)用,有需要的朋友可以參考下
    2014-10-10
  • 九步學(xué)會Python裝飾器

    九步學(xué)會Python裝飾器

    這篇文章主要介紹了Python裝飾器的用法,以實(shí)例形式較為詳細(xì)的介紹了Python裝飾器的使用方法,需要的朋友可以參考下
    2015-05-05
  • Python制作旋轉(zhuǎn)花燈祝大家元宵節(jié)快樂(實(shí)例代碼)

    Python制作旋轉(zhuǎn)花燈祝大家元宵節(jié)快樂(實(shí)例代碼)

    一年一度的元宵節(jié)來臨,小編在這里祝大家2022元宵節(jié)快樂,今天小編給大家分享一篇教程關(guān)于Python制作旋轉(zhuǎn)花燈祝大家元宵節(jié)快樂,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2022-02-02
  • Python生成任意波形并存為txt的實(shí)現(xiàn)

    Python生成任意波形并存為txt的實(shí)現(xiàn)

    本文主要介紹了Python生成任意波形并存為txt的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • 詳解python基礎(chǔ)之while循環(huán)及if判斷

    詳解python基礎(chǔ)之while循環(huán)及if判斷

    這篇文章主要介紹了python基礎(chǔ)之while循環(huán)及if判斷的相關(guān)資料,需要的朋友可以參考下
    2017-08-08
  • 一文搞懂Python的hasattr()、getattr()、setattr()?函數(shù)用法

    一文搞懂Python的hasattr()、getattr()、setattr()?函數(shù)用法

    python中的getattr()、setattr()、hasattr()函數(shù)均是對類屬性或方法的操作,其中g(shù)etattr()用于獲取類或?qū)嵗兄付ǚ椒ǐ@取屬性的值,setattr()用于設(shè)置類或?qū)嵗袑傩曰蚍椒?hasattr()用于判斷類或?qū)嵗惺欠翊嬖谥付ǖ膶傩曰蚍椒?本文通過例子給大家詳解,一起看看吧
    2022-04-04
  • 常用的10個(gè)Python實(shí)用小技巧

    常用的10個(gè)Python實(shí)用小技巧

    這篇文章主要介紹了常用的10個(gè)Python實(shí)用小技巧,幫助大家更好的理解和學(xué)習(xí)Python,感興趣的朋友可以了解下
    2020-08-08
  • django rest framework使用django-filter用法

    django rest framework使用django-filter用法

    這篇文章主要介紹了django rest framework使用django-filter用法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07

最新評論

北碚区| 泉州市| 芜湖市| 张家口市| 青龙| 阿拉善右旗| 大冶市| 顺平县| 富源县| 哈尔滨市| 论坛| 三门峡市| 临城县| 沙雅县| 瓦房店市| 清镇市| 靖宇县| 林州市| 仁布县| 鹰潭市| 云南省| 民勤县| 怀来县| 保德县| 泗洪县| 新泰市| 仁寿县| 高要市| 康马县| 昌都县| 黄石市| 如皋市| 淄博市| 武冈市| 囊谦县| 西宁市| 日喀则市| 左贡县| 沂源县| 海安县| 班玛县|