本文整理汇总了Python中snake.Snake.render方法的典型用法代码示例。如果您正苦于以下问题:Python Snake.render方法的具体用法?Python Snake.render怎么用?Python Snake.render使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类snake.Snake
的用法示例。
在下文中一共展示了Snake.render方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: GameScreen
# 需要导入模块: from snake import Snake [as 别名]
# 或者: from snake.Snake import render [as 别名]
class GameScreen(Screen):
def __init__(self, screen):
Screen.__init__(self, screen)
self.snake = Snake()
self.snake_dir = Directions.RIGHT
self.inPause = False
self.pauseImg = pygame.image.load('assets/img/pause.png').convert();
self.pauseImg.set_colorkey(Defines.COLORKEY)
self.apple = (20,20)
self.score = 0;
self.font = pygame.font.Font("assets/fonts/blogger-sans.ttf", 24)
self.fontL = pygame.font.Font("assets/fonts/blogger-sans.ttf", 48)
self.scoretxt = self.font.render("score %d" % self.score, True, (0, 0, 0))
self.txtloose = self.fontL.render("You Loose", True, (0, 0, 0))
def manageEvents(self, event):
#For testing Keyboard use event.dict['unicode'] because in default keyboard type is in American
if event.type == pygame.KEYDOWN:
if event.dict['unicode'] == "z" or event.key == pygame.K_UP:
self.snake_dir = Directions.UP
elif event.dict['unicode'] == "s" or event.key == pygame.K_DOWN:
self.snake_dir = Directions.DOWN
elif event.dict['unicode'] == "q" or event.key == pygame.K_LEFT:
self.snake_dir = Directions.LEFT
elif event.dict['unicode'] == "d" or event.key == pygame.K_RIGHT:
self.snake_dir = Directions.RIGHT
elif event.key == pygame.K_SPACE:
self.inPause = False if self.inPause else True
def update(self):
if self.snake.inLife:
if not self.inPause:
if self.snake_dir == Directions.RIGHT:
self.snake.move(self.snake.head_x + Defines.SNAKE_SIZE + 1, self.snake.head_y)
elif self.snake_dir == Directions.LEFT:
self.snake.move(self.snake.head_x - Defines.SNAKE_SIZE - 1, self.snake.head_y)
elif self.snake_dir == Directions.UP:
self.snake.move(self.snake.head_x, self.snake.head_y - Defines.SNAKE_SIZE - 1)
elif self.snake_dir == Directions.DOWN:
self.snake.move(self.snake.head_x, self.snake.head_y + Defines.SNAKE_SIZE + 1)
if self.snake.head_x == self.apple[0] and self.snake.head_y == self.apple[1]:
self.score += 10
self.scoretxt = self.font.render("score %d" % self.score, True, (0, 0, 0))
self.apple = (randint(0, 31) * 20, randint(0, 23) * 20)
self.snake.snake_length = self.snake.snake_length + 1
self.snake.snake_coords.insert(0, [self.snake.head_x, self.snake.head_y])
return self
def render(self):
self.snake.render(self.screen)
if not self.snake.inLife:
imgDie = pygame.Surface((640,480))
imgDie.set_alpha(128)
imgDie.fill((255, 0, 0))
self.screen.blit(imgDie, (0, 0))
self.screen.blit(self.txtloose, (320 - self.txtloose.get_width() // 2,200))
self.screen.blit(self.scoretxt, (10, 5))
pygame.draw.rect(self.screen, Defines.RED, [self.apple[0], self.apple[1], Defines.SNAKE_SIZE, Defines.SNAKE_SIZE])
if self.inPause:
self.screen.blit(self.pauseImg, (195, 215))
示例2: Game
# 需要导入模块: from snake import Snake [as 别名]
# 或者: from snake.Snake import render [as 别名]
class Game(object):
def __init__(self, screen_height, screen_width):
pygame.init()
self.screen_height = screen_height
self.screen_width = screen_width
self.level_maps = ("./data/level1.map", "./data/level2.map", "./data/level3.map")
self.current_level_map_index = 0
self.pause = False
self.won_grow_rate = 10
height_width = (self.screen_height, self.screen_width)
self.screen = pygame.display.set_mode(height_width)
pygame.display.set_caption("PySnake")
self.score = Score((0, 500))
self.reload_game = False
self.speed = 5
def init_resources(self):
self.level = Level(self.level_maps[self.current_level_map_index])
self.snake = Snake((64, 64))
self.move_direction = Direction.RIGHT
self.food = None
def start(self):
clock = pygame.time.Clock()
game_over = False
won = True
first_load = True
while not game_over:
clock.tick(self.speed)
for event in pygame.event.get():
self.event_handler(event)
if self.reload_game:
break
if not self.pause:
background = self.level.render()
self.snake.render(background)
if self.food == None:
position = self.generate_free_position()
self.food = Food(position, 1)
background.blit(self.food.image, self.food.rect)
self.screen.fill((0, 0, 0))
self.screen.blit(background, (0, 0))
self.screen.blit(self.score.image, self.score.rect)
pygame.display.flip()
if first_load:
first_load = False
self.pause = True
continue
not_collide = self.snake.update(self.move_direction)
if not not_collide or self.level.collide(self.snake.position):
game_over = True
won = False
if self.food.rect.colliderect(self.snake.head.rect):
self.snake.grow(self.food.grow_rate)
self.score.add(100)
self.food = None
if self.snake.length() == self.won_grow_rate:
game_over = True
if self.reload_game:
self.load_game()
elif won and game_over:
self.load_next_level()
elif game_over:
print("Game over")
def event_handler(self, event):
if event.type == QUIT:
sys.exit(0)
elif event.type == KEYDOWN:
key = pygame.key.get_pressed()
new_direction = self.move_direction
if key[pygame.K_LEFT]:
new_direction = Direction.LEFT
elif key[pygame.K_RIGHT]:
new_direction = Direction.RIGHT
elif key[pygame.K_UP]:
new_direction = Direction.UP
elif key[pygame.K_DOWN]:
new_direction = Direction.DOWN
elif key[pygame.K_p]:
self.pause = not self.pause
elif key[pygame.K_s]:
self.pause = True
level = self.current_level_map_index
snake = self.snake.get_snake_coords()
food = self.food.get_food_position()
direction = self.move_direction
SaveGame.save(level, snake, food, direction)
elif key[pygame.K_l]:
self.reload_game = True
#.........这里部分代码省略.........
示例3: Game
# 需要导入模块: from snake import Snake [as 别名]
# 或者: from snake.Snake import render [as 别名]
class Game():
star_cords = (0, 0)
def __init__(self, screen):
self.screen = screen
self.RUNNING = True
self.speed = 1.0
self.place_star()
def speed_up(self):
self.speed /= 2.0
def speed_down(self):
self.speed *= 2.0
def calc_start_loc(self):
y = curses.LINES // 2
x = curses.COLS // 2
return (y, x)
def start(self):
self.snek = Snake(
self.screen,
6,
self.calc_start_loc()
)
self.snek.render()
while self.RUNNING:
try:
time.sleep(self.speed)
self.tick()
except KeyboardInterrupt:
self.RUNNING = False
def place_star(self):
y = random.randint(0, curses.LINES - 1)
x = random.randint(0, curses.COLS - 1)
self.star_cords = (y, x)
def check_star_get(self):
if self.star_cords in [
seg.coordinates
for seg in self.snek.body
]:
self.snek.add_segment()
self.place_star()
def tick(self):
self.screen.clear()
direction = self.screen.getch()
if 258 <= direction <= 261:
if direction == self.snek.direction:
self.speed_up()
elif (direction <= 259 and self.snek.direction <= 259
or direction >= 260 and self.snek.direction >= 260):
self.speed_down()
elif direction < 260:
self.speed_down()
self.snek.direction = direction
else:
self.speed_up()
self.snek.direction = direction
self.snek.move()
self.snek.check_loss()
self.check_star_get()
self.screen.addstr(
self.star_cords[0],
self.star_cords[1],
'*'
)
self.snek.render()
self.screen.refresh()