예제 #1
0
파일: body.py 프로젝트: Layto888/Asteroids
 def update(self, dt):
     """ this will update Ship """
     self.vel = vect2d(0.0, 0.0)
     self.rot_vel += self.rot_vel * ROT_FRICTION
     keys = pg.key.get_pressed()
     if keys[pg.K_LEFT]:
         self.rot_vel = SHIP_ROT_VEL
     if keys[pg.K_RIGHT]:
         self.rot_vel = -SHIP_ROT_VEL
     if keys[pg.K_UP]:
         self.acc += vect2d(0, -SHIP_MAX_VEL).rotate(-self.rot)
         # play on a specific channel the acceleration sound
         self.res.play_fx(THRUSTER_FX, self.engine.snd[THRUSTER_FX])
     else:
         self.engine.snd[THRUSTER_FX].fadeout(TIME_FADE_OUT)
     if keys[pg.K_SPACE]:
         # create a new bullet when the space bar is pressed : shoot.
         now = pg.time.get_ticks()
         if now - self.last_shot > BULLET_SHOT_RATE:
             self.last_shot = now
             self.engine.snd[BULLET_FX].play()
             Bullet(self.engine, self.pos, self.rot)
     # movement equation of the ship with space friction / acceleration.
     self.rotate(dt)
     self.translate(dt)
     # keep the ship in the window
     bondary_limit(self.pos)
예제 #2
0
 def game_over(self):
     pg.mixer.music.stop()
     if self.bird.alive:
         self.snd[FX_HIT].play()
         self.bird.alive = False
     # stop the scrolling
     for i in range(2):
         self.ground[i].vel = vect2d(0.0, 0.0)
         self.pipe[i][0].vel = vect2d(0.0, 0.0)
         self.pipe[i][1].vel = vect2d(0.0, 0.0)
예제 #3
0
파일: body.py 프로젝트: Layto888/Asteroids
 def __init__(self, engine, pos, ship_rotation):
     self.groups = engine.all_sprites, engine.bullet_sprites
     pg.sprite.Sprite.__init__(self, self.groups)
     self.radius = 1
     self.image = engine.img[BULLET_IMG]
     self.rect = self.image.get_rect()
     self.pos = vect2d(pos)
     self.rect.center = pos
     self.vel = vect2d(0, 1).rotate(-ship_rotation) * BULLET_VEL
     self.spawn_time = pg.time.get_ticks()
예제 #4
0
 def __init__(self, engine, pos, radius):
     self.groups = engine.all_sprites, engine.particles_sprites
     pg.sprite.Sprite.__init__(self, self.groups)
     self.image = engine.img[BULLET_IMG]
     self.radius = radius * 0.35
     self.rect = self.image.get_rect()
     self.pos = vect2d(0, self.radius).rotate(uniform(0.0, 360.0))
     self.vel = self.pos.normalize() * uniform(PARTICLE_MIN_SPEED,
                                               PARTICLE_MAX_SPEED)
     self.pos += vect2d(pos)
     self.start_time = pg.time.get_ticks()
예제 #5
0
 def __init__(self, image, fx):
     super().__init__()
     self.frame = 0
     self.image_group = image
     self.image = image[0]
     self.pos = vect2d(WIDTH / 2.0, HEIGHT / 2.0)
     self.rect = self.image.get_rect()
     self.rect.center = vect2d(self.pos)
     self.vel = vect2d(0.0, 0.0)
     self.acc = vect2d(0.0, BIRD_GRAVITY)
     self.last_update = pg.time.get_ticks()
     self.frame_rate = 90
     self.alive = True
     self.wing = fx
예제 #6
0
 def __init__(self, pos, vel, radius, color, duration):
     self.x, self.y = pos
     self.vel = vect2d(vel)
     self.radius = radius
     self.color = color
     self.duration = duration
     self.startTime = pg.time.get_ticks()
예제 #7
0
파일: body.py 프로젝트: Layto888/Asteroids
 def __init__(self, engine):
     self.groups = engine.all_sprites, engine.ship_sprites
     pg.sprite.Sprite.__init__(self, self.groups)
     self.engine = engine
     self.image = engine.img[SHIP_IMG]
     self.img_copy = self.image
     self.radius = 12
     self.rect = self.image.get_rect()
     self.pos = vect2d(WIDTH / 2, HEIGHT / 2)
     self.vel = vect2d(0.0, 0.0)
     self.acc = vect2d(0.0, 0.0)
     # self.thrust = self.right = self.left = False
     self.rot = 0.0
     self.rot_vel = 0.0
     self.last_shot = 0
     self.res = Resources()
     self.last_respawn = pg.time.get_ticks()
예제 #8
0
 def update(self):
     if not self.playing and self.running:
         self.new_demo()
     # recycle the pipes
     if self.pipe[0][DOWN].x < -50:
         self.reset_pipe(self.pipe[0], PIPE_X)
     if self.pipe[1][DOWN].x < -50:
         self.reset_pipe(self.pipe[1], PIPE_X)
     # see if the bird hurt any pipe
     if self.collide(self.bird, self.pipe[0]):
         self.game_over()
     # when the bird hit the ground
     if self.collide(self.bird, self.ground):
         self.bird.vel = vect2d(0.0, 0.0)
         self.bird.acc = vect2d(0.0, 0.0)
         self.game_over()
     self.check_score(self.bird, self.pipe)
     self.all_sprites.update(self.dt)
