您好,登錄后才能下訂單哦!
小編給大家分享一下python如何制作簡(jiǎn)易版挖礦小游戲,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!
先寫初始設(shè)置的代碼:
import pygame, sys, random, time pygame.init() screen = pygame.display.set_mode((660, 640)) pygame.display.set_caption("寶石礦工") #標(biāo)題 gemImg = pygame.image.load("gem.png") #寶石 heroImg = pygame.image.load("hero.png") #挖礦者 gameoverbg = pygame.image.load("gameover.png") #失敗提示 bgImg = pygame.image.load("bg.png") #背景 myFont = pygame.font.SysFont("kaiti", 30) sound = pygame.mixer.Sound("moveMusic.wav") pygame.mixer.music.load("bgMusic.mp3") #背景音樂(lè) pygame.mixer.music.play(-1) heroRect = pygame.Rect(270, 0, 80, 80)
創(chuàng)建寶石的列表:
gemRectList = [] # 寶石列表 number = 0 # 挖到的寶石數(shù)量 start = False # False表示不退出第一層循環(huán),True表示退出 #創(chuàng)建寶石Rect列表 for i in range(5): x = random.randint(0, 610) y = random.randint(30, 590) gemRect = pygame.Rect(x, y, 50, 50) gemRectList.append(gemRect)
是否繼續(xù)上一次游戲的代碼:
while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_y: with open("record.txt","r") as file: number = int(file.read()) start = True elif event.key == pygame.K_n: start = True screen.fill((0, 0, 0)) myWord = "是否繼續(xù)上次的游戲成績(jī)?y/n" text = myFont.render(myWord, True, (255, 0, 0)) screen.blit(text, (130, 280)) if start==True: break pygame.display.update()
把結(jié)尾寫一下:
t1 = time.time() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() elif event.type == pygame.KEYDOWN: sound.play() if event.key == pygame.K_UP and heroRect.y > 0: heroRect.y = heroRect.y - 40 elif event.key == pygame.K_DOWN and heroRect.y < 560: heroRect.y = heroRect.y + 40 elif event.key == pygame.K_LEFT and heroRect.x > 0: heroRect.x = heroRect.x - 40 elif event.key == pygame.K_RIGHT and heroRect.x < 560: heroRect.x = heroRect.x + 40 t2 = time.time() t = t2 - t1 # t表示經(jīng)過(guò)的時(shí)間 # 繪制地圖 screen.blit(bgImg, (0, 0)) # 繪制寶石 for n in gemRectList: if n.colliderect(heroRect): number = number + 1 n.x = random.randint(0, 610) n.y = random.randint(40, 590) screen.blit(gemImg, n) # 繪制英雄 screen.blit(heroImg, heroRect) # 繪制字體 myWord = "挖到的寶石數(shù):" + str(number) myText = myFont.render(myWord, True, (0, 0, 0)) screen.blit(myText, (420, 10)) myWord = "剩余時(shí)間:" + str(int(20 - t)) myText = myFont.render(myWord, True, (0, 0, 0)) screen.blit(myText, (10, 10)) #時(shí)間結(jié)束,游戲結(jié)束,保存成績(jī),顯示成績(jī) if int(t) >= 20: with open("record.txt", "w") as file: file.write(str(number)) screen.blit(gameoverbg, (0, 0)) myWord = "時(shí)間到了,你的成績(jī)?yōu)?" + str(number) myText = myFont.render(myWord, True, (0, 0, 0)) screen.blit(myText, (160, 80)) pygame.display.update()
完成!
完整代碼:
import pygame, sys, random, time pygame.init() screen = pygame.display.set_mode((660, 640)) pygame.display.set_caption("寶石礦工") #標(biāo)題 gemImg = pygame.image.load("gem.png") #寶石 heroImg = pygame.image.load("hero.png") #挖礦者 gameoverbg = pygame.image.load("gameover.png") #失敗提示 bgImg = pygame.image.load("bg.png") #背景 myFont = pygame.font.SysFont("kaiti", 30) sound = pygame.mixer.Sound("moveMusic.wav") pygame.mixer.music.load("bgMusic.mp3") #背景音樂(lè) pygame.mixer.music.play(-1) heroRect = pygame.Rect(270, 0, 80, 80) gemRectList = [] # 寶石列表 number = 0 # 挖到的寶石數(shù)量 start = False # False表示不退出第一層循環(huán),True表示退出 #創(chuàng)建寶石Rect列表 for i in range(5): x = random.randint(0, 610) y = random.randint(30, 590) gemRect = pygame.Rect(x, y, 50, 50) gemRectList.append(gemRect) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_y: with open("record.txt","r") as file: number = int(file.read()) start = True elif event.key == pygame.K_n: start = True screen.fill((0, 0, 0)) myWord = "是否繼續(xù)上次的游戲成績(jī)?y/n" text = myFont.render(myWord, True, (255, 0, 0)) screen.blit(text, (130, 280)) if start==True: break pygame.display.update() t1 = time.time() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() elif event.type == pygame.KEYDOWN: sound.play() if event.key == pygame.K_UP and heroRect.y > 0: heroRect.y = heroRect.y - 40 elif event.key == pygame.K_DOWN and heroRect.y < 560: heroRect.y = heroRect.y + 40 elif event.key == pygame.K_LEFT and heroRect.x > 0: heroRect.x = heroRect.x - 40 elif event.key == pygame.K_RIGHT and heroRect.x < 560: heroRect.x = heroRect.x + 40 t2 = time.time() t = t2 - t1 # t表示經(jīng)過(guò)的時(shí)間 # 繪制地圖 screen.blit(bgImg, (0, 0)) # 繪制寶石 for n in gemRectList: if n.colliderect(heroRect): number = number + 1 n.x = random.randint(0, 610) n.y = random.randint(40, 590) screen.blit(gemImg, n) # 繪制英雄 screen.blit(heroImg, heroRect) # 繪制字體 myWord = "挖到的寶石數(shù):" + str(number) myText = myFont.render(myWord, True, (0, 0, 0)) screen.blit(myText, (420, 10)) myWord = "剩余時(shí)間:" + str(int(20 - t)) myText = myFont.render(myWord, True, (0, 0, 0)) screen.blit(myText, (10, 10)) #時(shí)間結(jié)束,游戲結(jié)束,保存成績(jī),顯示成績(jī) if int(t) >= 20: with open("record.txt", "w") as file: file.write(str(number)) screen.blit(gameoverbg, (0, 0)) myWord = "時(shí)間到了,你的成績(jī)?yōu)?" + str(number) myText = myFont.render(myWord, True, (0, 0, 0)) screen.blit(myText, (160, 80)) pygame.display.update()
看完了這篇文章,相信你對(duì)python如何制作簡(jiǎn)易版挖礦小游戲有了一定的了解,想了解更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。