本文整理汇总了Python中snake.Snake.head方法的典型用法代码示例。如果您正苦于以下问题:Python Snake.head方法的具体用法?Python Snake.head怎么用?Python Snake.head使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类snake.Snake
的用法示例。
在下文中一共展示了Snake.head方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: SnakeGame
# 需要导入模块: from snake import Snake [as 别名]
# 或者: from snake.Snake import head [as 别名]
class SnakeGame(object):
def __init__(self):
pygame.init()
pygame.display.set_caption('Snake')
self.block_size = BLOCK_SIZE
self.window = pygame.display.set_mode(WORLD_SIZE*self.block_size)
self.screen = pygame.display.get_surface()
self.clock = pygame.time.Clock()
self.font = pygame.font.SysFont(FONT_TYPE,FONT_SIZE)
self.world = Rect((0,0),WORLD_SIZE)
self.reset()
def reset(self):
''' Start a new game
'''
self.playing = True
self.next_direction = DIRECTION_UP
self.score = 0
self.snake = Snake(self.world.center, SNAKE_START_LENGTH)
self.food = set()
self.add_food()
return
def add_food(self):
''' Add food, and with a small
probability add more than
one
'''
while not (self.food and randrange(FOOD_PROBABILITY)):
food = Position(map(randrange,self.world.bottomright))
if food not in self.food and food not in self.snake:
self.food.add(food)
return
def inp(self,e):
''' Process keyboard event e
'''
if e.key in KEY_DIRECTION:
self.next_direction = KEY_DIRECTION[e.key]
elif e.key == K_SPACE and not self.playing: # space to reset
self.reset()
return
def update(self,dt):
''' Update the game by dt seconds
'''
self.snake.update(dt, self.next_direction)
head = self.snake.head()
if head in self.food: #if snake hits food with head
self.food.remove(head) #consume the food
self.add_food() #add more food
self.snake.grow() #grow the snake
self.score += len(self.snake) * SEGMENT_SCORE
if self.snake.self_intersection() or not self.world.collidepoint(self.snake.head()): #colliding with self of boundaries is game over
self.playing = False
return
def block(self,p):
''' Return the screen rectangle
corresponding to the position
p
'''
return Rect(p*self.block_size, DIRECTION_DR*self.block_size)
def draw_text(self,text,p):
''' Draw text at position p
'''
self.screen.blit(self.font.render(text, 1, TEXT_COLOR),p)
return
def draw(self):
''' Draw the game
'''
self.screen.fill(BACKGROUND_COLOR)
for p in self.snake:
pygame.draw.rect(self.screen,SNAKE_COLOR, self.block(p))
for f in self.food:
pygame.draw.rect(self.screen, FOOD_COLOR,self.block(f))
self.draw_text('Score: {}'.format(self.score),(20,20))
return
def draw_death(self):
''' Draw game after game over
'''
self.screen.fill(DEATH_COLOR)
self.draw_text('Game over! Press space to start a new game',(20,150))
self.draw_text('Your score is {}'.format(self.score),(140,140))
return
def process_events(self):
''' Process key events and
return bool saying if
we should quit
'''
for e in pygame.event.get():
if e.type == QUIT:
return True
#.........这里部分代码省略.........
示例2: SnakeGame
# 需要导入模块: from snake import Snake [as 别名]
# 或者: from snake.Snake import head [as 别名]
class SnakeGame(object):
"""
Represents game object, handles snake's movement and drawing,
mice creating, score count, draws everything.
"""
def __init__(self):
secure_screen_size()
pg.init() # initialize pygame module
pg.display.set_caption('PySnake Game')
# height of upper bound line of the drawing frame
self._snake_width = FRAME_SIZE/FRAME_WIDTH_TO_SNAKE_WIDTH
# make main screen surface a little bit higher (for score text)
self._screen = pg.display.set_mode((FRAME_SIZE, FRAME_SIZE + self._snake_width * 2))
# make a subsurface from screen surface. It will be rectangle where snake will move
self._frame = self._screen.subsurface([0, self._snake_width*2, FRAME_SIZE, FRAME_SIZE])
# set of all grid fields: range for x and y go from 0 to SCREEN_WIDTH_TO_SNAKE_WIDTH - 1
# it will be used to pick a place for draw mouse for snake to chase
self._grid_field_list = [(x, y) for x in xrange(FRAME_WIDTH_TO_SNAKE_WIDTH) for y in xrange(FRAME_WIDTH_TO_SNAKE_WIDTH)]
self._grid_field_size = self._screen.get_width() / FRAME_WIDTH_TO_SNAKE_WIDTH # in pixels
# initialize font
self._font = pg.font.SysFont("monospace", self._snake_width*2 - 3)
# create snake
self._snake = Snake(self._frame, self._snake_width)
# Clock object is used to slow down and main loop, it ensures the FPS
self._clock = pg.time.Clock()
# default speed of the game
self._speed = 10
# default speed increment when snake catches mouse
self._speed_inc = 3
# increment score when snake catches the mouse
self._score = 0
def run(self):
# draw the white background onto the surface
self._frame.fill(BLACK)
# draw frame for the game
self._draw_frame_line()
self._draw_score()
self._snake.draw(draw_everything=True)
mouse_pos = self._draw_mouse()
pg.display.update()
running = True
direction = DIR_RIGHT # initial movement direction
while self._snake.move(direction) and running:
self._snake.draw()
self._draw_frame_line()
# check if snake's head is on the mouse field
if self._snake.head() == mouse_pos:
self._delete_score()
self._score += 1
self._snake.grow()
self._delete_mouse(mouse_pos) # snake eats mouse -> remove it from field
mouse_pos = self._draw_mouse() # re-draw mouse again
self._speed += self._speed_inc # increase play speed
self._draw_score()
pg.display.flip()
self._clock.tick(self._speed) # ensure frame rate of the game: higher the FPS -> snake will be faster
for event in pg.event.get():
if event.type is pg.QUIT:
running = False
elif event.type is pg.KEYDOWN:
if event.key == pg.K_ESCAPE:
running = False
elif event.key == pg.K_LEFT:
direction = DIR_LEFT
elif event.key == pg.K_RIGHT:
direction = DIR_RIGHT
elif event.key == pg.K_UP:
direction = DIR_UP
elif event.key == pg.K_DOWN:
direction = DIR_DOWN
def _draw_mouse(self):
"""
Picks random location and draws mouse
:return: mouse location -> tuple (x,y)
"""
pos = self._snake.grid_occupied[0]
while pos in self._snake.grid_occupied:
pos = (randint(0, FRAME_WIDTH_TO_SNAKE_WIDTH-1), randint(0, FRAME_WIDTH_TO_SNAKE_WIDTH-1))
# draw a pink circle onto the surface
center = (pos[0] * self._grid_field_size + self._grid_field_size / 2,
pos[1] * self._grid_field_size + self._grid_field_size / 2)
#.........这里部分代码省略.........