コード例 #1
0
ファイル: entitymgr.py プロジェクト: JakubBerlinski/cs381
class Entity:
	def __init__(self, sceneManager, name, mesh = "", pos = ogre.Vector3(0,0,0), vel = ogre.Vector3(0,0,0), heading = 0, speed = 0, desiredSpeed = 0, desiredHeading = 0, turningRate = 0):
		self.name = name
		self.mesh = mesh
		self.pos = pos
		self.vel = vel
		self.heading = heading
		self.speed = speed
		self.desiredSpeed = desiredSpeed
		self.desiredHeading = desiredHeading
		self.turningRate = turningRate
		self.sceneManager = sceneManager
		self.physics = Physics(self)
		self.renderable = Renderable(self, self.sceneManager)

	def tick(self, time):
		self.physics.tick(time)
		self.renderable.tick()
コード例 #2
0
class Game(object):
    def __init__(self):

        # Settings

        pygame.mixer.init()

        pygame.mixer.music.load('latenight.ogg')
        pygame.mixer.music.play(0)

        self.WIDTH = 640
        self.HEIGHT = 360

        # Config
        self.tps_max = 100

        # Initialization
        pygame.init()
        font = pygame.font.SysFont("Arial", 18)
        self.resolution = (self.screen_width,
                           self.screen_height) = (self.WIDTH, self.HEIGHT)
        self.screen = pygame.display.set_mode(self.resolution,
                                              pygame.RESIZABLE)

        self.tps_clock = pygame.time.Clock()
        self.tps_delta = 0.0
        self.scroll = Vector2(0, 0)

        self.map = Map(self)
        self.player = Player(
            self
        )  # przy inicjalizacji przekazuje playerowi wszystko Player(self)
        self.enemy = Enemy(self)
        self.weapon = Weapon(self)
        self.fire = Fire(self)
        self.physics = Physics(self)
        self.platforms = Platforms(self)
        self.collision = Collision(self)
        self.sprite = Sprite(self)
        self.menu = Menu(self)
        self.file_loader = FileLoader(self)

        self.sprite.load_images()

        def create_fonts(font_sizes_list):
            "Creates different fonts with one list"
            fonts = []
            for size in font_sizes_list:
                fonts.append(pygame.font.SysFont("Arial", size))
            return fonts

        def render(fnt, what, color, where):
            "Renders the fonts as passed from display_fps"
            text_to_show = fnt.render(what, 0, pygame.Color(color))
            self.screen.blit(text_to_show, where)

        def display_fps():
            "Data that will be rendered and blitted in _display"
            render(fonts[0],
                   what=str(int(self.tps_clock.get_fps())),
                   color="white",
                   where=(0, 0))

        fonts = create_fonts([32, 16, 14, 8])
        while True:

            # Events
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    sys.exit(0)
                elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:  ############# klik i cos sie dzieje raz
                    sys.exit(0)

            # Ticking
            self.tps_delta += self.tps_clock.tick(
            ) / 1000.0  # zamieniam MS na sekundy
            while self.tps_delta > 1 / self.tps_max:
                self.tick()
                self.tps_delta -= 1 / self.tps_max

            # Rendering/Drawing
            self.screen.fill((0, 0, 0))
            self.draw()
            display_fps()
            pygame.display.flip()

    def tick(self):
        self.player.tick()
        self.weapon.tick()
        self.fire.tick()
        self.enemy.tick()
        self.physics.tick()

    def draw(self):
        # self.menu.draw()
        self.map.draw()
        self.player.draw()
        self.enemy.draw()
        self.weapon.draw()
        self.fire.draw()
