格式化
This commit is contained in:
parent
db85f0a0ce
commit
67e05a0964
@ -59,69 +59,80 @@ def show_end(settings, screen):
|
|||||||
pygame.time.wait(1500)
|
pygame.time.wait(1500)
|
||||||
settings.game_mode = 0
|
settings.game_mode = 0
|
||||||
|
|
||||||
|
|
||||||
def draw_grid(settings, screen):
|
def draw_grid(settings, screen):
|
||||||
#绘制横向线
|
# 绘制横向线
|
||||||
for x in range(0, settings.width, settings.cell_size):
|
for x in range(0, settings.width, settings.cell_size):
|
||||||
pygame.draw.line(screen,(40,40,40),(x,0),(x,settings.height))
|
pygame.draw.line(screen, (40, 40, 40), (x, 0), (x, settings.height))
|
||||||
#绘制竖向线
|
# 绘制竖向线
|
||||||
for y in range(0, settings.height, settings.cell_size):
|
for y in range(0, settings.height, settings.cell_size):
|
||||||
pygame.draw.line(screen,(40,40,40),(0, y),(settings.width,y))
|
pygame.draw.line(screen, (40, 40, 40), (0, y), (settings.width, y))
|
||||||
|
|
||||||
|
|
||||||
def update_screen(settings, screen):
|
def update_screen(settings, screen):
|
||||||
screen.fill(settings.bg_color)
|
screen.fill(settings.bg_color)
|
||||||
draw_grid(settings, screen)
|
draw_grid(settings, screen)
|
||||||
|
|
||||||
#绘制蛇
|
# 绘制蛇
|
||||||
|
|
||||||
|
|
||||||
def draw_snake(ai_settings, screen, snake):
|
def draw_snake(ai_settings, screen, snake):
|
||||||
#头部用白色
|
# 头部用白色
|
||||||
x = snake.coords[0]['x'] * ai_settings.cell_size
|
x = snake.coords[0]['x'] * ai_settings.cell_size
|
||||||
y = snake.coords[0]['y'] * ai_settings.cell_size
|
y = snake.coords[0]['y'] * ai_settings.cell_size
|
||||||
snake_head_rect = pygame.Rect(x, y, ai_settings.cell_size, ai_settings.cell_size)
|
snake_head_rect = pygame.Rect(
|
||||||
pygame.draw.rect(screen,(255,255,255), snake_head_rect)
|
x, y, ai_settings.cell_size, ai_settings.cell_size)
|
||||||
#蛇身内部用浅绿,外框用深绿
|
pygame.draw.rect(screen, (255, 255, 255), snake_head_rect)
|
||||||
|
# 蛇身内部用浅绿,外框用深绿
|
||||||
for coord in snake.coords[1: -1]:
|
for coord in snake.coords[1: -1]:
|
||||||
x = coord['x'] * ai_settings.cell_size
|
x = coord['x'] * ai_settings.cell_size
|
||||||
y = coord['y'] * ai_settings.cell_size
|
y = coord['y'] * ai_settings.cell_size
|
||||||
snake_part_rect = pygame.Rect(x, y, ai_settings.cell_size, ai_settings.cell_size)
|
snake_part_rect = pygame.Rect(
|
||||||
pygame.draw.rect(screen,(0,155,0), snake_part_rect)
|
x, y, ai_settings.cell_size, ai_settings.cell_size)
|
||||||
snake_part_inner_rect = pygame.Rect(x + 4, y + 4, ai_settings.cell_size - 8, ai_settings.cell_size - 8)
|
pygame.draw.rect(screen, (0, 155, 0), snake_part_rect)
|
||||||
pygame.draw.rect(screen,(0,255,0), snake_part_inner_rect)
|
snake_part_inner_rect = pygame.Rect(
|
||||||
#蛇尾用浅绿
|
x + 4, y + 4, ai_settings.cell_size - 8, ai_settings.cell_size - 8)
|
||||||
|
pygame.draw.rect(screen, (0, 255, 0), snake_part_inner_rect)
|
||||||
|
# 蛇尾用浅绿
|
||||||
coord = snake.coords[-1]
|
coord = snake.coords[-1]
|
||||||
x = coord['x'] * ai_settings.cell_size
|
x = coord['x'] * ai_settings.cell_size
|
||||||
y = coord['y'] * ai_settings.cell_size
|
y = coord['y'] * ai_settings.cell_size
|
||||||
snake_tail_rect = pygame.Rect(x, y, ai_settings.cell_size, ai_settings.cell_size)
|
snake_tail_rect = pygame.Rect(
|
||||||
pygame.draw.rect(screen,(0,255,0), snake_tail_rect)
|
x, y, ai_settings.cell_size, ai_settings.cell_size)
|
||||||
|
pygame.draw.rect(screen, (0, 255, 0), snake_tail_rect)
|
||||||
|
|
||||||
|
# 绘制游戏界面
|
||||||
|
|
||||||
|
|
||||||
#绘制游戏界面
|
|
||||||
def update_screen(settings, screen, snake):
|
def update_screen(settings, screen, snake):
|
||||||
screen.fill(settings.bg_color)
|
screen.fill(settings.bg_color)
|
||||||
draw_grid(settings, screen)
|
draw_grid(settings, screen)
|
||||||
#移动蛇
|
# 移动蛇
|
||||||
del snake.coords[-1]#加蛇头
|
del snake.coords[-1] # 加蛇头
|
||||||
snake.update()#去蛇尾
|
snake.update() # 去蛇尾
|
||||||
if not is_game_over(settings, snake):
|
if not is_game_over(settings, snake):
|
||||||
settings.game_mode = -1
|
settings.game_mode = -1
|
||||||
else:
|
else:
|
||||||
draw_snake(settings, screen, snake)
|
draw_snake(settings, screen, snake)
|
||||||
pygame.display.flip()
|
pygame.display.flip()
|
||||||
#暂停一下
|
# 暂停一下
|
||||||
settings.my_clock.tick(settings.clock_frq)
|
settings.my_clock.tick(settings.clock_frq)
|
||||||
|
|
||||||
def is_game_over(settings,snake):
|
|
||||||
#碰到左右墙壁
|
def is_game_over(settings, snake):
|
||||||
|
# 碰到左右墙壁
|
||||||
if (snake.coords[snake.head_index]['x'] == -1 or snake.coords[snake.head_index]['x'] == settings.cell_w):
|
if (snake.coords[snake.head_index]['x'] == -1 or snake.coords[snake.head_index]['x'] == settings.cell_w):
|
||||||
return False
|
return False
|
||||||
#碰到上下墙壁
|
# 碰到上下墙壁
|
||||||
if(snake.coords[snake.head_index]['y'] == -1 or snake.coords[snake.head_index]['y'] == settings.cell_h):
|
if (snake.coords[snake.head_index]['y'] == -1 or snake.coords[snake.head_index]['y'] == settings.cell_h):
|
||||||
return False
|
return False
|
||||||
#碰到自己
|
# 碰到自己
|
||||||
if(snake.coords[snake.head_index] in snake.coords[1:]):
|
if (snake.coords[snake.head_index] in snake.coords[1:]):
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
#检测游戏过程中的按键
|
# 检测游戏过程中的按键
|
||||||
def check_play_events(snake):
|
def check_play_events(snake):
|
||||||
for event in pygame.event.get():
|
for event in pygame.event.get():
|
||||||
if event.type == pygame.QUIT:
|
if event.type == pygame.QUIT:
|
||||||
|
13
main.py
13
main.py
@ -4,27 +4,30 @@ import game_functions as gf
|
|||||||
from snake import Snake
|
from snake import Snake
|
||||||
|
|
||||||
settings = Settings()
|
settings = Settings()
|
||||||
#设置屏幕
|
# 设置屏幕
|
||||||
screen = pygame.display.set_mode((settings.width, settings.height))
|
screen = pygame.display.set_mode((settings.width, settings.height))
|
||||||
#添加游戏标题
|
# 添加游戏标题
|
||||||
pygame.display.set_caption('贪吃蛇')
|
pygame.display.set_caption('贪吃蛇')
|
||||||
#创建蛇
|
# 创建蛇
|
||||||
snake = Snake(settings)
|
snake = Snake(settings)
|
||||||
|
|
||||||
|
|
||||||
def run_game1():
|
def run_game1():
|
||||||
gf.check_play_events(snake)
|
gf.check_play_events(snake)
|
||||||
gf.update_screen(settings, screen, snake)
|
gf.update_screen(settings, screen, snake)
|
||||||
|
|
||||||
|
|
||||||
def run_game():
|
def run_game():
|
||||||
while True:
|
while True:
|
||||||
if settings.game_mode == 0:
|
if settings.game_mode == 0:
|
||||||
gf.show_start(settings,screen)
|
gf.show_start(settings, screen)
|
||||||
elif settings.game_mode == -1:
|
elif settings.game_mode == -1:
|
||||||
gf.show_end(settings,screen)
|
gf.show_end(settings, screen)
|
||||||
snake.reset(settings)
|
snake.reset(settings)
|
||||||
elif settings.game_mode == 1:
|
elif settings.game_mode == 1:
|
||||||
run_game1()
|
run_game1()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
pygame.init()
|
pygame.init()
|
||||||
run_game()
|
run_game()
|
@ -1,6 +1,7 @@
|
|||||||
import pygame
|
import pygame
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
|
||||||
class Settings:
|
class Settings:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.bg_color = (0, 0, 0) # 背景颜色
|
self.bg_color = (0, 0, 0) # 背景颜色
|
||||||
|
47
snake.py
47
snake.py
@ -1,38 +1,39 @@
|
|||||||
import random
|
import random
|
||||||
|
|
||||||
|
|
||||||
class Snake():
|
class Snake():
|
||||||
def __init__(self,settings):
|
def __init__(self, settings):
|
||||||
self.reset(settings)
|
self.reset(settings)
|
||||||
|
|
||||||
#蛇的初始化
|
# 蛇的初始化
|
||||||
def reset(self,settings):
|
def reset(self, settings):
|
||||||
#蛇头的坐标
|
# 蛇头的坐标
|
||||||
self.start_x = random.randint(5, settings.cell_w - 6)
|
self.start_x = random.randint(5, settings.cell_w - 6)
|
||||||
self.start_y = random.randint(5, settings.cell_h - 6)
|
self.start_y = random.randint(5, settings.cell_h - 6)
|
||||||
self.head_index = 0
|
self.head_index = 0
|
||||||
#蛇的初始运动方向
|
# 蛇的初始运动方向
|
||||||
self.direction = 'right'
|
self.direction = 'right'
|
||||||
#蛇的初始坐标字典,初始蛇为蛇头位置及其左边的两个格子
|
# 蛇的初始坐标字典,初始蛇为蛇头位置及其左边的两个格子
|
||||||
self.coords = [{'x' : self.start_x, 'y': self.start_y},
|
self.coords = [{'x': self.start_x, 'y': self.start_y},
|
||||||
{'x' : self.start_x - 1, 'y': self.start_y},
|
{'x': self.start_x - 1, 'y': self.start_y},
|
||||||
{'x' : self.start_x - 2, 'y': self.start_y}]
|
{'x': self.start_x - 2, 'y': self.start_y}]
|
||||||
#更新蛇,每次蛇移动一步,因此相当于每次移动在蛇的坐标字典中再插入一个新蛇头
|
# 更新蛇,每次蛇移动一步,因此相当于每次移动在蛇的坐标字典中再插入一个新蛇头
|
||||||
#按常识,每次移动后,不仅仅是蛇头向前移动一个,更重要的是蛇尾也要向前移,但是在该函数中先不处理蛇尾
|
# 按常识,每次移动后,不仅仅是蛇头向前移动一个,更重要的是蛇尾也要向前移,但是在该函数中先不处理蛇尾
|
||||||
#因为如果蛇吃了食物,此时蛇尾其实并没有移动
|
# 因为如果蛇吃了食物,此时蛇尾其实并没有移动
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
newHead = {}
|
newHead = {}
|
||||||
#根据移动方向确定蛇头
|
# 根据移动方向确定蛇头
|
||||||
if self.direction == 'up':
|
if self.direction == 'up':
|
||||||
newHead = {'x' : self.coords[self.head_index]['x'],
|
newHead = {'x': self.coords[self.head_index]['x'],
|
||||||
'y' : self.coords[self.head_index]['y'] - 1}
|
'y': self.coords[self.head_index]['y'] - 1}
|
||||||
elif self.direction == 'down':
|
elif self.direction == 'down':
|
||||||
newHead = {'x' : self.coords[self.head_index]['x'],
|
newHead = {'x': self.coords[self.head_index]['x'],
|
||||||
'y' : self.coords[self.head_index]['y'] + 1}
|
'y': self.coords[self.head_index]['y'] + 1}
|
||||||
elif self.direction == 'left':
|
elif self.direction == 'left':
|
||||||
newHead = {'x' : self.coords[self.head_index]['x'] - 1,
|
newHead = {'x': self.coords[self.head_index]['x'] - 1,
|
||||||
'y' : self.coords[self.head_index]['y']}
|
'y': self.coords[self.head_index]['y']}
|
||||||
elif self.direction == 'right':
|
elif self.direction == 'right':
|
||||||
newHead = {'x' : self.coords[self.head_index]['x'] + 1,
|
newHead = {'x': self.coords[self.head_index]['x'] + 1,
|
||||||
'y' : self.coords[self.head_index]['y']}
|
'y': self.coords[self.head_index]['y']}
|
||||||
self.coords.insert(0,newHead)
|
self.coords.insert(0, newHead)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user