赞
踩
●版本:Python3
●系统:Windows
●相关模块:pygame
pip install pygame安装即可
- import random, pygame, sys
- from pygame.locals import *
- import time
- '''
- '''
- FPS = 1
- ##WINDOWWIDTH = 640
- #WINDOWHEIGHT = 480
- WINDOWWIDTH = 600
- WINDOWHEIGHT = 480
- CELLSIZE = 40
- assert WINDOWWIDTH % CELLSIZE == 0, "Window width must be a multiple of cell size."
- assert WINDOWHEIGHT % CELLSIZE == 0, "Window height must be a multiple of cell size."
- CELLWIDTH = int(WINDOWWIDTH / CELLSIZE)
- CELLHEIGHT = int(WINDOWHEIGHT / CELLSIZE)
-
- # R G B
- WHITE = (255, 255, 255)
- BLACK = ( 0, 0, 0)
- RED = (255, 0, 0)
- GREEN = ( 0, 255, 0)
- DARKGREEN = ( 0, 155, 0)
- DARKGRAY = ( 40, 40, 40)
- BGCOLOR = BLACK
-
-
-
- UP = 'up'
- DOWN = 'down'
- LEFT = 'left'
- RIGHT = 'right'
-
- direction = UP
- DIRECTION = [UP,DOWN,LEFT,RIGHT]
-
- HEAD = 0 # syntactic sugar: index of the worm's head
-
-
-
- distance = []
-
- for y in range(CELLHEIGHT):
- distance.append([])
- for x in range(CELLWIDTH):
- distance[y].append(8888)
-
- def into_queue(grid, queue, visited, worm,apple):
- x,y = grid
- if (x, y) == (apple['x'],apple['y']):
- return False
- elif x < 0 or x >= CELLWIDTH:
- return False
- elif y < 0 or y >= CELLHEIGHT:
- return False
- elif (x, y) in queue:
- return False
- elif (x, y) in visited:
- return False
- else:
- return True
-
- def is_snake(x,y,worm):
- for body in worm:
- if body['x'] == x and body['y'] == y:
- return True
- return False
-
-
- def cal_distance(worm,apple):
- queue = [(apple['x'],apple['y'])]
- visited = []
- found = False
- for y in range(CELLHEIGHT):
- for x in range(CELLWIDTH):
- distance[y][x] = 9999
-
- distance[apple['y']][apple['x']] = 0
-
- while len(queue) != 0:
- head = queue[0]
- visited.append(head)
- up_grid = head[0], head[1] - 1
- down_grid = head[0], head[1] + 1
- left_grid = head[0] - 1, head[1]
- right_grid = head[0] + 1, head[1]
-
- for grid in [up_grid, down_grid, left_grid, right_grid]:
- if into_queue(grid, queue, visited,worm,apple):
- if grid[0] == worm[HEAD]['x'] and grid[1] == worm[HEAD]['y']:
- found = True
- if not is_snake(grid[0],grid[1],worm):
- queue.append(grid)
- distance[grid[1]][grid[0]] = distance[head[1]][head[0]] + 1
- queue.pop(0)
- return found
-
- def main():
- global FPSCLOCK, DISPLAYSURF, BASICFONT
-
- pygame.init()
- FPSCLOCK = pygame.time.Clock()
- DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
- BASICFONT = pygame.font.Font('freesansbold.ttf', 18)
- pygame.display.set_caption('Snaky')
-
- showStartScreen()
- while True:
- runGame()
- showGameOverScreen()
-
- def can_move(grid, worm):
- x,y = grid
- if x < 0 or x >= CELLWIDTH:
- return False
- elif y < 0 or y >= CELLHEIGHT:
- return False
- elif is_snake(x, y,worm):
- return False
- elif (x, y) == (worm[HEAD]['x'], worm[HEAD]['y']):
- return False
- else:
- return True
-
-
- def update_dirc(now, direc):
- loc = {'x':0,'y':0}
- if direc == UP:
- loc = {'x':now['x'],'y':now['y']-1}
- elif direc == DOWN:
- loc = {'x':now['x'],'y':now['y']+1}
- elif direc == RIGHT:
- loc = {'x':now['x']+1,'y':now['y']}
- elif direc == LEFT:
- loc = {'x':now['x']-1,'y':now['y']}
- return loc
-
- def virtual_run(wormCoords, apple,direction):
- wormCoords = list(wormCoords)
- food_eated = False
- while not food_eated:
- cal_distance(wormCoords,apple)
- four_dis = [99999, 99999, 99999, 99999]
- if can_move((wormCoords[HEAD]['x'], wormCoords[HEAD]['y'] - 1), wormCoords):
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。