def shake(k): try: if k==0:return vec2(-10,-10) if k==1:return vec2(random.randint(-20,0), random.randint(-20,0)) else:return vec2(random.randint(-10-int(10*k),0-int(10*k)), random.randint(-10-int(10*k),0-int(10*k))) except ValueError: print k
def __init__(self, world, img, pos): self.world = world self.pos = vec2(pos) self.image = img self.img = img self.img.set_colorkey((255, 0, 255)) self.acc_const = 5 self.bullet_list = self.world.bullet_list self.reload_max = .15 self.reload = self.reload_max self.firewall_reload_max = 5 self.firewall_reload = self.firewall_reload_max self.num_fullscan = 1 self.fullscan_reload_max = 1 self.fullscan_reload = self.fullscan_reload_max self.mxy = vec2(0, 0) self.dxy = vec2(0, 0) self.rect = self.img.get_rect() self.mask = pygame.mask.from_surface(self.img) self.speed = 300 self.health = 100 self.health_max = self.health self.invulnerable = False self.velocity = vec2() self.acceleration = 5 self.points = 0
def do_actions(self, tick): if self.enemy.dead == True: self.enemy.world.game_win = True self.enemy.reload -= tick if self.enemy.reload <= 0: """fire""" dx = self.enemy.pos.x - self.player.pos.x dy = self.enemy.pos.y - self.player.pos.y for i in xrange(25): angle = random.randint(0,360) vel = vec2(cos(angle), sin(angle))*-300 self.enemy.bullet_list.append(Shot.Shot(self.enemy.pos.copy(), angle, vel)) self.enemy.reload = self.enemy.reload_max if self.enemy.animate == False: if random.randint(0,int((1/tick)*1))==0: self.enemy.animate = True else: self.enemy.img = self.enemy.lst[self.enemy.ani.get_full_frame(tick)] self.enemy.img.set_colorkey((255,0,255)) if self.enemy.ani.run == 1: for i in range(2): angle = radians(random.randint(0,359)) dist = random.randint(40, 100) vec = self.enemy.pos.copy() + vec2(cos(angle) * dist, sin(angle) * dist) if random.randint(0, 1): self.enemy.world.enemy_list.append(Spam(self.enemy.world, vec)) else: self.enemy.world.enemy_list.append(Virus(self.enemy.world, vec)) if self.enemy.ani.finished == True: self.enemy.ani.reset() self.enemy.animate = False
def explode(self, playerPos, group): normalizedDirection = vec2().from_points(self.pos, playerPos)*-1 def rO(): return randint(-10,10) group.add(enemy(self.pos+normalizedDirection+vec2(rO(),rO()))) group.add(enemy(self.pos+normalizedDirection+vec2(rO(),rO()))) self.kill()
def __init__(self, pos): pygame.sprite.Sprite.__init__(self) self.image = pygame.Surface((32,32)) self.rect = self.image.get_rect() self.pos = vec2(*pos) self.vel = vec2(600, 0) self.rect.center = self.pos self.grounded = False self.falling = True self.slammed = False self.image.fill((255,255,255)) pygame.draw.rect(self.image, (0,0,0), (0,0,32,32), 1)
def update(self, tick): """ called every frame, the tick is the time passed since the last frame. Multiplying movement by it causes movement to be the same speed regardless of how fast a computer is (not good when frame rate is low)""" self.brain.think(tick) self.velocity *= 0.99 capped_vel = vec2() if self.velocity.x < -10: capped_vel.x = -10 elif self.velocity.x > 10: capped_vel.x = 10 else: capped_vel.x = self.velocity.x if self.velocity.y < -10: capped_vel.y = -10 elif self.velocity.y > 10: capped_vel.y = 10 else: capped_vel.y = self.velocity.y #capped_vel = self.cap(self.velocity, -10, 10) self.pos += capped_vel self.rect.center = self.pos self.mask = pygame.mask.from_surface(self.img) """
def __init__(self, world, pos): """Base variables""" self.world = world self.pos = pos self.player = self.world.player self.acceleration = 5 self.max_range = 300 self.attacking_range = 150 self.scared_range = 100 self.bullet_list = self.world.bullet_list self.invulnerable = False self.reload_max = 5 self.reload = self.reload_max self.velocity = vec2() self.target = self.player self.mask = None self.look_ahead = 50 self.health = 50 self.health_max = self.health """AI variables""" self.brain = StateMachine() self.radius = 25 self.hit_this_frame = False self.visible = True
def __init__(self,x,z,h): self.z = z self.color = (z+2)*10 self.width,self.height = (10-z)*5,randint((10-z)*15,(10-z)*25) self.pos = vec2(x,h-z*30) self.rect = pygame.Rect(0,0,self.width, self.height) self.rect.bottomleft = self.pos
def cap(self, vec, lower, upper): """caps a vector to a certain threshold""" new_vec = vec2() new_vec.x = max(lower, min(vec.x, upper)) new_vec.y = max(lower, min(vec.y, upper)) return new_vec
def do_actions(self, tick): self.enemy.reload -= tick self.enemy.img = self.enemy.image if self.enemy.reload <= 0: """fire""" self.enemy.img = self.enemy.lst[self.enemy.ani.get_frame(tick)] self.enemy.image = self.enemy.img self.enemy.img.set_colorkey((255, 0, 255)) if self.enemy.ani.finished == True: self.enemy.ani.finished = False dx = self.enemy.pos.x - self.player.pos.x dy = self.enemy.pos.y - self.player.pos.y for i in xrange(10): a = random.randint(int(dx-60), int(dx+60)) b = random.randint(int(dy-60), int(dy+60)) angle = atan2(b, a) vel = vec2(cos(angle), sin(angle)) * -350 temps = Shot.Shot(self.enemy.pos.copy(), angle, vel) temps.img = self.b_img temps.img.set_colorkey((255, 0, 255)) temps.img = pygame.transform.rotate(temps.img, 180 - degrees(temps.angle)) temps.rect = temps.img.get_rect() self.enemy.bullet_list.append(temps) self.enemy.reload = self.enemy.reload_max self.enemy.ani.reset() self.fired = True
def do_actions(self, tick): self.enemy.velocity += self.enemy.get_vector_to_target() * self.enemy.acceleration * tick if self.enemy.get_dist_to(self.enemy.target.pos) < 25: angle = radians(random.randint(0, 359)) self.enemy.target = RoamPoint(self.enemy.pos + vec2(cos(angle), sin(angle)) * random.randint(50, 250))
def __init__(self, pos): pygame.sprite.Sprite.__init__(self) self.health = 100 self.pos = vec2(*pos) self.image = enemyImg self.rect = self.image.get_rect() self.rect.center = self.pos self.speed = 1
def do_actions(self, tick): v_lst = [] self.enemy.food += tick for i in self.enemy.world.enemy_list: try: if i.max_virus == self.enemy.max_virus: v_lst.append(i) except Exception: pass if self.enemy.food > 3.14159 and len(v_lst) < self.enemy.max_virus: new_virus = Virus(self.enemy.world, self.enemy.pos.copy()+vec2(random.random(), random.random())) self.enemy.food = 0 self.enemy.world.enemy_list.append(new_virus)
def update_collisions(self, enemy_list): if self.__class__.__name__ == "Boss": return for i, current_enemy in enumerate(enemy_list): if current_enemy is self or current_enemy.__class__.__name__ == "Boss": continue current_vec = vec2().from_points(self.pos, current_enemy.pos) if current_vec.get_magnitude() < self.radius + current_enemy.radius: self.pos -= 0.1 * current_vec current_enemy.pos += 0.1 * current_vec self.rect.center = self.pos current_enemy.rect.center = current_enemy.pos
def shoot(self, pos): if self.reload <= 0: self.world.sound_classes[2].play() angle = self.get_angle(pos) vel = vec2(math.cos(angle), math.sin(angle)) * 500 x = self.pos.copy()[0]+(math.cos(angle)*20) y = self.pos.copy()[1]+(math.sin(angle)*20) self.world.instantiate_projectile((x,y), angle, vel, False, True) self.velocity -= vel * .0003 self.reload = self.reload_max
def __init__(self, pos): pygame.sprite.Sprite.__init__(self) self.width = 500 + random.randint(0, 250) + random.randint(0,250) self.image = pygame.Surface((self.width, 20)) self.image.fill((255,255,255)) self.rect = self.image.get_rect() pygame.draw.rect(self.image, (0,0,0), self.rect, 1) self.pos = vec2(*pos) self.rect.topleft = self.pos
def do_actions(self, tick): """flying towards the player while firing""" self.enemy.velocity += self.enemy.get_vector_to_target() * self.enemy.acceleration * tick self.enemy.reload -= tick if self.enemy.reload <= 0 and self.enemy.get_dist_to(self.player.pos) < self.enemy.attacking_range and self.enemy.attacking_range!=0: """fire""" dx = self.enemy.pos.x - self.player.pos.x dy = self.enemy.pos.y - self.player.pos.y angle = atan2(dy, dx) vel = vec2(cos(angle), sin(angle))*-250 self.enemy.bullet_list.append(Shot.Shot(self.enemy.pos.copy(), angle, vel)) self.enemy.reload = self.enemy.reload_max
def firewall(self, pos): if self.firewall_reload <= 0: num_shots = 10 for i in xrange(num_shots): angle = self.get_angle(pos) + math.radians(((num_shots / 2) - i) * (60.0 / num_shots)) vel = vec2(math.cos(angle), math.sin(angle)) * 500 x = self.pos.copy()[0] + (math.cos(angle) * 20) y = self.pos.copy()[1] + (math.sin(angle) * 20) self.world.instantiate_projectile((x, y), angle, vel, False, True, True) self.velocity -= vel * .0005 self.firewall_reload = self.firewall_reload_max
def fullscan(self, pos): if self.num_fullscan > 0 and self.fullscan_reload <= 0: num_shots = 500 for i in xrange(num_shots): angle = math.radians(((num_shots / 2) - i) * 360.0 / num_shots) vel = vec2(math.cos(angle), math.sin(angle)) * 500 x = self.pos.copy()[0] + (math.cos(angle) * 20) y = self.pos.copy()[1] + (math.sin(angle) * 20) self.world.instantiate_projectile((x, y), angle, vel, False, True, False) self.num_fullscan -= 1 self.fullscan_reload = self.fullscan_reload_max
def do_actions(self, tick): """backing away while firing""" self.enemy.velocity -= self.enemy.get_vector_to_target() * self.enemy.acceleration * tick self.enemy.reload -= tick if self.enemy.reload <= 0: """fire""" dx = self.enemy.pos.x - self.player.pos.x dy = self.enemy.pos.y - self.player.pos.y angle = atan2(dy, dx) vel = vec2(cos(angle), sin(angle))*-250 x = self.enemy.copy()[0]+(math.cos(angle)*20) y = self.enemy.copy()[1]+(math.sin(angle)*20) self.enemy.bullet_list.append(Shot.Shot(self.enemy.pos.copy(), angle, vel)) self.enemy.reload = self.enemy.reload_max
def do_actions(self, tick): magnitude = vec2(self.enemy.pos - self.enemy.target.pos).get_magnitude() self.enemy.img.set_alpha(200 - magnitude) self.enemy.velocity += self.enemy.get_vector_to_target() * self.enemy.acceleration * tick
import math pygame.init() pygame.font.init() font1 = pygame.font.Font(None, 25) screen_size = 800,600 w,h = screen_size screen = pygame.display.set_mode(screen_size) clock = pygame.time.Clock() whole_surface = pygame.Surface((w+20,h+20)) whole_w,whole_h = w+20,h+20 draw_pos = vec2(-10,-10) p1 = player((whole_w/2,whole_h-46)) floor_group = pygame.sprite.Group(floor((whole_w/2,whole_h-30))) gravity = 350 time = 0 distance = 0 shakeTimer = 0.25; maxShakeTimer = shakeTimer; slamming = False lose = False
import pygame from pygame.locals import * pygame.init() from random import randint from vector2 import Vector2 as vec2 w,h = screen_size = (640,480) center = vec2(w/2,h/2) screen = pygame.display.set_mode(screen_size) enemyImg = pygame.image.load("GenericEnemy.png").convert() class enemy(pygame.sprite.Sprite): def __init__(self, pos): pygame.sprite.Sprite.__init__(self) self.health = 100 self.pos = vec2(*pos) self.image = enemyImg self.rect = self.image.get_rect() self.rect.center = self.pos self.speed = 1 def update(self, tp, playerPos): normalizedDirection = vec2().from_points(self.pos, playerPos) self.pos+=normalizedDirection*self.speed*tp self.rect.center = self.pos
def clamp_vec(self, vec): new_vec = vec2() new_vec.x = max(0, min(vec.x, screen_size.x)) new_vec.y = max(0, min(vec.y, screen_size.y)) return new_vec
import pygame, math, Shot from vector2 import Vector2 as vec2 debug = False screen_size = vec2(1000, 600) class Player(object): def __init__(self, world, img, pos): self.world = world self.pos = vec2(pos) self.image = img self.img = img self.img.set_colorkey((255, 0, 255)) self.acc_const = 5 self.bullet_list = self.world.bullet_list self.reload_max = .15 self.reload = self.reload_max self.firewall_reload_max = 5 self.firewall_reload = self.firewall_reload_max self.num_fullscan = 1 self.fullscan_reload_max = 1 self.fullscan_reload = self.fullscan_reload_max self.mxy = vec2(0, 0) self.dxy = vec2(0, 0)
def update(self, tp, playerPos): normalizedDirection = vec2().from_points(self.pos, playerPos) self.pos+=normalizedDirection*self.speed*tp self.rect.center = self.pos
def entry_actions(self): angle = radians(random.randint(0, 359)) self.enemy.target = RoamPoint(self.enemy.pos+vec2( cos(angle),sin(angle))*random.randint(25, 150))
def __init__(self, world): self.world = world self.offset = vec2()
def __init__(self, handler): self.handler = handler self.offset = vec2()
def entry_actions(self): self.enemy.img = self.enemy.lst[0] self.enemy.img.set_colorkey((255,0,255)) angle = radians(random.randint(0, 359)) self.enemy.target = RoamPoint(self.enemy.pos + vec2( cos(angle),sin(angle)) * random.randint(25, 150))
def render(self, surface): surface.blit(self.image, self.handler.camera.offset + vec2((16*self.pos.x)-(16*self.pos.y), (8*self.pos.x)+(8*self.pos.y)))#-(32*self.pos.z)))
def render(self, surface): surface.blit(self.image, self.handler.camera.offset + vec2( (16 * self.pos.x) - (16 * self.pos.y), (8 * self.pos.x) + (8 * self.pos.y))) #-(32*self.pos.z)))