コード例 #3
0
ファイル: destroy.py プロジェクト: calzoneman/ludumdare25
def game():
    screen.fill(black)
    running = True
    keyboard = Keyboard()
    world = World(32, 32)
    off = ((WIDTH - world.get_render_width()) / 2,
           (HEIGHT - world.get_render_height()) / 2)
    world.offX = off[0]
    world.offY = off[1]
    ply = Player(0, 0, world)
    ply.x = random.randint(0, WIDTH - ply.w)
    ply.y = random.randint(0, HEIGHT - ply.h)
    while world.entity_hitpos(ply):
        ply.x = random.randint(0, WIDTH - ply.w)
        ply.y = random.randint(0, HEIGHT - ply.h)
    physics = Physics(world, (WIDTH, HEIGHT))
    physics.watch(ply)
    clock = pygame.time.Clock()
    last = pygame.time.get_ticks()
    ticks = 0
    plydeathtimer = 0
    lastrect = pygame.Rect(0, 0, 0, 0)

    while running:
        ticks += 1
        clock.tick(0)
        for ev in pygame.event.get():
            if ev.type == QUIT:
                running = False
                break
            elif ev.type == FRAMECOUNTER:
                print clock.get_fps()
            elif ev.type == WORLDREGEN:
                world.regen_once()
            elif ev.type == ENTITYGEN:
                x = random.randint(0, WIDTH)
                y = random.randint(0, HEIGHT)
                physics.watch(Enemy(x, y, world, physics))
            elif ev.type == MOUSEBUTTONDOWN:
                if ev.button == 0 and plydeathtimer == 0:
                    mpos = ev.pos
                    center = ply.get_center()
                    a = mpos[0] - center[0]
                    b = mpos[1] - center[1]
                    # tan(ang) = b / a
                    ang = math.atan2(b, a)

                    # Calculate bullet pos
                    pos = [math.cos(ang) * ply.w + center[0],
                           math.sin(ang) * ply.h + center[1]]

                    bull = Bullet(pos[0], pos[1], world)
                    speed = 2
                    bull.vx = speed * math.cos(ang)
                    bull.vy = speed * math.sin(ang)
                    physics.watch(bull)
                    shootsound.play()

            elif ev.type == KEYDOWN:
                keyboard.keydown(ev.key)
            elif ev.type == KEYUP:
                keyboard.keyup(ev.key)

        # Handle movement
        if plydeathtimer == 0:
            if keyboard.is_down(K_w):
                ply.vy = -1
            elif keyboard.is_down(K_s):
                ply.vy = 1
            else:
                ply.vy = 0
            if keyboard.is_down(K_a):
                ply.vx = -1
            elif keyboard.is_down(K_d):
                ply.vx = 1
            else:
                ply.vx = 0

        # Shooting repeat
        if ticks % 10 == 0 and pygame.mouse.get_pressed()[0]:
            mpos = pygame.mouse.get_pos()
            center = ply.get_center()
            a = mpos[0] - center[0]
            b = mpos[1] - center[1]
            # tan(ang) = b / a
            ang = math.atan2(b, a)

            # Calculate bullet pos
            pos = [math.cos(ang) * ply.w + center[0],
                   math.sin(ang) * ply.h + center[1]]

            bull = Bullet(pos[0], pos[1], world)
            speed = 2
            bull.vx = speed * math.cos(ang)
            bull.vy = speed * math.sin(ang)
            physics.watch(bull)
            shootsound.play()


        for ent in physics.entities:
            if isinstance(ent, Enemy):
                ent.think(ply)

        if world.win():
            win()
            running = False
        if ply.lives == 0:
            lose()
            running = False
        elif ply.removeme:
            ply.removeme = False
            plydeathtimer = 180
        if plydeathtimer > 0:
            if plydeathtimer == 1:
                ply.x = random.randint(0, WIDTH - ply.w)
                ply.y = random.randint(0, HEIGHT - ply.h)
                while world.entity_hitpos(ply):
                    ply.x = random.randint(0, WIDTH - ply.w)
                    ply.y = random.randint(0, HEIGHT - ply.h)
                physics.watch(ply)
            plydeathtimer -= 1

        # Clear previous font
        screen.fill(black, lastrect)

        # Render
        physics.renderclear(screen)
        world.render(screen)
        physics.tick()
        physics.render(screen)

        # Display health
        msg = "Lives: %d | Health: %d" % (ply.lives, ply.health)
        text = bigfont.render(msg, 1, white)
        lastrect = pygame.Rect(0, 0, text.get_width(), text.get_height())
        screen.blit(text, (0, 0))
        pygame.display.flip()


        dt = pygame.time.get_ticks() - last
        if dt < (1000 / 60):
            pygame.time.delay((1000 / 60) - dt)
        last = pygame.time.get_ticks()