您好,登錄后才能下訂單哦!
小編給大家分享一下python如何制作簡易版挖礦小游戲,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!
先寫初始設置的代碼:
import pygame, sys, random, time pygame.init() screen = pygame.display.set_mode((660, 640)) pygame.display.set_caption("寶石礦工") #標題 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") #背景音樂 pygame.mixer.music.play(-1) heroRect = pygame.Rect(270, 0, 80, 80)
創建寶石的列表:
gemRectList = [] # 寶石列表 number = 0 # 挖到的寶石數量 start = False # False表示不退出第一層循環,True表示退出 #創建寶石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 = "是否繼續上次的游戲成績?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表示經過的時間 # 繪制地圖 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 = "挖到的寶石數:" + str(number) myText = myFont.render(myWord, True, (0, 0, 0)) screen.blit(myText, (420, 10)) myWord = "剩余時間:" + str(int(20 - t)) myText = myFont.render(myWord, True, (0, 0, 0)) screen.blit(myText, (10, 10)) #時間結束,游戲結束,保存成績,顯示成績 if int(t) >= 20: with open("record.txt", "w") as file: file.write(str(number)) screen.blit(gameoverbg, (0, 0)) myWord = "時間到了,你的成績為:" + 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("寶石礦工") #標題 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") #背景音樂 pygame.mixer.music.play(-1) heroRect = pygame.Rect(270, 0, 80, 80) gemRectList = [] # 寶石列表 number = 0 # 挖到的寶石數量 start = False # False表示不退出第一層循環,True表示退出 #創建寶石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 = "是否繼續上次的游戲成績?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表示經過的時間 # 繪制地圖 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 = "挖到的寶石數:" + str(number) myText = myFont.render(myWord, True, (0, 0, 0)) screen.blit(myText, (420, 10)) myWord = "剩余時間:" + str(int(20 - t)) myText = myFont.render(myWord, True, (0, 0, 0)) screen.blit(myText, (10, 10)) #時間結束,游戲結束,保存成績,顯示成績 if int(t) >= 20: with open("record.txt", "w") as file: file.write(str(number)) screen.blit(gameoverbg, (0, 0)) myWord = "時間到了,你的成績為:" + str(number) myText = myFont.render(myWord, True, (0, 0, 0)) screen.blit(myText, (160, 80)) pygame.display.update()
看完了這篇文章,相信你對python如何制作簡易版挖礦小游戲有了一定的了解,想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。