예제 #9
0
def rotate2p(v1: vect2d, v2: vect2d, angle: float) -> vect2d:
    """
    this function rotates a point p1
    around the point p0 with a certain angle."""
    dx = v2.x - v1.x
    dy = v2.y - v1.y
    vector = vect2d((dx * math.cos(angle) - dy * math.sin(angle)),
                    (dx * math.sin(angle) + dx * math.cos(angle)))
    vector += v1

    return vector
예제 #10
0
def rotate2p(v1, v2, angle):
    """
    this function rotates a point p1
    around the point p0 with a certain angle.
    The angle is in radian.
    """
    dx = v2.x - v1.x
    dy = v2.y - v1.y
    vector = vect2d((dx * math.cos(angle) - dy * math.sin(angle)),
                    (dx * math.sin(angle) + dy * math.cos(angle)))
    vector += v1

    return vector
예제 #11
0
파일: body.py 프로젝트: Layto888/Asteroids
    def __init__(self, engine, astr_type, pos=None):
        self.groups = engine.all_sprites, engine.asteroid_sprites
        pg.sprite.Sprite.__init__(self, self.groups)
        self.type = astr_type
        # switch types
        if self.type == TYPE_BIG:
            self.radius = 50
            self.image = engine.img[randint(BIGAS1_IMG, BIGAS3_IMG)]
            self.rect = self.image.get_rect()
            self.pos = vect2d(WIDTH / 2, HEIGHT / 2)
            # asteroids must start far from the ship
            while dist(self.pos.x, self.pos.y,
                       WIDTH / 2.0, HEIGHT / 2.0) < self.radius * 7.0:
                self.pos = vect2d(randint(0, WIDTH), randint(0, HEIGHT))

            self.vel = vect2d(0, 1).rotate(
                uniform(-ASTR_ROT_VEL, ASTR_ROT_VEL)) * BIG_ASTR_VEL
        elif self.type == TYPE_MEDIUM:
            self.radius = 25
            self.image = engine.img[randint(MIDAS1_IMG, MIDAS3_IMG)]
            self.rect = self.image.get_rect()
            self.pos = vect2d(pos)
            self.vel = vect2d(0, 1).rotate(
                uniform(-ASTR_ROT_VEL, ASTR_ROT_VEL)) * MEDIUM_ASTR_VEL
        elif self.type == TYPE_SMALL:
            self.radius = 12
            self.image = engine.img[randint(SMLAS1_IMG, SMLAS3_IMG)]
            self.rect = self.image.get_rect()
            self.pos = vect2d(pos)
            self.vel = vect2d(0, 1).rotate(
                uniform(-ASTR_ROT_VEL, ASTR_ROT_VEL)) * SMALL_ASTR_VEL

        self.img_copy = self.image
        # for rotation
        self.rot = 0.0
        self.rot_vel = uniform(-ASTR_ROT_VEL, ASTR_ROT_VEL)
예제 #12
0
 def __init__(self, image, pos=vect2d(BKGRND_POS)):
     super().__init__(image, pos)
예제 #13
0
 def reset_pipe(pipe, pos_x):
     pipe[0].pos = vect2d(pos_x, randint(*PIPE_POS))
     pipe[1].pos = vect2d(pos_x, pipe[0].y + PIPES_DIST)
     pipe[0].new_pipe = True
     pipe[1].new_pipe = True
예제 #14
0
 def __init__(self, image, pos=(0.0, 0.0)):
     super().__init__(image, pos)
     self.vel = vect2d(PIPE_VEL, 0.0)
     self.new_pipe = False
예제 #15
0
 def __init__(self, image, pos):
     super().__init__(image, pos)
     self.vel = vect2d(GRND_VEL, 0.0)
예제 #16
0
class Body(object):
    def __init__(self, x, y, w, h):
        self.x = x
        self.y = y
        self.w = w
        self.h = h


@pytest.fixture
def null_body():
    return Body(0, 0, 0, 0)


@pytest.mark.parametrize("vector1, vector2, expected",
                         [(vect2d(-1.0, 1.0), vect2d(-1.0, 1.0), 0.0),
                          (vect2d(-2.1, 3.0), vect2d(1.0, -5.1), 8.672),
                          (vect2d(-1.3, -2.0), vect2d(2.0, 1.3), 4.666)])
def test_dist(vector1, vector2, expected):
    assert (util.dist(vector1, vector2) == pytest.approx(expected, 0.01))


@pytest.mark.parametrize("rad, expected", [(0, 0), (33.3, 1907.94),
                                           (0.81, 46.40)])
def test_rad2deg(rad, expected):
    assert (util.rad2deg(rad) == pytest.approx(expected, 0.01))


@pytest.mark.parametrize("deg, expected", [(0, 0), (25.2, 0.43),
                                           (0.28, 0.0048)])
def test_deg2rad(deg, expected):
예제 #17
0
 def __init__(self, pos, vel, color, r):
     self.x, self.y = pos
     self.vel = vect2d(vel)
     self.color = color
     self.radius = r
예제 #18
0
 def __init__(self, image, pos=(0.0, 0.0)):
     super().__init__()
     self.image = image
     self.rect = self.image.get_rect()
     self.pos = vect2d(pos)
     self.rect.center = vect2d(pos)