def update(self): self.prevpos = V.vector(self.rect.center).copy() self.state.execute(self) ## Add a tiny force toward the center of the field if False: #self.state != S.Wait(): center = V.vector(pygame.display.get_surface().get_rect().center) towardcenter = center - self.rect.center V.normalize_ip(towardcenter) self.steering += 0.5 * towardcenter self.velocity += FORCE_PER_TICK * self.steering / (self.mass) V.truncate(self.velocity, self.maxspeed) speed = V.length(self.velocity) if speed > 0.001: self.heading = V.normalize(self.velocity) self.rect.center += self.velocity self.depth = -self.rect.centery #determine which image direction to use, based on heading and velocity: if speed >= 0.001: small = 0.382 # sin(22.5 degrees) big = 0.923 # cos(22.5 degrees) x, y = self.heading if y >= big: self.dir = 's' elif small <= y: if x > 0: self.dir = 'se' else: self.dir = 'sw' elif -small <= y: if x > 0: self.dir = 'e' else: self.dir = 'w' elif -big <= y: if x > 0: self.dir = 'ne' else: self.dir = 'nw' else: self.dir = 'n' else: self.velocity[0] = self.velocity[1] = 0.0 #image action: stopped or moving? if speed < 0.001: self.aspeed = 0.5 action = 'looking' else: self.aspeed = 0.2 * speed action = 'walking' self.images = Images.get(self.name)[self.dir][action] self.nframes = len(self.images) #advance animation frame self.frame = self.aspeed + self.frame while self.frame >= self.nframes: self.frame -= self.nframes self.image = self.images[int(self.frame)]
def update(self): self.prevpos = V.vector(self.rect.center).copy() self.state.execute(self) ## Add a tiny force toward the center of the field if False:#self.state != S.Wait(): center = V.vector(pygame.display.get_surface().get_rect().center) towardcenter = center - self.rect.center V.normalize_ip(towardcenter) self.steering += 0.5*towardcenter self.velocity += FORCE_PER_TICK*self.steering/(self.mass) V.truncate(self.velocity, self.maxspeed) speed = V.length(self.velocity) if speed > 0.001: self.heading = V.normalize(self.velocity) self.rect.center += self.velocity self.depth = -self.rect.centery #determine which image direction to use, based on heading and velocity: if speed >= 0.001: small = 0.382 # sin(22.5 degrees) big = 0.923 # cos(22.5 degrees) x,y = self.heading if y >= big: self.dir = 's' elif small <= y: if x > 0: self.dir = 'se' else: self.dir = 'sw' elif -small <= y: if x > 0: self.dir = 'e' else: self.dir = 'w' elif -big <= y: if x > 0: self.dir = 'ne' else: self.dir = 'nw' else: self.dir = 'n' else: self.velocity[0] = self.velocity[1] = 0.0 #image action: stopped or moving? if speed < 0.001: self.aspeed = 0.5 action = 'looking' else: self.aspeed = 0.2*speed action = 'walking' self.images = Images.get(self.name)[self.dir][action] self.nframes = len(self.images) #advance animation frame self.frame = self.aspeed+self.frame while self.frame >= self.nframes: self.frame -= self.nframes self.image = self.images[int(self.frame)]
def __init__(self, name, pos, maxspeed=PLAYER_MAXSPEED, screen=None): AutoObject.__init__(self, pos) # Image and rect vars self.name = name self.images = Images.get(self.name)['e']['looking'] self.imagesize = self.images[0].get_size() w, h = self.imagesize self.rect = pygame.Rect(0, 0, Player.wfrac * w, Player.hfrac * h) self.rect.center = V.vector(pos) # Physics vars self.obstacles = [] self.state = S.Wait() self.maxspeed = maxspeed self.maxforce = maxspeed * 0.1 self.velocity = V.vector(0, 0) self.steering = V.vector(0, 0) randang = random.uniform(0, 2 * math.pi) x, y = math.cos(randang), math.sin(randang) self.heading = V.vector(x, y) self.dir = 's' # Animation vars self.depth = 0 self.nframes = len(self.images) self.frame = random.randint(0, self.nframes - 1) self.mass = 1.0 # Vars that should be in the state (IMHO): wanderangle = random.uniform(0, 2 * math.pi) self.wanderpoint = V.vector(math.cos(wanderangle), math.sin(wanderangle)) V.normalize_ip(self.wanderpoint) self.seektarget = None self.seektarget2 = None
def __init__(self, name, pos, maxspeed=PLAYER_MAXSPEED, screen=None): AutoObject.__init__(self, pos) # Image and rect vars self.name = name self.images = Images.get(self.name)['e']['looking'] self.imagesize = self.images[0].get_size() w,h = self.imagesize self.rect = pygame.Rect(0, 0, Player.wfrac*w, Player.hfrac*h) self.rect.center = V.vector(pos) # Physics vars self.obstacles = [] self.state = S.Wait() self.maxspeed = maxspeed self.maxforce = maxspeed*0.1 self.velocity = V.vector(0,0) self.steering = V.vector(0,0) randang = random.uniform(0, 2*math.pi) x,y = math.cos(randang), math.sin(randang) self.heading = V.vector(x,y) self.dir = 's' # Animation vars self.depth = 0 self.nframes = len(self.images) self.frame = random.randint(0,self.nframes-1) self.mass = 1.0 # Vars that should be in the state (IMHO): wanderangle = random.uniform(0, 2*math.pi) self.wanderpoint = V.vector(math.cos(wanderangle), math.sin(wanderangle)) V.normalize_ip(self.wanderpoint) self.seektarget = None self.seektarget2 = None