Ejemplo n.º 1
0
    def __init__(self, position, level):

        PhysicalObject.__init__(self, position)

        self.level = level
        self.controllingPlayer = physical_object.OWNER_DEFENDER

        self.physicsRect = pygame.rect.Rect(self.r_x, self.r_y, TURRET_WIDTH, TURRET_HEIGHT)

        self.image = pygame.image.load("images/defenses.png")
        self.rect = self.image.get_rect()

        self.actions = {
            "charged 0": (0, 112, TURRET_WIDTH, TURRET_HEIGHT),
            "charged 50": (TURRET_WIDTH, 112, TURRET_WIDTH, TURRET_HEIGHT),
            "charged 100": (2 * TURRET_WIDTH, 112, TURRET_WIDTH, TURRET_HEIGHT),
        }

        self.boundsRect = Rect(level.rect.x, level.rect.y, level.rect.width, constants.SCREEN_HEIGHT)

        self.action = "charged 0"
        self.area = pygame.rect.Rect(self.actions[self.action])
        # print 'turret (x,y) = ', (self.r_x, self.r_y)
        # print 'turret owner = ', self.controllingPlayer

        self.timeLeftToCharge = GUN_CHARGEUP_TIME
Ejemplo n.º 2
0
	def step(self, scrollPosition):

			#tight physics
			v_step = 0.5
			v_target = 6.0  # max. speed relative to screen

			# translate movement boundary
			self.boundsRect.y = scrollPosition

			# determine speed
			def towards(current,expected):
				if abs(expected-current) < v_step: return expected
				if current > expected:
					return current - v_step
				return current + v_step

			# continue to determine speed
			if self.DOWN and not self.UP:
				self.v_y = towards(self.v_y,v_target-constants.SCROLL_RATE)
			elif self.UP and not self.DOWN:
				self.v_y = towards(self.v_y,-v_target-constants.SCROLL_RATE)
			elif not self.UP and not self.DOWN:
				self.v_y = towards(self.v_y,-constants.SCROLL_RATE)
			if self.LEFT and not self.RIGHT:
				self.v_x = towards(self.v_x,-v_target-constants.SCROLL_RATE)
			elif self.RIGHT and not self.LEFT:
				self.v_x = towards(self.v_x,v_target-constants.SCROLL_RATE)
			elif not self.LEFT and not self.RIGHT:
				self.v_x = towards(self.v_x,0)

			# update position
			PhysicalObject.step(self, scrollPosition)

			#hard bounds fix
			if self.physicsRect.x + self.physicsRect.width > self.boundsRect.x + self.boundsRect.width:
				self.setX(self.boundsRect.x + self.boundsRect.width - self.physicsRect.width)
			if self.r_x < self.boundsRect.x:
				self.setX(self.boundsRect.x)
			if self.r_y + self.physicsRect.height > self.boundsRect.y + self.boundsRect.height:
				self.setY(self.boundsRect.y + self.boundsRect.height - self.physicsRect.height)
			if self.r_y < self.boundsRect.y:
				self.setY(self.boundsRect.y)

			#update image
			if 0 < self.v_x < (v_target / 2.0):
				self.action = "right-center"
			elif (v_target / 2.0) <= self.v_x <= v_target:
				self.action = "right"
			elif self.v_x==0:
				self.action = "center"
			elif -(v_target / 2.0) < self.v_x < 0:
				self.action = "left-center"
			elif -(v_target) < self.v_x < -(v_target/2.0):
				self.action = "left"

			self.area = pygame.rect.Rect(self.actions[self.action])

			# update weapon
			if self.timeUntilWeaponCanFireAgain > 0:
				self.timeUntilWeaponCanFireAgain -= 1
Ejemplo n.º 3
0
    def test_eq(self):
        po = PhysicalObject()
        po2 = PhysicalObject()

        self.assertTrue(po != po2)
        self.assertFalse(po == po2)

        self.assertTrue(po == po)
        self.assertFalse(po != po)
