本文整理汇总了Python中utils.Timer.update方法的典型用法代码示例。如果您正苦于以下问题:Python Timer.update方法的具体用法?Python Timer.update怎么用?Python Timer.update使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类utils.Timer
的用法示例。
在下文中一共展示了Timer.update方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Animation
# 需要导入模块: from utils import Timer [as 别名]
# 或者: from utils.Timer import update [as 别名]
class Animation(object):
def __init__(self, screen, pos, images, scroll_period, duration=-1):
"""
If duration == -1, animation goes on indefinetly.
"""
self.screen = screen
self.pos = pos
self.images = [pygame.image.load(image) for image in images]
self.image_ptr = 0
self.scroll_period = scroll_period
self.duration = duration
self.active = True
self.scroll_timer = Timer(scroll_period, self.advance_images)
self.active_timer = Timer(duration, self.inactivate, 1)
def update(self, time_passed):
if self.active:
self.scroll_timer.update(time_passed)
self.active_timer.update(time_passed)
def blitme(self):
if self.active:
self.update_rect()
self.screen.blit(self.images[self.image_ptr], self.rect)
def update_rect(self):
image_w, image_h = self.images[self.image_ptr].get_size()
self.rect = self.images[self.image_ptr].get_rect().move(
self.pos.x - image_w / 2,
self.pos.y - image_h / 2)
def advance_images(self):
self.image_ptr = (self.image_ptr + 1) % len(self.images)
def inactivate(self):
if self.duration >= 0:
self.active = False
示例2: RunTime
# 需要导入模块: from utils import Timer [as 别名]
# 或者: from utils.Timer import update [as 别名]
class RunTime(object):
def __init__(self):
# behaviour update callbacks
self.behav_callbacks = []
self.init_time = time()
self.current_time = 0
self.timer = Timer()
def run(self):
while True:
# update all the behaviours
for cb in self.behav_callbacks:
cb(self.current_time)
# notify all the timer events
self.timer.update(self.current_time)
# TODO fps control
sleep(0.1)
self.current_time = time()-self.init_time
def register(self, callback):
self.behav_callbacks.append(callback)
示例3: Game
# 需要导入模块: from utils import Timer [as 别名]
# 或者: from utils.Timer import update [as 别名]
class Game(object):
def __init__(self):
pygame.init()
self.screen = pygame.display.set_mode(
(SCREEN_WIDTH, SCREEN_HEIGHT), 0, 32)
self.clock = pygame.time.Clock()
# Game states
self.paused = 0
self.game_over = 0
self.running = 1
self.keep_drawing_ship = 1
# Game objects
self.ship = ship.Ship(
self.screen, (SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2))
self.asteroids = []
self.bullets = []
# Scoreboard
self.score = 0
self.scoreboard = Scoreboard(self.screen)
# Bullet Gauge
self.bullet_gauge = BulletGauge(self.screen, 10, self.spawn_bullet)
# Health bar
self.health_bar = Bar(self.screen, self.ship.max_health,
[30, SCREEN_HEIGHT - 20,
80, 10],
RED, WHITE)
# Game Over Text
self.game_over_text = GameOverText(self.screen)
self.reduce_game_over_text_alpha = Timer(
100, self.game_over_text.reduce_alpha)
# Asteroid spawning
self.since_last_asteroid = 0
self.new_asteroid_time = 1000
# Levels
self.level_gen = generate_levels()
self.level = 1
self.level_limit = next(self.level_gen)
self.level_text = LevelText(self.screen)
def run(self):
while self.running:
time_passed = self.clock.tick(60)
# KEYBOARD INPUT
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.running = 0
break
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
self.running = 0
break
if event.key == pygame.K_SPACE:
if not self.paused and not self.game_over:
self.bullet_gauge.shoot()
if event.key == pygame.K_p:
self.paused = not self.paused
if not self.paused and not self.game_over:
self.update(time_passed)
if self.game_over:
self.update_game_over_sequence(time_passed)
self.draw()
def update(self, time_passed):
# Send keyboard input
self.ship.handleKeyevents(pygame.key.get_pressed())
# Object updates
self.ship.update(time_passed)
for bullet in self.bullets:
bullet.update(time_passed)
for asteroid in self.asteroids:
asteroid.update(time_passed, self.ship.pos)
# Maintenance functions
self.ship.keep_in_bounds()
self.maintain_bullets()
self.maintain_asteroids()
# Collisions
#.........这里部分代码省略.........
示例4: Game
# 需要导入模块: from utils import Timer [as 别名]
# 或者: from utils.Timer import update [as 别名]
#.........这里部分代码省略.........
Color(50, 50, 50),
(self.field_rect.left, self.field_rect.top + y * self.GRID_SIZE - 1),
(self.field_rect.right - 1, self.field_rect.top + y * self.GRID_SIZE - 1))
for x in range(self.grid_ncols + 1):
pygame.draw.line(
self.screen,
Color(50, 50, 50),
(self.field_rect.left + x * self.GRID_SIZE - 1, self.field_rect.top),
(self.field_rect.left + x * self.GRID_SIZE - 1, self.field_rect.bottom - 1))
def draw_walls(self):
wallcolor = Color(140, 140, 140)
for wall in self.walls:
nrow, ncol = wall
pos_x = self.field_rect.left + ncol * self.GRID_SIZE + self.GRID_SIZE / 2
pos_y = self.field_rect.top + nrow * self.GRID_SIZE + self.GRID_SIZE / 2
radius = 3
pygame.draw.polygon(self.screen, wallcolor,
[ (pos_x - radius, pos_y), (pos_x, pos_y + radius),
(pos_x + radius, pos_y), (pos_x, pos_y - radius)])
if (nrow + 1, ncol) in self.walls:
pygame.draw.line(self.screen, wallcolor,
(pos_x, pos_y), (pos_x, pos_y + self.GRID_SIZE), 3)
if (nrow, ncol + 1) in self.walls:
pygame.draw.line(self.screen, wallcolor,
(pos_x, pos_y), (pos_x + self.GRID_SIZE, pos_y), 3)
def draw(self):
self.draw_background()
self.field_box.draw()
if self.options['draw_grid']:
self.draw_grid()
self.draw_walls()
self.tboard.draw()
self.mboard.text = self.mboard_text
self.mboard.draw()
for creep in self.creeps:
creep.draw()
self.draw_portals()
def run(self):
# The main game loop
#
while True:
# Limit frame speed to 30 FPS
#
time_passed = self.clock.tick(30)
#~ time_passed = self.clock.tick()
#~ print time_passed
# If too long has passed between two frames, don't
# update (the game must have been suspended for some
# reason, and we don't want it to "jump forward"
# suddenly)
#
if time_passed > 100:
continue
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
self.paused = not self.paused
elif event.key == pygame.K_g:
if pygame.key.get_mods() & pygame.KMOD_CTRL:
self.options['draw_grid'] = not self.options['draw_grid']
elif ( event.type == pygame.MOUSEBUTTONDOWN and
event.button == 1):
for creep in self.creeps:
creep.mouse_click_event(event.pos)
if not self.paused:
msg1 = 'Creeps: %d' % len(self.creeps)
msg2 = ''
self.mboard_text = [msg1, msg2]
self.creep_spawn_timer.update(time_passed)
# Update and all creeps
for creep in self.creeps:
creep.update(time_passed)
self.draw()
pygame.display.flip()
def quit(self):
sys.exit()
示例5: Game
# 需要导入模块: from utils import Timer [as 别名]
# 或者: from utils.Timer import update [as 别名]
#.........这里部分代码省略.........
self.field_rect.top +self.GRID_SIZE/2),
init_direction = (1,1),
speed = 0.05 ) )
self.spawned_creep_count += 1
def getFieldRect(self):
return self.field_box.get_internal_rect()
def drawBackground(self):
img_rect = self.tile_img.get_rect()
nrows = int(self.screen.get_height()/img_rect.height) + 1
ncols = int(self.screen.get_width() /img_rect.width) + 1
for y in range(nrows):
for x in range(ncols):
img_rect.topleft = x*img_rect.width, y*img_rect.height
self.screen.blit(self.tile_img, img_rect)
def drawPostals(self):
self.entrance_rect = pygame.Rect(self.field_rect.left, self.field_rect.top,
2*self.GRID_SIZE,2*self.GRID_SIZE)
self.exit_rect = pygame.Rect(self.field_rect.right - 2*self.GRID_SIZE,
self.field_rect.bottom - 2*self.GRID_SIZE,
2*self.GRID_SIZE, 2*self.GRID_SIZE)
entrance = pygame.Surface( (self.entrance_rect.w, self.entrance_rect.h) )
entrance.fill(pygame.Color(80,200,80) )
entrance.set_alpha(150)
self.screen.blit(entrance,self.entrance_rect)
exit = pygame.Surface( (self.exit_rect.w,self.exit_rect.h) )
exit.fill( pygame.Color(80,200,80) )
exit.set_alpha(150)
self.screen.blit(exit,self.exit_rect)
def drawGrid(self):
for y in range(self.grid_nrows+1):
pygame.draw.line(self.screen, pygame.Color(50,50,50),
(self.field_rect.left, self.field_rect.top+y*self.GRID_SIZE-1),
(self.field_rect.right, self.field_rect.top + y*self.GRID_SIZE-1) )
for x in range(self.grid_ncols+1):
pygame.draw.line(self.screen,pygame.Color(50,50,50),
(self.field_rect.left + x*self.GRID_SIZE-1,self.field_rect.top),
(self.field_rect.left + x*self.GRID_SIZE-1,self.field_rect.bottom-1) )
def drawWalls(self):
wallcolor = pygame.Color(140,140,140)
for wall in self.walls:
row, col = wall
pos_x = self.field_rect.left + col*self.GRID_SIZE + self.GRID_SIZE/2
pos_y = self.field_rect.top + row*self.GRID_SIZE + self.GRID_SIZE/2
radius = 3
pygame.draw.polygon(self.screen, wallcolor,
[ (pos_x - radius, pos_y),(pos_x,pos_y + radius),
(pos_x + radius, pos_y), (pos_x,pos_y-radius) ] )
if (row+1,col) in self.walls:
pygame.draw.line(self.screen, wallcolor, (pos_x,pos_y),(pos_x,pos_y+self.GRID_SIZE),3 )
if (row, col+1) in self.walls:
pygame.draw.line(self.screen, wallcolor, (pos_x,pos_y),(pos_x+self.GRID_SIZE,pos_y),3)
def draw(self):
self.drawBackground()
self.field_box.draw()
if self.options['draw_grid']: self.drawGrid()
self.drawWalls()
self.tboard.draw()
self.mboard.text = self.mboard_text
self.mboard.draw()
for creep in self.creeps:
creep.draw()
self.drawPostals()
def onEvent(self,event):
if event.type == pygame.QUIT:
self.quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
self.paused = not self.paused
elif event.key == pygame.K_g:
if pygame.key.get_mods() & pygame.KMOD_CTRL:
self.options['draw_grid'] = not self.options['draw_grid']
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
for creep in self.creeps:
creep.mouseClickEvent(event.pos)
def run(self):
while True:
time_passed = self.clock.tick(30)
if time_passed > 100: continue
for event in pygame.event.get():
self.onEvent(event)
if not self.paused:
msg1 = 'Creeps:%d' %len(self.creeps)
msg2 = ''
self.mboard_text = [msg1,msg2]
self.creep_spawn_timer.update(time_passed)
# update and all creeps
for creep in self.creeps:
creep.update(time_passed)
self.draw()
pygame.display.flip()
def quit(self):
sys.exit()
示例6: Simulation
# 需要导入模块: from utils import Timer [as 别名]
# 或者: from utils.Timer import update [as 别名]
class Simulation(object):
"""
The main simulation entry point
"""
# basic defaults
SCREEN_WIDTH, SCREEN_HEIGHT = 700, 700
GRID_SIZE = 20, 20
FIELD_SIZE = 500, 500
FIELD_LIMITS = 0, 0, 600, 600
field_bgcolor = SIM_COLORS['white']
def __init__(self, params=None):
pygame.init()
if params is not None:
self.SCREEN_HEIGHT, self.SCREEN_WIDTH = int(float(params['display']['height']) * SCALE), \
int(float(params['display']['width']) * SCALE)
self.FIELD_LIMITS = int(float(params['field_top_left_x']) * SCALE), \
int(float(params['field_top_left_y']) * SCALE), \
int(float(params['field_bottom_right_x']) * SCALE), \
int(float(params['field_bottom_right_y']) * SCALE)
self.FIELD_SIZE = self.FIELD_LIMITS[2] - self.FIELD_LIMITS[0], self.FIELD_LIMITS[3] - self.FIELD_LIMITS[1]
self.GRID_SIZE = int(float(params['cell']['width']) * SCALE), int(float(params['cell']['height']) * SCALE)
# zoom and centering
self.offset = 0, 0
self.zoom_factor = 1.0
self._old_screen = self.SCREEN_WIDTH, self.SCREEN_HEIGHT
# setup the screen and the box field of play
self.initialize_screen()
# agents
self.agents = pygame.sprite.Group()
self.agent_image = pygame.image.load('assets/blueagent.bmp').convert_alpha()
self.controller = SocialForceController(self)
# self.controller = RandomController(self)
# time related items
self.clock = pygame.time.Clock()
self.paused = False
self.simulation_timer = Timer(10, self.simulation_update)
# create the grid
self.setup_grid()
# additional options (remove this)
self.options = dict(draw_grid=True)
# setup objects (waypoints, obstacles)
self.waypoints = dict()
self.obstacles = []
self._agent_count = 0
# initialize the time
self.time_passed = 0
def initialize_screen(self):
self.screen = pygame.display.set_mode((self.SCREEN_WIDTH, self.SCREEN_HEIGHT),
HWSURFACE | DOUBLEBUF | RESIZABLE, 32)
self.field_rect_outer = Rect(self.FIELD_LIMITS[0],
self.FIELD_LIMITS[1],
int(self.FIELD_SIZE[0]*self.zoom_factor),
int(self.FIELD_SIZE[1]*self.zoom_factor))
self.field_box = Box(surface=self.screen,
rect=self.field_rect_outer,
background_color=self.field_bgcolor
)
self.field_rect = self.field_box.get_internal_rect()
def setup_grid(self):
self.grid_nrows = self.FIELD_SIZE[1] / int(self.GRID_SIZE[0]*self.zoom_factor)
self.grid_ncols = self.FIELD_SIZE[0] / int(self.GRID_SIZE[1]*self.zoom_factor)
def get_agent_neighbors(self, agent, dist_range):
neighbors = []
for other in self.agents:
if not agent.id == other.id:
dist = agent.position.get_distance(other.position)
if dist <= dist_range:
neighbors.append(other)
return neighbors
def draw_grid(self):
for y in range(self.grid_nrows + 1):
pygame.draw.line(
self.screen,
SIM_COLORS['light gray'],
(self.field_rect.left, self.field_rect.top + y * int(self.GRID_SIZE[1]*self.zoom_factor) - 1),
(self.field_rect.right - 1, self.field_rect.top + y * int(self.GRID_SIZE[1]*self.zoom_factor) - 1))
for x in range(self.grid_ncols + 1):
pygame.draw.line(
self.screen,
SIM_COLORS['light gray'],
#.........这里部分代码省略.........
示例7: test_timer
# 需要导入模块: from utils import Timer [as 别名]
# 或者: from utils.Timer import update [as 别名]
def test_timer():
t = Timer()
t2 = Timer(3)
t.update()
t2.update()
示例8: Tower
# 需要导入模块: from utils import Timer [as 别名]
# 或者: from utils.Timer import update [as 别名]
class Tower(Sprite):
"""A tower object, placed on the grid. Unwalkable. Fires projectiles at nearby creeps."""
def __init__(self, screen, game, position=(0, 0), tower_image='images/towers/arrow_tower_1.png', projectile_image='images/projectiles/Arrow1.png', frameinterval=100, template=False):
""" Create a new Tower.
screen:
The screen on which the tower is located (must be a pygame Surface object, such as pygame.display)
game:
This is the game object that holds information about the game world.
tower_image:
An image (as Pygame surface objects) for the tower.
projectile_image:
An image for the projectile the tower emits
position:
The (x, y) position of the tower.
"""
Sprite.__init__(self)
self.name = "Arrow Tower"
self.description = None
if not template:
game._placed_tower_count += 1
self.id = game._placed_tower_count
game.last_placed_tower_id = self.id
else:
self.id = None
self.type = 'Tower'
self.screen = screen
self.game = game
self.image_list = None
if tower_image.__class__ == "".__class__:
self.image = pygame.image.load(tower_image).convert_alpha()
elif tower_image.__class__ == [].__class__:
self.image = pygame.image.load(tower_image[0]).convert_alpha()
self.image_list = tower_image
self.imageframeid = 0
self.frametimer = Timer(frameinterval, self.next_frame, oneshot=False)
self.icon = pygame.transform.scale(self.image, (30, 30))
self.projectile_image = pygame.image.load(projectile_image).convert_alpha()
self.orig_pos = position #just for eventual need to know exactly which pixel it was placed at (and then it was snapped to the topleft of that tile)
if not template:
#Tile snapping code
self.coordx_topleft, self.coordy_topleft = self.game.xy2coord(position) #these are used further down
self.coord_topleft = (self.coordx_topleft, self.coordy_topleft)
self.pos = game.coord2xy(self.coord_topleft)
self.is_upgrade = False
self.cost = 10
#Fire-related
self.damage = 4
self.radius = 100 #Attacking range
self.ratio = 1.0 #Ratio of the radius, used for upgrades. #is this really used by current collison detection?
self.attack_speed = 700 #ms delay
self.aoe = False #should be radius of area
self.aoefactor = 0.50 #factor to multiply by damage on area attack
self.projectileeffect = None
self.last_fire_time = 0
self.last_target_id = None
#Experience-related
self.level = 1
self.experience = 0
self.kills = 0
self.experience_req = 5
self.last_experience_req = 0
self.delta_exp_req = self.experience_req - self.last_experience_req
#Projectile-related
self.projectile_speed = 0.23
self.width, self.height = self.image.get_size()
self.widthbycoord = ceil(self.width / float(game.GRID_SIZE)) #get the ceiling, to block all tiles the tower occupies a portion of.
self.heightbycoord = ceil(self.height / float(game.GRID_SIZE))
#Blocking all covered tiles
#
if not template:
for extra_tile_y in range(int(self.heightbycoord)): #Block all occupied tiles
for extra_tile_x in range(int(self.widthbycoord)):
self.game.gridpath.set_blocked((self.coordx_topleft + extra_tile_x, self.coordy_topleft + extra_tile_y))
self.rect = Rect(self.pos[0], self.pos[1], self.width, self.height)
#print("Image: ", tower_image, "projectile_image: ", projectile_image, "Position xy: ", self.pos, "Position Coord: ", self.coord_topleft)
def draw(self, time_passed): ## If this turns out to be the better way, don't forget to change the drawing method from self.towers.draw(self.screen) to for tower in self.towers: tower.draw()
if self.image_list:
self.frametimer.update(time_passed)
self.screen.blit(self.image, self.rect)
## # The experience bar is 15x4 px.
## #
## experience_bar_length = 15
## experience_bar_height = 4
##
## experience_percentage = get_Percentage((self.experience_req - self.last_experience_req), (self.experience - self.last_experience_req)) #float fraction, like 0.333333 (equals 33.3333% )
#.........这里部分代码省略.........