|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
import pygame,sys,time,random,math
import collections
import copy
import time
screen_width,screen_height=(600,600)
rect_color=(255,255,255)
snake_color=(100,100,100)
food_color=(50,50,50)
bgcolor=(0,0,0)
direction='up'
last_direction='up'
snake=collections.deque()
def draw_screen(screen,rect_color):
for y in range(30):
for x in range(30):
pygame.draw.rect(screen,rect_color,((20*y,20*x),(19,19)),0)
def init_snake(snake):
snake.appendleft(pygame.Rect((80, 80),(19, 19)))
snake.appendleft(pygame.Rect((80, 100), (19, 19)))
snake.appendleft(pygame.Rect((80, 120), (19, 19)))
return snake
def draw_snake(screen,snake_color,snake):
snake_copy=copy.deepcopy(snake)
while snake_copy:
pygame.draw.rect(screen,snake_color,snake_copy.pop(),0)
def create_food(snake):
food_x=random.randrange(0,600,20)
food_y=random.randrange(0,600,20)
food_rect=pygame.Rect(((food_x, food_y),(19, 19)))
snake_copy = copy.deepcopy(snake)
while snake_copy:
invalid_food=snake_copy.pop()
if food_rect==invalid_food:
food_x = random.randrange(0, 600, 20)
food_y = random.randrange(0, 600, 20)
food_rect = pygame.Rect(((food_x, food_y), (19, 19)))
return food_rect
def draw_food(screen,food_color,food_rect):
pygame.draw.rect(screen,food_color,food_rect,0)
def snake_auto_move(snake,direction):
if direction=='up' :
rect= copy.deepcopy(snake[len(snake) - 1]) #小心浅复制
rect.top-=20
snake.popleft()
snake.append(rect)
if direction == 'down':
rect = copy.deepcopy(snake[len(snake) - 1])
rect.top += 20
snake.popleft()
snake.append(rect)
if direction == 'right':
rect = copy.deepcopy(snake[len(snake) - 1])
rect.left += 20
snake.popleft()
snake.append(rect)
if direction == 'left':
rect = copy.deepcopy(snake[len(snake) - 1])
rect.left -= 20
snake.popleft()
snake.append(rect)
def game_over():
print('you lose')
pygame.quit()
sys.exit()
def move_conflit(direction,last_direction):
if (direction=='up' and last_direction=='down') or (direction=='down' and last_direction=='up') :
game_over()
if (direction=='left' and last_direction=='right') or (direction=='right' and last_direction=='left'):
game_over()
def add_snake(snake,direction):
if direction=='up':
rect=copy.deepcopy(snake[0])
rect.top+=20
snake.appendleft(rect)
if direction=='down':
rect=copy.deepcopy(snake[0])
rect.top-=20
snake.appendleft(rect)
if direction=='right':
rect=copy.deepcopy(snake[0])
rect.left-=20
snake.appendleft(rect)
if direction=='left':
rect=copy.deepcopy(snake[0])
rect.left+=20
snake.appendleft(rect)
def get_food(snake,food_rect,direction):
if snake[len(snake)-1]==food_rect:
add_snake(snake,direction)
food_rect=create_food(snake)
return food_rect
def collision_snake(snake):
if snake[len(snake)-1].left<0 or snake[len(snake)-1].left>screen_width:
game_over()
if snake[len(snake)-1].top<0 or snake[len(snake)-1].left>screen_height:
game_over()
def difficulty_choose():
print('选择难度')
print('{},{},{}'.format('a:简单','b:中等','c:困难'))
choice=input()
if choice=='a':
return 0.2
elif choice=='b':
return 0.15
elif choice=='c':
return 0.1
else:
print('叫你乱按,席勒')
return 0
def difficulty_uplift(snake,speed):
difficuty_limit=0.005
if not (len(snake)%3) and (speed>difficuty_limit):
speed-=0.0004
return speed
food_rect=create_food(snake)
last_time_piece_move=time.time()
speed=difficulty_choose()
pygame.init()
screen=pygame.display.set_mode((screen_width,screen_height))
pygame.display.set_caption('贪吃蛇')
screen.fill(bgcolor)
time.sleep(2)
init_snake(snake)
while True:
screen.fill(bgcolor)
draw_screen(screen,rect_color)
draw_snake(screen,snake_color,snake)
draw_food(screen,food_color,food_rect)
pygame.display.update()
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
sys.exit()
if event.type==pygame.KEYDOWN:
if event.key==pygame.K_UP:
direction='up'
if event.key == pygame.K_DOWN:
direction='down'
if event.key==pygame.K_LEFT:
direction='left'
if event.key==pygame.K_RIGHT:
direction='right'
if time.time()-last_time_piece_move>speed:
difficulty_uplift(snake,speed)
move_conflit(direction,last_direction)
collision_snake(snake)
snake_auto_move(snake,direction)
food_rect=get_food(snake,food_rect,direction)
last_direction=direction
last_time_piece_move=time.time()
|
|