用python写一个贪吃蛇的代码
时间: 2023-03-15 16:00:56 浏览: 57
答:这里有一个简单的贪吃蛇python代码的示例:import pygameimport randomimport sysfrom pygame.locals import *pygame.init()FPS = 5fpsClock = pygame.time.Clock()window_width = 600window_height = 600cell_size = 10num_cells_x = int(window_width/cell_size)num_cells_y = int(window_height/cell_size)checker_size = 3# set up the colorsBLACK = (0, 0, 0)WHITE = (255, 255, 255)RED = (255, 0, 0)GREEN = (0, 255, 0)DARKGREEN = (0, 155, 0)DARKGRAY = (40, 40, 40)BGCOLOR = BLACK#draws the game boarddef draw_grid():for x in range(0, window_width, cell_size):pygame.draw.line(DISPLAYSURF, DARKGRAY, (x, 0), (x, window_height))for y in range(0, window_height, cell_size):pygame.draw.line(DISPLAYSURF, DARKGRAY, (0, y), (window_width, y))#set up the direction variablesUP = 'up'DOWN = 'down'LEFT = 'left'RIGHT = 'right'#opposite directions for when the snake runs into itselfopposites = {UP: DOWN, DOWN: UP, LEFT: RIGHT, RIGHT: LEFT}#game variablessnake_position = [100, 100]snake_body = [[100, 100], [90, 100], [80, 100]]direction = RIGHT#food position variablesfood_position = [random.randint(0, num_cells_x-1) * cell_size, random.randint(0, num_cells_y-1) * cell_size]food_spawned = True#set up the display windowDISPLAYSURF = pygame.display.set_mode((window_width, window_height))pygame.display.set_caption('Snake')#game loopwhile True:for event in pygame.event.get():if event.type == QUIT:pygame.quit()sys.exit()elif event.type == KEYDOWN:if event.key == K_UP and direction != DOWN:direction = UPelif event.key == K_DOWN and direction != UP:direction = DOWNelif event.key == K_LEFT and direction != RIGHT:direction = LEFTelif event.key == K_RIGHT and direction != LEFT:direction = RIGHT#check if the snake has run into itselfif snake_position[0] in [cell[0] for cell in snake_body[1:]] and snake_position[1] in [cell[1] for cell in snake_body[1:]]:direction = opposites[direction]#check if the snake has run into the edgeif snake_position[0] == 0 or snake_position[0] == window_width or snake_position[1] == 0 or snake_position[1] == window_height:direction = opposites[direction]if direction == UP:snake_position[1] -= cell_sizeif direction == DOWN:snake_position[1] += cell_sizeif direction == LEFT:snake_position[0] -= cell_sizeif direction == RIGHT:snake_position[0] += cell_size#insert new head into the snake_body listsnake_body.insert(0, list(snake_position))#check if the snake has eaten the foodif snake_position[0] == food_position[0] and snake_position[1] == food_position[1]:food_spawned = Falseelse:snake_body.pop()#spawn new foodif not food_spawned:food_position = [random.randint(0, num_cells_x-1) * cell_size, random.randint(0, num_cells_y-1) * cell_size]food_spawned = True#draw the screenDISPLAYSURF.fill(BGCOLOR)draw_grid()for position in snake_body:#snake bodypygame.draw.rect(DISPLAYSURF, DARKGREEN, Rect(position[0], position[1], cell_size, cell_size))#snake headpygame.draw.rect(DISPLAYSURF, GREEN, Rect(position[0], position[1], cell_size, cell_size))#foodpygame.draw.rect(DISPLAYSURF, RED, Rect(food_position[0], food_position[1], cell_size, cell_size))#update the screenpygame.display.update()fpsClock.tick(FPS)
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)