python简易版挖矿小游戏
大家好,今天我们来说一说怎么制作一个简易版的挖矿小游戏。
先写初始设置的代码:
import pygame, sys, random, timepygame.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, timepygame.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简易版挖矿小游戏 的全部内容, 来源链接: utcz.com/z/541454.html