使用方向键控制贪吃蛇,有时间再优化。
import pygame
from pygame.sprite import Group,Sprite
import sys
import random
import time
def run_game():
pygame.init()
screen = pygame.display.set_mode((500,500))
pygame.display.set_caption("snake")
#背景色
bg = (80,80,80)
screen.fill(bg)
#图片
img = pygame.image.load("QR.png")
img_rect = img.get_rect() #获取外接矩形
screen_rect = screen.get_rect()
img_rect.centerx = screen_rect.centerx #中心坐标
#img_rect.bottom = screen_rect.bottom #下边缘
#移动步长
step_len = 10
#前进方向
left = 1
top = 2
right = 3
bottom = 4
direction = left
class SnakeItem(Sprite):
len = 0
def __init__(self,x,y,color):
super().__init__()
self.x = x
self.y = y
self.color = color
#蛇
item = SnakeItem(screen_rect.centerx,screen_rect.centery,(255,255,255))
snake = []
snake.append(item)
#食物
def set_food():
while True:
x = 0
y = 0
while True:
x = random.randrange(0,screen_rect.width)
if x % 10 == 0:
break
while True:
y = random.randrange(0,screen_rect.height)
if y % 10 == 0:
break
for i in snake:
if i.x == x and i.y == y:
continue
return SnakeItem(x,y,(255,0,0))
temp_rect = set_food()
while True:
#方向
for event in pygame.event.get():#键盘 鼠标 事件
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
direction = right
elif event.key == pygame.K_LEFT:
direction = left
elif event.key == pygame.K_UP:
direction = top
elif event.key == pygame.K_DOWN:
direction = bottom
#screen.blit(img,img_rect)#在指定位置绘制
#背景
screen.fill(bg)
#食物
rect = pygame.Rect(temp_rect.x,temp_rect.y,10,10)
pygame.draw.rect(screen,temp_rect.color,rect)
#蛇
s = snake[:]
item_head = s[0]
xx = s[0].x
yy = s[0].y
list_x=[-1]
list_y=[-1]
for i in snake:
list_x.append(i.x)
list_y.append(i.y)
list_x = set(list_x)
list_y = set(list_y)
print(len(list_x))
print(len(list_y))
if direction == left:
xx = xx - step_len if xx - step_len > screen_rect.left-10*len(list_x) else screen_rect.left-10*len(list_x)
item_head.x = item_head.x - step_len if item_head.x - step_len > screen_rect.left-10*len(list_x) else screen_rect.left-10*len(list_x)
elif direction == top:
item_head.y = item_head.y - step_len if item_head.y - step_len > screen_rect.top-10*len(list_y) else screen_rect.top-10*len(list_y)
yy = yy -step_len if yy - step_len > screen_rect.top else screen_rect.top
elif direction == right:
xx = xx + step_len if xx + step_len < screen_rect.right -10*len(list_x) else screen_rect.right -10*len(list_x)
item_head.x = item_head.x + step_len if item_head.x + step_len < screen_rect.right-10*len(list_x) else screen_rect.right-10*len(list_x)
elif direction == bottom:
yy = yy + step_len if yy + step_len < screen_rect.bottom -10*len(list_y) else screen_rect.bottom -10*len(list_y)
item_head.y = item_head.y + step_len if item_head.y + step_len < screen_rect.bottom-10*len(list_y) else screen_rect.bottom-10*len(list_y)
#snake.append(item_head) #不知为何snake中的坐标是一样的?
nn = SnakeItem(xx,yy,(255,255,255))
snake.insert(0,nn)
for i in snake:
r = pygame.Rect(i.x,i.y,10,10)
pygame.draw.rect(screen, i.color, r)
pygame.display.flip() #最近绘制的屏幕可见
if item_head.x >= temp_rect.x and item_head.x <= temp_rect.x+10 and item_head.y >= temp_rect.y and item_head.y <= temp_rect.y+10:
temp_rect = set_food()
else:
snake.pop()
time.sleep(0.3)
# print("--------------")
# for i in snake:
# print(i.x,i.y)
run_game()