import tkinter as tk
import pygame
import random
import sys
global rmb
global grade
rmb=0#复活币数量黄色的是rmb
grade=1#初始等级
win =tk.Tk()
win.title("不花点小钱玩蛇")
win.geometry("500x500")#初始界面
md="大爷来玩啊"
I=tk.Label(win,text=md,font=("Arial",20),width=15,
height=3)
I.pack()#
def hit():#主函数
class point:
hang=0
lie=0
def __init__(self,hang,lie):
self.hang=hang
self.lie=lie
def copy(self):
return point(hang=self.hang,lie=self.lie)
def rect(point,color):
left=point.lie*(H/lie)
top=point.hang*(W/hang)
pygame.draw.rect(screen,color,(left,top,W/hang,H/lie))
pygame.init()
#
file="./musice/56.ogg"#需要对应的文件音乐才可以运行
pygame.mixer.init()
track = pygame.mixer.music.load(file)
pygame.mixer.music.play()
#
W=600
H=600
size=(W,H)
hang=40#有多少行
lie=40#有多少列
hang1=W/lie#每小列多高
lie1=H/hang#每小行多宽
scores=0
foodcolor=(0,0,255)#食物颜色蓝
rmbcolor=(255,215,0)#金币颜色
headcolor=(255,0,0)#头颜色红
snakecolor=(255,190,210)
h1=int(W/2)#正中央坐标
h2=int(H/2)#
head=point(hang=int(hang/2),lie=int(lie/2))#头的位置
direct="left"#初始默认方向左
screen=pygame.display.set_mode(size)#创建一个框架
pygame.display.set_caption('贪吃蛇小游戏')
def show_text(screen,pos,text,color,font_bold=False,font_size=60,font_italic=False):
#获取系统文字,并设置其大小。
cur_font =pygame.font.SysFont("宋体",font_size)
#设置是否加粗
cur_font.set_bold(font_bold)
#设置是否斜体
cur_font.set_italic(font_italic)
#设置文字内容
text_fmt = cur_font.render(text,1,color)
#绘制文字
screen.blit(text_fmt,pos)
snakes=[point(hang=head.hang+1,lie=head.lie),point(hang=head.hang+2,lie=head.lie)]
t=True
def food1():#食物的随机分布
m=False
while 1:
po=point(hang=random.randint(0,hang-1),lie=random.randint(0,lie-1))
if head.hang==po.hang and head.lie==po.lie:
m=True
for i in snakes:
if i.hang==po.hang and i.lie==po.lie:
m=True
if m==False:
break
return po
clock=pygame.time.Clock()
food=food1()
rmb1=food1()
while t:#游戏函数的运行
pygame.display.update()
for event in pygame.event.get():
if event.type== pygame.QUIT:
pygame.quit()
exit()
t=False
elif event.type==pygame.KEYDOWN:
if event.key==273 or event.key==119:
if direct=="left" or direct=="right":
direct='up'
elif event.key==274 or event.key==115:
if direct=="left" or direct=="right":
direct='down'
elif event.key==276 or event.key==97 :
if direct=="up" or direct=="down":
direct='left'
elif event.key==275 or event.key==100:
if direct=="up" or direct=="down":
direct='right'
eat=(head.hang==food.hang and head.lie==food.lie)
eat1=(head.hang==rmb1.hang and head.lie==rmb1.lie)
if direct =="left":
head.lie-=1
elif direct=='right':
head.lie+=1
elif direct=='up':
head.hang-=1
elif direct=='down':
head.hang+=1
g=False
if head.hang<0 or head.hang>hang or head.lie<0 or head.lie >lie:
g=True
for i in snakes:
if head.hang==i.hang and head.lie ==i.lie:
g=True
if g:#死亡
wi=tk.Tk()
wi.title("掏钱复活")
wi.geometry("500x500")
nmd=("勇者啊做出你的选择")
l1=tk.Label(wi,text=nmd,font=("Arial",20),width=15,
height=3)
l1.pack()
def hit12():
global rmb
if rmb>0:
rmb-=1
wi.destroy()
hit()
else:
bkk1=tk.Button(wi,text='你身上没有钱',bg='red',width=15,height=5,command=hit123)
bkk1.pack()
def hit123():
bjk=tk.Button(wi,text='从地上捡起一块钱',bg='red',width=15,height=5,command=hitk)
bjk.pack()
def hitk():
global rmb
rmb+=1
def hit22():
exit()
b1=tk.Button(wi,text='掏钱复活',bg='pink',width=15,height=5,command=hit12)
k1=tk.Button(wi,text='没钱认命',width=15,height=5,comman=hit22)
b1.pack()
k1.pack()
wi.mainloop()
clock.tick(0.1)
t=False
pygame.draw.rect(screen,(255,255,255),(0,0,W,H))#设置背景白色
snakes.insert(0,head.copy())
if eat1:
global rmb
global grade
rmb+=1
rmb1=food1()
if not eat:
snakes.pop()
else:
food=food1()
scores+=50
for i in snakes:
rect(i,snakecolor)
rect(head,headcolor)#设置初始头的位置
show_text(screen, (50,500),'Scores:'+ str(scores), (0,0,255))
show_text(screen, (50,50),'RMB:'+ str(rmb), (0,0,255))
show_text(screen, (400,50),'Grade:'+ str(grade), (0,0,255))
rect(food,foodcolor)
rect(rmb1,rmbcolor)
if scores<100:
clock.tick(9)
grade=1
elif scores>=100 and scores<150:
clock.tick(13)
grade=2
elif scores>=150 and scores <300:
clock.tick(15)
grade=3
else:
clock.tick(18)
grade=4
def hit2():
exit()#退出游戏
b=tk.Button(win,text='我有钱来玩蛇',bg='pink',width=15,height=5,command=hit)
b.pack()
k=tk.Button(win,text='我没钱',width=15,height=5,comman=hit2)
k.pack()
win.mainloop()
内容代码原创,游戏可能仍存bug,希望大佬可以指正