class QuestGame(object):
    """ This class is a basic game.

    This class will load data, create a pyscroll group, a hero object.
    It also reads input and moves the Hero around the map.
    Finally, it uses a pyscroll group to render the map and Hero.
    """
    filename = get_map(MAP_FILENAME)

    def __init__(self):

        # true while running
        self.running = False

        # load data from pytmx
        tmx_data = pytmx.load_pygame(self.filename)

        # setup level geometry with simple pygame rects, loaded from pytmx
        self.walls = list()
        for object in tmx_data.objects:
            self.walls.append(pygame.Rect(
                object.x, object.y,
                object.width, object.height))

        # create new data source for pyscroll
        map_data = pyscroll.data.TiledMapData(tmx_data)

        w, h = screen.get_size()

        # create new renderer (camera)
        # clamp_camera is used to prevent the map from scrolling past the edge
        self.map_layer = pyscroll.BufferedRenderer(map_data,
                                                   (w / 2, h / 2),
                                                   clamp_camera=True)

        # pyscroll supports layered rendering.  our map has 3 'under' layers
        # layers begin with 0, so the layers are 0, 1, and 2.
        # since we want the sprite to be on top of layer 1, we set the default
        # layer for sprites as 1
        self.group = PyscrollGroup(map_layer=self.map_layer,
                                   default_layer=2)

        self.hero = Hero()

        # put the hero in the center of the map
        self.hero.position = self.map_layer.rect.center

        # add our hero to the group
        self.group.add(self.hero)

    def draw(self, surface):

        # center the map/screen on our Hero
        self.group.center(self.hero.rect.center)

        # draw the map and all sprites
        self.group.draw(surface)

    def handle_input(self):
        """ Handle pygame input events
        """
        event = pygame.event.poll()
        while event:
            if event.type == QUIT:
                self.running = False
                break

            elif event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    self.running = False
                    break

            # this will be handled if the window is resized
            elif event.type == VIDEORESIZE:
                init_screen(event.w, event.h)
                self.map_layer.set_size((event.w / 2, event.h / 2))

            event = pygame.event.poll()

        # using get_pressed is slightly less accurate than testing for events
        # but is much easier to use.
        pressed = pygame.key.get_pressed()
        if pressed[K_UP]:
            self.hero.velocity[1] = -HERO_MOVE_SPEED
        elif pressed[K_DOWN]:
            self.hero.velocity[1] = HERO_MOVE_SPEED
        else:
            self.hero.velocity[1] = 0

        if pressed[K_LEFT]:
            self.hero.velocity[0] = -HERO_MOVE_SPEED
        elif pressed[K_RIGHT]:
            self.hero.velocity[0] = HERO_MOVE_SPEED
        else:
            self.hero.velocity[0] = 0

    def update(self, dt):
        """ Tasks that occur over time should be handled here
        """
        self.group.update(dt)

        # check if the sprite's feet are colliding with wall
        # sprite must have a rect called feet, and move_back method,
        # otherwise this will fail
        for sprite in self.group.sprites():
            if sprite.feet.collidelist(self.walls) > -1:
                sprite.move_back(dt)

    def run(self):
        """ Run the game loop
        """
        clock = pygame.time.Clock()
        fps = 60
        scale = pygame.transform.scale
        self.running = True

        try:
            while self.running:
                dt = clock.tick(fps) / 1000.

                self.handle_input()
                self.update(dt)
                self.draw(temp_surface)
                scale(temp_surface, screen.get_size(), screen)
                pygame.display.flip()

        except KeyboardInterrupt:
            self.running = False
예제 #2
0
파일: game.py 프로젝트: knehring/Untitled
class Game(object):
	#game Main entry point. handles intialization of game and graphics.    
	filename = get_map('outdoor_main.tmx')
	done = False

	def __init__(self): 

		self.PLAYER_MOVE_SPEED = 200
		self.screen = init_screen(800, 600)

		#load data from pytmx
		self.tmx_data = pytmx.load_pygame(self.filename)

		w, h = self.screen.get_size()

		#create new data source for pyscroll
		map_data = pyscroll.data.TiledMapData(self.tmx_data)

		self.map_layer = pyscroll.BufferedRenderer(map_data, (w / 2, h / 2), clamp_camera=True)

		self.group = PyscrollGroup(map_layer=self.map_layer, default_layer=0)

		#setup level with pygame rects
		self.blockers = list()
		#self.enemies = dict()
		#self.index = 0
		for object in self.tmx_data.objects:

			if "blockers" in object.properties:
				if object.properties["blockers"] == "1":
					self.blockers.append(pygame.Rect(object.x, object.y, object.width, object.height))
			if object.name == "playerStart":
				self.playerStart = pygame.Rect(object.x, object.y, object.width, object.height)
			if object.name == "enemy":
				self.group.add(Enemy((object.x, object.y)))
				#self.enemies[self.index] = pygame.Rect(object.x, object.y, object.width, object.height)
				#self.index = self.index + 1
				self.blockers.append(pygame.Rect(object.x, object.y, object.width, object.height))

		self.player = Player()

		# put the hero in the center of the map
		self.player.position = [self.playerStart.x, self.playerStart.y]
		# add our hero to the group
		self.group.add(self.player)

		self.temp_surface = pygame.Surface((w / 2, h / 2)).convert()

	def draw(self, surface):

		#center map / screen on player
		self.group.center(self.player.rect.center)

		#draw the map and all sprites
		self.group.draw(surface)

	def handle_events(self):
		#handles input events
		event = pygame.event.poll()

		while event:            
			if event.type == pygame.QUIT: 
				self.done = True
				break

			elif event.type == KEYDOWN:
			# exit on Escape
				if event.key == K_ESCAPE: 
					self.done = True
					break
			event = pygame.event.poll()

		pressed = pygame.key.get_pressed()
		if pressed[K_SPACE]:
			self.group.add(Magic(self.player.position, self.player.direction, self.blockers))
		if pressed[K_UP]:
			self.player.velocity[1] = -self.PLAYER_MOVE_SPEED
			self.player.direction = 1
		elif pressed[K_DOWN]:
			self.player.velocity[1] = self.PLAYER_MOVE_SPEED
			self.player.direction = 2
		else:
			self.player.velocity[1] = 0
			#print "self.player.velocity[1]: %s"%(self.player.velocity[1])

		if pressed[K_LEFT]:
			self.player.velocity[0] = -self.PLAYER_MOVE_SPEED
			self.player.direction = 3
		elif pressed[K_RIGHT]:
			self.player.velocity[0] = self.PLAYER_MOVE_SPEED
			self.player.direction = 4
		else:
			self.player.velocity[0] = 0



	def update(self, dt):
		self.group.update(dt)

		for sprite in self.group.sprites():
			if sprite.rect.collidelist(self.blockers) > -1:
				sprite.move_back(dt)

	def main_loop(self):
		#Game() main loop 
		clock = pygame.time.Clock()
		fps = 60
		scale = pygame.transform.scale  
		done  = False 
		try:  
			while not self.done:
				# get key input, move, draw.
				dt = clock.tick(fps) / 1000.
				
				self.handle_events()
				self.update(dt)
				self.draw(self.temp_surface)
				scale(self.temp_surface, self.screen.get_size(), self.screen)
				pygame.display.flip()

		except KeyboardInterrupt:
			self.done = True