用户:
SLZS查看:0 回复:1 评论:0 创建时间:2022-10-21T20:05:51
【作品展示】

【作品介绍】
贪吃蛇
【作品源代码】
import pgzrun
from random import *
size = 20
HEIGHT = size * 100
WIDTH = size * 50
x = 0
snake_head = [15,15]
snake_body = [[15,15],[15,16],[15,17]]
food = [randint(1, 24), randint(1, 24)]
game_state = "ready"
direction = "up"
def draw():
global snake_head,snake_body
if game_state == "ready":
screen.fill("red")
for body in snake_body:
screen.draw.filled_rect(
Rect((250, body[1] * size), (size, size)), "green")
screen.draw.filled_rect(Rect((snake_head[0] * size,snake_head[1] * size),(size,size)),"black")
screen.draw.text("Snakes Subsonic",center = (250,100),color = "black" ,fontsize = 50)
screen.draw.text("Press Space To Start",center = (250,150),color = "black",fontsize = 24)
if game_state == "play":
screen.clear()
screen.fill("green")
for body in snake_body:
screen.draw.filled_rect(Rect((body[0] * size, body[1] * size),(size,size)), "black")
screen.draw.filled_rect(Rect((snake_head[0] * size,snake_head[1] * size),(size,size)),"black")
screen.draw.filled_rect(Rect((food[0] * size, food[1] * size), (size, size)), randint(0, 255))
if game_state == "end":
screen.clear()
screen.fill("green")
screen.draw.text("Your final length:%.0f"%x,center = (250,350),color = "black" ,fontsize = 80)
def update():
global direction,game_state,x,snake_head,snake_body
if game_state == "ready":
if keyboard.space:
game_state = "play"
if game_state == "play":
if keyboard.w and direction != "down":
direction = "up"
if keyboard.a and direction != "right":
direction = "left"
if keyboard.s and direction != "up":
direction = "down"
if keyboard.d and direction != "left":
direction = "right"
if game_state == "end":
snake_head = [15,15]
snake_body = [[15,15],[15,16],[15,17]]
if keyboard.space:
game_state = "play"
x = 0
def move():
global food,x,game_state
if game_state == "play":
if direction == "up":
snake_head[1] -= 1
if direction == "down":
snake_head[1] += 1
if direction == "left":
snake_head[0] -= 1
if direction == "right":
snake_head[0] += 1
if snake_head == food:
food = [randint(1, 24),randint(1, 24)]
x += 1
else:
snake_body.pop()
snake_body.insert(0, list(snake_head))
if snake_head in snake_body[1:]:
game_state = "end"
if snake_head[0] * size < 0 or snake_head[1] * size < 0 or snake_head[0] * size > WIDTH or snake_head[1] * size > HEIGHT:
game_state = "end"
clock.schedule_interval(move,0.2)
pgzrun.go()
【提示】
部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:
https://python.codemao.cn