当前位置: 首页 > 图灵资讯 > 行业资讯> python简易版挖矿小游戏

python简易版挖矿小游戏

来源:图灵python
时间: 2024-10-14 13:35:44

大家好,今天我们来说一说怎么制作一个简易版的挖矿小游戏。

先写初始设置的代码:

importpygame,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列表
foriinrange(5):
x=random.randint(0,610)
y=random.randint(30,590)
gemRect=pygame.Rect(x,y,50,50)
gemRectList.append(gemRect)

是否继续上一次游戏的代码:

whileTrue:
foreventinpygame.event.get():
ifevent.type==pygame.QUIT:
pygame.quit()
sys.exit()
elifevent.type==pygame.KEYDOWN:
ifevent.key==pygame.K_y:
withopen("record.txt","r")asfile:
number=int(file.read())
start=True
elifevent.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))
ifstart==True:
break
pygame.display.update()

把结尾写一下:

t1=time.time()
whileTrue:
foreventinpygame.event.get():
ifevent.type==pygame.QUIT:
pygame.quit()
sys.exit()
elifevent.type==pygame.KEYDOWN:
sound.play()
ifevent.key==pygame.K_UPandheroRect.y>0:
heroRect.y=heroRect.y-40
elifevent.key==pygame.K_DOWNandheroRect.y<560:
heroRect.y=heroRect.y+40
elifevent.key==pygame.K_LEFTandheroRect.x>0:
heroRect.x=heroRect.x-40
elifevent.key==pygame.K_RIGHTandheroRect.x<560:
heroRect.x=heroRect.x+40
t2=time.time()
t=t2-t1#t表示经过的时间
#绘制地图
screen.blit(bgImg,(0,0))
#绘制宝石
forningemRectList:
ifn.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))
#时间结束,游戏结束,保存成绩,显示成绩
ifint(t)>=20:
withopen("record.txt","w")asfile:
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()

完成!

完整代码:

importpygame,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列表
foriinrange(5):
x=random.randint(0,610)
y=random.randint(30,590)
gemRect=pygame.Rect(x,y,50,50)
gemRectList.append(gemRect)

whileTrue:
foreventinpygame.event.get():
ifevent.type==pygame.QUIT:
pygame.quit()
sys.exit()
elifevent.type==pygame.KEYDOWN:
ifevent.key==pygame.K_y:
withopen("record.txt","r")asfile:
number=int(file.read())
start=True
elifevent.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))
ifstart==True:
break
pygame.display.update()

t1=time.time()
whileTrue:
foreventinpygame.event.get():
ifevent.type==pygame.QUIT:
pygame.quit()
sys.exit()
elifevent.type==pygame.KEYDOWN:
sound.play()
ifevent.key==pygame.K_UPandheroRect.y>0:
heroRect.y=heroRect.y-40
elifevent.key==pygame.K_DOWNandheroRect.y<560:
heroRect.y=heroRect.y+40
elifevent.key==pygame.K_LEFTandheroRect.x>0:
heroRect.x=heroRect.x-40
elifevent.key==pygame.K_RIGHTandheroRect.x<560:
heroRect.x=heroRect.x+40
t2=time.time()
t=t2-t1#t表示经过的时间
#绘制地图
screen.blit(bgImg,(0,0))
#绘制宝石
forningemRectList:
ifn.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))
#时间结束,游戏结束,保存成绩,显示成绩
ifint(t)>=20:
withopen("record.txt","w")asfile:
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()