Ejemplo n.º 4
0
	def __init__(self, position, bulletkind):
		
		PhysicalObject.__init__(self, position)

		self.collisionType = physical_object.COLLISION_TYPE_BULLET
		self.physicsRect = pygame.rect.Rect(self.r_x, self.r_y, BULLET_WIDTH, BULLET_HEIGHT)

		self.image = pygame.image.load('images/bullets.png')
		self.rect = self.image.get_rect()

		self.kinds = {"shp": (0, 0, self.physicsRect.width, self.physicsRect.height),
			      "tur": (12, 0, self.physicsRect.width, self.physicsRect.height)}

		self.kind = bulletkind
		self.area = pygame.rect.Rect(self.kinds[self.kind])

		self.timeToLive = LIFE_SPAN
Ejemplo n.º 5
0
def print_debug():

    print("time: {0:.2E}  ".format(PhysicalObject.get_total_time()), end='')
    print("distance to mars: {0:.2E}\t".format(mars.distance_with(rocket)),
          end='')
    print("distance to earth: {0:.2E}   ".format(earth.distance_with(rocket)),
          end='')
    print("energy used: {0:.2E}\t".format(get_total_energy()), end='')

    # print("Net force on earth: {0:.2E} ".format(earth.get_net_force().mag))
    # print("Accln: {0:.2E} ".format(earth.accln().mag))

    print()
Ejemplo n.º 6
0
	def __init__(self, position, level):

		PhysicalObject.__init__(self, position)
		self.level = level

		self.controllingPlayer = physical_object.OWNER_ATTACKER

		self.image = pygame.image.load('images/ship.png')
		self.rect = self.image.get_rect()

		self.actions = {	"center":	(SHIP_WIDTH*2, 0, SHIP_WIDTH,SHIP_HEIGHT),
			"left":	 (SHIP_WIDTH*0, 0, SHIP_WIDTH,SHIP_HEIGHT),
			"left-center":	(SHIP_WIDTH*1, 0, SHIP_WIDTH,SHIP_HEIGHT),
			"right-center":	(SHIP_WIDTH*3, 0, SHIP_WIDTH,SHIP_HEIGHT),
			"right":	(SHIP_WIDTH*4, 0, SHIP_WIDTH,SHIP_HEIGHT)
			}

		self.action = "center"
		self.area = pygame.rect.Rect(self.actions[self.action])

		self.boundsRect = Rect(level.rect.x,level.rect.y,level.rect.width,constants.SCREEN_HEIGHT)

		self.physicsRect = pygame.rect.Rect(self.r_x, self.r_y, SHIP_WIDTH, SHIP_HEIGHT)
		self.targetType = physical_object.TARGET_TYPE_SHIP
Ejemplo n.º 7
0
 def test_is_clicked(self):
     bola = Bola((50, 50))
     self.assertTrue(bola.is_clicked(PhysicalObject((51, 52))))
     self.assertFalse(bola.is_clicked(PhysicalObject((60, 52))))
Ejemplo n.º 8
0
    earth.register(color.green)
    sun.register(color.yellow)
    mars.register(color.red)
    # moon.register(color.white)
    rocket.register(color.cyan)

    frame = 0

    lastdiff = 1e1000
    while True:
        # print(PhysicalObject.get_total_time())

        render = frame > MAIN_SKIPS

        PhysicalObject.update_bodies(render)
        add_energy(rocket.get_net_propulsion(), rocket.vel, dt)

        frame += 1

        # print((rocket.pos - mars.pos).mag)
        # print(render)
        # print(FRAME_RATE)
        print_debug()
        # print(abs((DISTANCE_BETWEEN_SUN_AND_MARS-RADIUS_OF_MARS) - rocket.pos.mag), PhysicalObject.get_total_time())
        diff = abs(rocket.pos.mag - DISTANCE_BETWEEN_SUN_AND_MARS -
                   RADIUS_OF_MARS)
        if diff > lastdiff and PhysicalObject.get_total_time() > 8000000:
            print("Difference in distance started to increase")
            print(rocket.pos, rocket.vel)
            print(mars.pos, mars.vel)
Ejemplo n.º 9
0
 def test_init(self):
     po = PhysicalObject((50, 50))
     self.assertEqual(po.pos(), (50, 50))
Ejemplo n.º 10
0
 def test_actualize(self):
     po = PhysicalObject((50, 50))
     po.actualize(1)
     self.assertEqual(po.v, Vector(0, 0))
     po.v = Vector(1, 0)
     po.actualize(1)
     self.assertEqual(po.pos(), (51, 50))
     po.actualize(1)
     self.assertEqual(po.pos(), (52, 50))
Ejemplo n.º 11
0
    def step(self, scrollPosition):

        # translate movement boundary
        self.boundsRect.y = scrollPosition

        # update self
        PhysicalObject.step(self, scrollPosition)
        if self.timeLeftToCharge < (1 / 5.0) * GUN_CHARGEUP_TIME:
            self.action = "charged 100"
        elif self.timeLeftToCharge < (3 / 5.0) * GUN_CHARGEUP_TIME:
            self.action = "charged 50"
        else:
            self.action = "charged 0"
        self.area = pygame.rect.Rect(self.actions[self.action])

        if self.physicsRect.colliderect(self.boundsRect):
            turretSeesShip = False
            target = None
            for o in self.level.physicalObjects:
                if (
                    o.controllingPlayer == physical_object.OWNER_ATTACKER
                    and o.targetType == physical_object.TARGET_TYPE_SHIP
                ):
                    turretSeesShip = True
                    target = o
            if turretSeesShip:
                self.timeLeftToCharge -= 1
                if self.timeLeftToCharge <= 0:
                    # it's the ship! get it!
                    soundEfx = pygame.mixer.Sound(constants.TURRET_BULLET_SFX)
                    soundEfx.set_volume(0.5)
                    play_sound.PlaySounds(soundEfx, 2)
                    theBullet = bullet.Bullet(
                        (
                            self.rect.x + TURRET_WIDTH / 2 - bullet.BULLET_WIDTH / 2,
                            self.rect.y + (bullet.BULLET_HEIGHT + 6),
                        ),
                        "tur",
                    )
                    theBullet.controllingPlayer = self.controllingPlayer
                    # old velocity code
                    # deltaX = o.r_x - self.r_x
                    # deltaY = o.r_y - self.r_y
                    # distance = math.hypot(deltaX, deltaY)
                    # theBullet.v_x = bullet.DEFAULT_SPEED*(deltaX/distance)  # v_x = speed*cos
                    # theBullet.v_y = bullet.DEFAULT_SPEED*(deltaY/distance)  # v_y = speed*sin
                    # new velocity code; apparently tries to divide by zero and take the square root of a negative number
                    # timeToImpact = ((o.r_x*o.v_x + o.r_y*o.v_y + math.sqrt(-pow(o.r_y,2)*(-1 + pow(o.v_x, 2)) + o.r_x*(o.r_x + 2*o.r_y*o.v_x*o.v_y - o.r_x*pow(o.v_y, 2))))/(-1 + pow(o.v_x, 2) + pow(o.v_y, 2)))
                    # theBullet.v_x = (o.r_x + timeToImpact*o.v_x)/timeToImpact
                    # theBullet.v_y = (o.r_y + timeToImpact*o.v_y)/timeToImpact
                    # new velocity code, mk. II
                    futurepos = (
                        target.r_x,
                        target.r_y,
                    )  # Guess that where they'll be in the future is where they are now
                    MY_SPEED = 1.5 + constants.SCROLL_RATE
                    for i in range(0, 4):
                        dist = (futurepos[0] - self.r_x, futurepos[1] - self.r_y)
                        timetotarget = math.hypot(dist[0], dist[1]) / bullet.DEFAULT_SPEED
                        distcovered = (target.v_x * timetotarget, target.v_y * timetotarget)
                        futurepos = (target.r_x + distcovered[0], target.r_y + distcovered[1])
                    dirNotNormalized = (futurepos[0] - self.r_x, futurepos[1] - self.r_y)
                    dirNormalized = (
                        dirNotNormalized[0] / math.hypot(dirNotNormalized[0], dirNotNormalized[1]),
                        dirNotNormalized[1] / math.hypot(dirNotNormalized[0], dirNotNormalized[1]),
                    )
                    theBullet.v_x = MY_SPEED * dirNormalized[0]
                    theBullet.v_y = MY_SPEED * dirNormalized[1]
                    # end of velocity code
                    self.childObjects.append(theBullet)
                    self.timeLeftToCharge = GUN_CHARGEUP_TIME
            else:  # if the turret doesn't see the ship,
                self.timeLeftToCharge = GUN_CHARGEUP_TIME  # then the turret should power down
Ejemplo n.º 12
0
	def step(self, scrollPosition):
		PhysicalObject.step(self, scrollPosition)
		self.timeToLive -= 1
		if(self.timeToLive <= 0):  self.destroyed = True;