贪吃蛇完整版
import pygameimport sys
import time
import random
from pygame.locals import *
#
pygame.init()
size = width, height = 350,350
screen = pygame.display.set_mode((size))
t = pygame.time.Clock()
snake_head =
snake_body = [,,,,]
foodx= random.randint(5,30)*10
foody= random.randint(5,30)*10
# fx = "RIGHT"
# while True:
# t.tick(5)
# for event in pygame.event.get():
# if event.type == QUIT:
# pygame.quit()
# sys.exit()
def Fx2():
if event.type == KEYDOWN and event.key == K_LEFT:
# snak_head -=10
global fx
if fx != "RIGHT":
fx ="LEFT"
if event.type == KEYDOWN and event.key == K_RIGHT:
# snak_head -=10
if fx != "LEFT":
fx ="RIGHT"
if event.type == KEYDOWN and event.key == K_DOWN:
# snak_head -=10
if fx != "UP":
fx ="DOWN"
if event.type == KEYDOWN and event.key == K_UP:
# snak_head -=10
if fx != "DOWN":
fx ="UP"
def Move2():
if fx == "RIGHT":
snake_head+=10
if fx == "LEFT":
snake_head-=10
if fx == "DOWN":
snake_head+=10
if fx == "UP":
snake_head-=10
#
# screen.fill((255, 255, 255))
# # snak_head += 10
# print(snak_head)
# snak_body.insert(0,list(snak_head))
# snak_body.pop()
# print(snak_body)
# for i in snak_body:
# pygame.draw.rect(screen,(0,0,0),(i,i,10,10))
#
# pygame.display.update()
fx = "K_d"
def Fx():
if event.type == KEYDOWN and event.key == K_w:
# snake_head-=10
global fx
if fx != "K_s":
fx = "K_w"
if event.type == KEYDOWN and event.key == K_a:
# snake_head -= 10
if fx != "K_d":
fx = "K_a"
if event.type == KEYDOWN and event.key == K_s:
# snake_head+=10
if fx != "K_w":
fx = "K_s"
if event.type == KEYDOWN and event.key == K_d:
# snake_head+=10
if fx != "K_a":
fx = "K_d"
def Rest():
global fx,foody,foodx,snake_head,snake_body,score,kaiguan
while True:
event=pygame.event.poll()
if event.type == QUIT:
pygame.quit()
if event.type == KEYDOWN and event.key == K_RETURN:
fx = "K_d"
snake_head =
snake_body = [, , , , ]
foodx = random.randint(5, 30) * 10
foody = random.randint(5, 30) * 10
score = 0
kaiguan=0
break
def Move():
# global fx
if fx == "K_a":
snake_head-=10
if fx == "K_d":
snake_head+=10
if fx == "K_s":
snake_head+=10
if fx == "K_w":
snake_head-=10
def Fontf(mx,ziti,x,y):
afont = pygame.font.Font(None,40)
bfont = afont.render(ziti,True,(255,0,0))
screen.blit(bfont,(x,y))
pygame.display.update()
def color():
r = random.randint(0,255)
g = random.randint(0, 255)
b = random.randint(0, 255)
color1 = (r,g,b)
return color1
list3 = []
for i in range(5):
colors = color()
list3.append(colors)
print(list3)
n = 0
score = 0
kaiguan = 0
while 1:
n+=1
t.tick(5)
screen.fill((0,0,0))
# colors = color()
# list.append(colors)
# print(colors)
pygame.draw.rect(screen,(255,0,0),(foodx,foody,10,10))
if snake_head == :
foodx = random.randint(5, 30) * 10
foody = random.randint(5, 30) * 10
snake_body.append([-10,-10])
score+=1
Fontf(40,"score:"+str(score),200,0)
if snake_head<=-10 or snake_head>=400 or snake_head<=-10 or snake_head>=400:
kaiguan = 1
if snake_head in snake_body:
kaiguan = 1
if kaiguan:
Fontf(40,"you lose",100,150)
# break
Rest()
# while True:
# event = pygame.event.poll()
# if event.type == QUIT:
# pygame.quit()
# if event.type == KEYDOWN and event.key == K_RETURN:
# fx = "K_d"
# snake_head =
# snake_body = [, , , , ]
#
# foodx = random.randint(5, 30) * 10
# foody = random.randint(5, 30) * 10
# score = 0
# kaiguan =0
# break
for i in snake_body:
pygame.draw.rect(screen,list3, (i, i, 10, 10))
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
Fx()
# if event.type==KEYDOWN and event.key==K_w:
# # snake_head-=10
# if fx !="K_s":
# fx = "K_w"
#
# if event.type == KEYDOWN and event.key == K_a:
# # snake_head -= 10
# if fx !="K_d":
# fx = "K_a"
# if event.type==KEYDOWN and event.key==K_s:
# # snake_head+=10
# if fx !="K_w":
# fx = "K_s"
# if event.type==KEYDOWN and event.key==K_d:
# # snake_head+=10
# if fx !="K_a":
# fx = "K_d"
# if fx == "K_a":
# snake_head-=10
# if fx == "K_d":
# snake_head+=10
# if fx == "K_s":
# snake_head+=10
# if fx == "K_w":
# snake_head-=10
# snake_head += 10
Move()
snake_body.insert(0,list(snake_head))
snake_body.pop()
pygame.display.update()
weizhu1983 发表于 2019-11-25 21:47
我去这是啥啊 牛B
编程语言python开发的贪吃蛇小游戏 只要有环境运行起来就可以玩哦:想要运行必须安装python解释器,或者再安装个工具pycharm等,如果真的感兴趣不妨了解下python再研究
import pygame
import sys
import time
import random
from pygame.locals import *
#
pygame.init()
size = width, height = 350,350
screen = pygame.display.set_mode((size))
t = pygame.time.Clock()
snake_head =
snake_body = [,,,,]
foodx= random.randint(5,30)*10
foody= random.randint(5,30)*10
# fx = "RIGHT"
# while True:
# t.tick(5)
# for event in pygame.event.get():
# if event.type == QUIT:
# pygame.quit()
# sys.exit()
def Fx2():
if event.type == KEYDOWN and event.key == K_LEFT:
# snak_head -=10
global fx
if fx != "RIGHT":
fx ="LEFT"
if event.type == KEYDOWN and event.key == K_RIGHT:
# snak_head -=10
if fx != "LEFT":
fx ="RIGHT"
if event.type == KEYDOWN and event.key == K_DOWN:
# snak_head -=10
if fx != "UP":
fx ="DOWN"
if event.type == KEYDOWN and event.key == K_UP:
# snak_head -=10
if fx != "DOWN":
fx ="UP"
def Move2():
if fx == "RIGHT":
snake_head+=10
if fx == "LEFT":
snake_head-=10
if fx == "DOWN":
snake_head+=10
if fx == "UP":
snake_head-=10
#
# screen.fill((255, 255, 255))
# # snak_head += 10
# print(snak_head)
# snak_body.insert(0,list(snak_head))
# snak_body.pop()
# print(snak_body)
# for i in snak_body:
# pygame.draw.rect(screen,(0,0,0),(i,i,10,10))
#
# pygame.display.update()
fx = "K_d"
def Fx():
if event.type == KEYDOWN and event.key == K_w:
# snake_head-=10
global fx
if fx != "K_s":
fx = "K_w"
if event.type == KEYDOWN and event.key == K_a:
# snake_head -= 10
if fx != "K_d":
fx = "K_a"
if event.type == KEYDOWN and event.key == K_s:
# snake_head+=10
if fx != "K_w":
fx = "K_s"
if event.type == KEYDOWN and event.key == K_d:
# snake_head+=10
if fx != "K_a":
fx = "K_d"
def Rest():
global fx,foody,foodx,snake_head,snake_body,score,kaiguan
while True:
event=pygame.event.poll()
if event.type == QUIT:
pygame.quit()
if event.type == KEYDOWN and event.key == K_RETURN:
fx = "K_d"
snake_head =
snake_body = [, , , , ]
foodx = random.randint(5, 30) * 10
foody = random.randint(5, 30) * 10
score = 0
kaiguan=0
break
def Move():
# global fx
if fx == "K_a":
snake_head-=10
if fx == "K_d":
snake_head+=10
if fx == "K_s":
snake_head+=10
if fx == "K_w":
snake_head-=10
def Fontf(mx,ziti,x,y):
afont = pygame.font.Font(None,40)
bfont = afont.render(ziti,True,(255,0,0))
screen.blit(bfont,(x,y))
pygame.display.update()
def color():
r = random.randint(0,255)
g = random.randint(0, 255)
b = random.randint(0, 255)
color1 = (r,g,b)
return color1
list3 = []
for i in range(5):
colors = color()
list3.append(colors)
print(list3)
n = 0
score = 0
kaiguan = 0
while 1:
n+=1
t.tick(5)
screen.fill((0,0,0))
# colors = color()
# list.append(colors)
# print(colors)
pygame.draw.rect(screen,(255,0,0),(foodx,foody,10,10))
if snake_head == :
foodx = random.randint(5, 30) * 10
foody = random.randint(5, 30) * 10
snake_body.append([-10,-10])
score+=1
Fontf(40,"score:"+str(score),200,0)
if snake_head<=-10 or snake_head>=400 or snake_head<=-10 or snake_head>=400:
kaiguan = 1
if snake_head in snake_body:
kaiguan = 1
if kaiguan:
Fontf(40,"you lose",100,150)
# break
Rest()
# while True:
# event = pygame.event.poll()
# if event.type == QUIT:
# pygame.quit()
# if event.type == KEYDOWN and event.key == K_RETURN:
# fx = "K_d"
# snake_head =
# snake_body = [, , , , ]
#
# foodx = random.randint(5, 30) * 10
# foody = random.randint(5, 30) * 10
# score = 0
# kaiguan =0
# break
for i in snake_body:
pygame.draw.rect(screen,list3, (i, i, 10, 10))
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
Fx()
# if event.type==KEYDOWN and event.key==K_w:
# # snake_head-=10
# if fx !="K_s":
# fx = "K_w"
#
# if event.type == KEYDOWN and event.key == K_a:
# # snake_head -= 10
# if fx !="K_d":
# fx = "K_a"
# if event.type==KEYDOWN and event.key==K_s:
# # snake_head+=10
# if fx !="K_w":
# fx = "K_s"
# if event.type==KEYDOWN and event.key==K_d:
# # snake_head+=10
# if fx !="K_a":
# fx = "K_d"
# if fx == "K_a":
# snake_head-=10
# if fx == "K_d":
# snake_head+=10
# if fx == "K_s":
# snake_head+=10
# if fx == "K_w":
# snake_head-=10
# snake_head += 10
Move()
snake_body.insert(0,list(snake_head))
snake_body.pop()
pygame.display.update()
大神膜拜厉害 无图无真相啊 这个要怎么用的起来? 很赞发给我同学 怎么用的 ?大哥我改了 html bat exe都不行 咋用?这个? 楼主真技术宅,膜拜膜拜,感谢分享! 这个,不会用啊,大神,给个说明吧。
页:
[1]
2