Commit dd26c98e by BellCodeEditor

save project

parent 16f0d60e
Showing with 84 additions and 26 deletions
import pygame import pygame
from pygame import locals import sys
import random
# 初始化pygame,为使用硬件做准备 #初始化pygame
pygame.init() pygame.init()
# 创建一个窗口 #创建一个画布
screen = pygame.display.set_mode((660, 480)) screen = pygame.display.set_mode((800,600))
pygame.display.set_caption('贪吃蛇')#画布名字
cell_size = 20 #网格尺寸
snake_head = [80,80] #蛇头坐标
snake_body = [[80,80],[60,80],[40,80]] #蛇身坐标
direction = 'right'
change_dir = 'right'
food = [200,200]
flag = 1
# 背景 #创建蛇的函数
background = pygame.image.load('bg.png') def draw_snake(body):
right = pygame.image.load('right.png') for i in body:
food = pygame.image.load('apple.png') pygame.draw.rect(screen,(0,0,255),(i[0],i[1],cell_size,cell_size))
body = pygame.image.load('body.png')
#退出程序
while True: while True:
for event in pygame.event.get(): for i in pygame.event.get():
if event.type == locals.QUIT: if i.type == pygame.QUIT:
# 接收到退出事件后退出程序 pygame.quit()
exit() sys.exit()
# 将背景图画上去 if i.type == pygame.KEYDOWN:
screen.blit(background, (0, 0)) if i.key == pygame.K_UP:
# 将贪吃蛇画上去 change_dir = 'up'
screen.blit(right, (240, 120)) if i.key == pygame.K_DOWN:
# 将贪吃蛇的身体画上去 change_dir = 'down'
screen.blit(body, (210, 120)) if i.key == pygame.K_RIGHT:
screen.blit(body, (180, 120)) change_dir = 'right'
screen.blit(body, (180, 90)) if i.key == pygame.K_LEFT:
# 将果实画上去 change_dir = 'left'
screen.blit(food, (360, 300)) #运动方向限制
# 刷新画面 if change_dir == 'right' and direction == 'left':
pygame.display.update() change_dir = 'left'
\ No newline at end of file if change_dir == 'left' and direction == 'right':
change_dir = 'right'
if change_dir == 'up' and direction == 'down':
change_dir = 'down'
if change_dir == 'down' and direction == 'up':
change_dir = 'up'
direction = change_dir
if direction == 'right':
snake_head[0] += cell_size
if direction == 'left':
snake_head[0] -= cell_size
if direction == 'up':
snake_head[1] -= cell_size
if direction == 'down':
snake_head[1] += cell_size
snake_body.insert(0,list(snake_head))
if snake_head == food:
flag = 0
else:
snake_body.pop()
if flag == 0:
x = random.randrange(1,40)
y = random.randrange(1,30)
food = [int(x*cell_size),int(y*cell_size)]
flag = 1
screen.fill((0,0,0))
pygame.time.delay(80)
draw_snake(snake_body) #绘制贪吃蛇
for i in range(0,800,20): #绘制网格
pygame.draw.line(screen,(40,40,40),(i,0),(i,600))
pygame.display.update()#刷新界面
for i in range(0,600,20): #绘制网格
pygame.draw.line(screen,(40,40,40),(0,i),(800,i))
pygame.draw.rect(screen,(255,0,0),(food[0],food[1],cell_size,cell_size))
if snake_head[0] > 800 or snake_head[0] < 0:
pygame.quit()
sys.exit()
if snake_head[1] > 600 or snake_head[1] < 0:
pygame.quit()
sys.exit()
for i in snake_body[1:]:
if snake_head == i:
pygame.quit()
sys.exit()
pygame.display.update()#刷新界面
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment