python如何制作简易版挖矿小游戏-创新互联
小编给大家分享一下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如何制作简易版挖矿小游戏有了一定的了解,想了解更多相关知识,欢迎关注创新互联行业资讯频道,感谢各位的阅读!
新闻标题:python如何制作简易版挖矿小游戏-创新互联
本文网址:http://azwzsj.com/article/diojpj.html