def __init__(self): """setup of the game class.""" pygame.init() self.screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) self.clock = pygame.time.Clock() pygame.display.set_caption("boids") self.done = False self.boid_list = [] self.hoik_list = [] self.obsticle_list = [] self.all_things_to_draw = [] #creating instances of boids for i in range(BOID_COUNT): self.boid = Boids( Vector2D(randrange(0, SCREEN_WIDTH), randrange(0, SCREEN_HEIGHT)), Vector2D(randrange(MIN_SPEED, MAX_SPEED), randrange(MIN_SPEED, MAX_SPEED)), BLACK, BOID_RADIUS) self.boid_list.append(self.boid) #creating instances of hoiks for i in range(HOIK_COUNT): self.hoik = Hoiks( Vector2D(randrange(0, SCREEN_WIDTH), randrange(0, SCREEN_HEIGHT)), Vector2D(randrange(MIN_SPEED, MAX_SPEED), randrange(MIN_SPEED, MAX_SPEED)), RED, BOID_RADIUS) self.hoik_list.append(self.hoik) #appending boidslist and hoikslist to a list of both list, allowing us to easily draw both lists self.all_things_to_draw.append(self.boid_list) self.all_things_to_draw.append(self.hoik_list)
def spawn_ammo(self): if len(self.ammoList) == 0: rand = randrange(0, 10000) if rand < 150: ammo = Ammo(Vector2D(randrange(0, SCREEN_W), -10), Vector2D(randrange(-10, 10), randrange(2, 10))) self.all_sprites.add(ammo) self.ammoList.append(ammo)
def __init__(self): """Initializing all attributes 'Arrow' needs. """ self.pos = Vector2D(cf.X_POS, cf.Y_POS) # To the 'north' self.dir = Vector2D(0, -1) self.vel = 0.0 self.two_d_pos = Vector2D(0, 0)
def shoot(self, key, projlist, sprlist): if key == True: if self.ammo > 0: projectile = Projectile( Vector2D((self.rect.centerx), (self.rect.centery)), Vector2D(self.speed2.x, self.speed2.y)) sprlist.add(projectile) projlist.append(projectile) self.ammo -= 1
def handle_events(self): """handles the quit event, and creates a new obsticle if u click the mouse.""" events = pygame.event.get() for event in events: #event for quiting if event.type == pygame.QUIT: self.done = True #event for creating a new obsticle when clicking the mousebutton if event.type == pygame.MOUSEBUTTONDOWN: coordinate = pygame.mouse.get_pos() self.obsticle = Boids(Vector2D(coordinate[0], coordinate[1]), Vector2D(0, 0), RED, OBSTICLE_RADIUS) self.obsticle_list.append(self.obsticle)
def setup(self): self.left = False self.right = False self.forward = False self.stop = False self.shoot = False self.win = False self.score = 0 self.rocket = Rocket(Vector2D((SCREEN_W / 2), (SCREEN_H / 2)), Vector2D(0, 0), Vector2D(0, 0)) self.all_sprites = pygame.sprite.Group() self.all_proj = [] self.all_obs = [] self.ammoList = [] self.all_sprites.add(self.rocket)
def forward(self, key): angle = (self.angle + 90) rad = (math.pi / 180) * angle x = math.cos(rad) y = -math.sin(rad) #adding x and y to fint top of ship tmpx = self.rect.centerx + x * 1.5 tmpy = self.rect.centery + y * 1.5 #desired = target - location speedx = (tmpx - self.rect.centerx) speedy = (tmpy - self.rect.centery) #if W/Key-up is pushed and has fuel, add to speed if key == True: self.speed += Vector2D(speedx, speedy) self.speed2 = Vector2D((speedx * 20), (speedy * 20))
def draw(self, screen): """Draw a polygon that takes the shape of a big arrow. Makes the polygon from seven 'Vector2D' objects that are all based on the 'dir'-attribute of the arrow. Arguments: screen {pygame surface} -- the surface that the polygon shall be drawn onto """ try: # Every point is rotated from 'dir' about the center of the polygon, # and are then scaled out to give it the correct shape of an arrow. one = self.dir.rotate(cf.ROTATION[0]).normalized() * cf.SCALING[0] two = self.dir.rotate(cf.ROTATION[1]).normalized() * cf.SCALING[1] three = self.dir.rotate( cf.ROTATION[2]).normalized() * cf.SCALING[2] four = self.dir.rotate(cf.ROTATION[3]).normalized() * cf.SCALING[3] five = self.dir.rotate(cf.ROTATION[4]).normalized() * cf.SCALING[4] six = self.dir.rotate(cf.ROTATION[5]).normalized() * cf.SCALING[5] seven = self.dir.rotate( cf.ROTATION[6]).normalized() * cf.SCALING[6] pygame.draw.polygon(screen, (cf.POLYGON_COLOR), ((self.pos.x + one.x, self.pos.y + one.y), (self.pos.x + two.x, self.pos.y + two.y), (self.pos.x + three.x, self.pos.y + three.y), (self.pos.x + four.x, self.pos.y + four.y), (self.pos.x + five.x, self.pos.y + five.y), (self.pos.x + six.x, self.pos.y + six.y), (self.pos.x + seven.x, self.pos.y + seven.y))) except ReturnZero: # In case we try to normalize a null-vector, # e.g. if 'dir' has a length of zero. one = Vector2D(0, 0) two = Vector2D(0, 0) three = Vector2D(0, 0) four = Vector2D(0, 0) five = Vector2D(0, 0) six = Vector2D(0, 0) seven = Vector2D(0, 0)
def spawn_obs(self): if len(self.all_obs) < 10: obs = Obsticles(Vector2D(randrange(0, SCREEN_W), -10), Vector2D(randrange(-10, 10), randrange(2, 10))) self.all_sprites.add(obs) self.all_obs.append(obs)