写一个贪吃蛇的代码
```python
import sys
import random
import pygame
from pygame.locals import *
pygame.init()
FPS = 10
WINDOW_WIDTH = 640
WINDOW_HEIGHT = 480
CELL_SIZE = 20
CELL_WIDTH = int(WINDOW_WIDTH / CELL_SIZE)
CELL_HEIGHT = int(WINDOW_HEIGHT / CELL_SIZE)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
def main():
global FPSCLOCK, DISPLAYSURF, BASICFONT
FPSCLOCK = pygame.time.Clock()
DISPLAYSURF = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
BASICFONT = pygame.font.Font('freesansbold.ttf', 18)
pygame.display.set_caption('贪吃蛇')
while True:
run_game()
show_gameover_screen()
def run_game():
start_x = random.randint(5, CELL_WIDTH - 6)
start_y = random.randint(5, CELL_HEIGHT - 6)
worm_coords = [{'x': start_x, 'y': start_y},
{'x': start_x - 1, 'y': start_y},
{'x': start_x - 2, 'y': start_y}]
direction = RIGHT
apple = get_random_location()
while True:
for event in pygame.event.get():
if event.type == QUIT:
terminate()
elif event.type == KEYDOWN:
if (event.key == K_LEFT or event.key == K_a) and direction != RIGHT:
direction = LEFT
elif (event.key == K_RIGHT or event.key == K_d) and direction != LEFT:
direction = RIGHT
elif (event.key == K_UP or event.key == K_w) and direction != DOWN:
direction = UP
elif (event.key == K_DOWN or event.key == K_s) and direction != UP:
direction = DOWN
elif event.key == K_ESCAPE:
terminate()
if worm_coords[HEAD]['x'] == -1 or worm_coords[HEAD]['x'] == CELL_WIDTH or worm_coords[HEAD]['y'] == -1 or worm_coords[HEAD]['y'] == CELL_HEIGHT:
return
for worm_body in worm_coords[1:]:
if worm_body['x'] == worm_coords[HEAD]['x'] and worm_body['y'] == worm_coords[HEAD]['y']:
return
if worm_coords[HEAD]['x'] == apple['x'] and worm_coords[HEAD]['y'] == apple['y']:
apple = get_random_location()
else:
del worm_coords[-1]
if direction == UP:
new_head = {'x': worm_coords[HEAD]['x'], 'y': worm_coords[HEAD]['y'] - 1}
elif direction == DOWN:
new_head = {'x': worm_coords[HEAD]['x'], 'y': worm_coords[HEAD]['y'] + 1}
elif direction == LEFT:
new_head = {'x': worm_coords[HEAD]['x'] - 1, 'y': worm_coords[HEAD]['y']}
elif direction == RIGHT:
new_head = {'x': worm_coords[HEAD]['x'] + 1, 'y': worm_coords[HEAD]['y']}
worm_coords.insert(0, new_head)
DISPLAYSURF.fill(BLACK)
draw_grid()
draw_worm(worm_coords)
draw_apple(apple)
draw_score(len(worm_coords) - 3)
pygame.display.update()
FPSCLOCK.tick(FPS)
def draw_press_key_msg():
press_key_surf = BASICFONT.render('按任意键继续...', True, WHITE)
press_key_rect = press_key_surf.get_rect()
press_key_rect.topleft = (WINDOW_WIDTH - 200, WINDOW_HEIGHT - 30)
DISPLAYSURF.blit(press_key_surf, press_key_rect)
def check_for_key_press():
if len(pygame.event.get(QUIT)) > 0:
terminate()
key_up_events = pygame.event.get(KEYUP)
if len(key_up_events) == 0:
return None
if key_up_events[0].key == K_ESCAPE:
terminate()
return key_up_events[0].key
def show_gameover_screen():
gameover_surf = BASICFONT.render('游戏结束', True, WHITE)
gameover_rect = gameover_surf.get_rect()
gameover_rect.midtop = (WINDOW_WIDTH / 2, 10)
DISPLAYSURF.blit(gameover_surf, gameover_rect)
draw_press_key_msg()
pygame.display.update()
pygame.time.wait(500)
check_for_key_press()
while True:
if check_for_key_press():
pygame.event.get()
return
def terminate():
pygame.quit()
sys.exit()
def get_random_location():
return {'x': random.randint(0, CELL_WIDTH - 1), 'y': random.randint(0, CELL_HEIGHT - 1)}
def draw_score(score):
score_surf = BASICFONT.render('得分: %s' % (score), True, WHITE)
score_rect = score_surf.get_rect()
score_rect.topleft = (WINDOW_WIDTH - 120, 10)
DISPLAYSURF.blit(score_surf, score_rect)
def draw_worm(worm_coords):
for coord in worm_coords:
x = coord['x'] * CELL_SIZE
y = coord['y'] * CELL_SIZE
worm_segment_rect = pygame.Rect(x, y, CELL_SIZE, CELL_SIZE)
pygame.draw.rect(DISPLAYSURF, GREEN, worm_segment_rect)
worm_inner_segment_rect = pygame.Rect(x + 4, y + 4, CELL_SIZE - 8, CELL_SIZE - 8)
pygame.draw.rect(DISPLAYSURF, GREEN, worm_inner_segment_rect)
def draw_apple(coord):
x = coord['x'] * CELL_SIZE
y = coord['y'] * CELL_SIZE
apple_rect = pygame.Rect(x, y, CELL_SIZE, CELL_SIZE)
pygame.draw.rect(DISPLAYSURF, RED, apple_rect)
def draw_grid():
for x in range(0, WINDOW_WIDTH, CELL_SIZE):
pygame.draw.line(DISPLAYSURF, WHITE, (x, 0), (x, WINDOW_HEIGHT))
for y in range(0, WINDOW_HEIGHT, CELL_SIZE):
pygame.draw.line(DISPLAYSURF, WHITE, (0, y), (WINDOW_WIDTH, y))
if __name__ == '__main__':
main()
```
创作工场
免责声明:
以上内容除特别注明外均来源于网友提问,创作工场回答,未经许可,严谨转载。
点击这里>>使用🔥专业版,更聪明、更完整、更原创!