コード例 #1
0
ファイル: Game.py プロジェクト: brenopsouza/j
def main():
    pygame.init()
    clock = pygame.time.Clock()
    running = True

    #Constantes
    SLICE_SIZE_PIXEL = 5120
    SLICE_SIZE = 80
    REPEAT_DELAY = 50 #milisseconds between each KEYDOWN event (when repeating)
    KEY_TIMEOUT = 185 #MAX milisseconds between key pressings
    SCREEN_WIDTH, SCREEN_HEIGHT = (1024, 640)

    screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
    pygame.display.set_caption("Good Intentions")
    img_fatguy = pygame.image.load(os.path.join('', 'images', 'sprite.png'))
    fatguy = Caracter("jonatas", img_fatguy, 10, 115, 115, 25)
    commandHandler = CommandHandler(fatguy)
    cam_speed  = (4,0)
    
    fatguy.set_pos(200,200)
    
    
    world_map = TileMapParser().parse_decode("huge_objects.tmx")
    world_map.load(ImageLoaderPygame())
    
    ground_objects = get_ground_objects(world_map)
    killer_objects = get_killer_objects(world_map)
    print killer_objects
    slices = set_slices(world_map, SLICE_SIZE, SLICE_SIZE_PIXEL)

    key_timeout = -1

    #create sprites groups for collision detection
    playerGroup = pygame.sprite.RenderUpdates()
    playerGroup.add(fatguy)

    objectGroup = pygame.sprite.Group()
    enemyGroup = pygame.sprite.Group()
    sceneGroup = pygame.sprite.Group()

    pygame.key.set_repeat(REPEAT_DELAY, REPEAT_DELAY)
    
    offset = 0
    actual_slice = slices.pop(0)
    past_slice = actual_slice
    transition = False
    while running:
        clock.tick(90)
        
        #blit level----------------------------------------------------------------------------------
        if transition:
            join_point = SLICE_SIZE_PIXEL - offset
            screen.blit(past_slice.subsurface(offset, 0, join_point, SCREEN_HEIGHT),(0,0))
            screen.blit(actual_slice.subsurface(0 ,0, (offset + SCREEN_WIDTH - SLICE_SIZE_PIXEL), SCREEN_HEIGHT),(join_point,0))
            if join_point < 0:
                offset = 0
                transition = False
        else:
            screen.blit(actual_slice.subsurface((offset,0,SCREEN_WIDTH, SCREEN_HEIGHT)), (0, 0))
        
        offset += cam_speed[0]
        if(offset + SCREEN_WIDTH) > SLICE_SIZE_PIXEL and transition == False:
            past_slice = actual_slice
            if len(slices) == 0: break
            actual_slice = slices.pop(0)
            transition = True
        #----------------------------------------------------------------------------------------------
	    
	    
        screen.blit(fatguy.image,  fatguy.get_pos())
        fatguy.update(pygame.time.get_ticks(), SCREEN_WIDTH, SCREEN_HEIGHT, cam_speed)
        
        obj, col_type = fatguy.collides_with_objects(ground_objects)
        if  col_type == 1:
            fatguy.put_on_ground_running(obj[1])
        elif col_type ==  2:
            running = False
        
        obj, col_type = fatguy.collides_with_objects(killer_objects)
        if  col_type != -1:
            running = False

        if key_timeout >= 0:
            if (pygame.time.get_ticks() - key_timeout) > KEY_TIMEOUT:
                commandHandler.actual_state = 0
                key_timeout = -1

        for e in pygame.event.get():
            if e.type == QUIT or (e.type == KEYDOWN and e.key == K_ESCAPE):
                running = False
            elif e.type == KEYDOWN:
                key_timeout = pygame.time.get_ticks()
                fatguy.state = commandHandler.refresh_state(e.key)
            if not fatguy.alive():
                print 'Game Over'
                pygame.time.wait(2000)
                sys.exit()

#        pygame.display.update()
        pygame.display.flip()
コード例 #2
0
ファイル: game.py プロジェクト: leonardoalt/sforsabotage
def main():
	pygame.init()
	clock = pygame.time.Clock()
	running = True
	
	root = Tkinter.Tk()
	TILE_WIDTH = 64
	TILE_HEIGHT = 64
	
	SCREEN_WIDTH, SCREEN_HEIGHT = (root.winfo_screenwidth(), root.winfo_screenheight())

	screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT), pygame.FULLSCREEN)
	pygame.display.set_caption("S for Sabotage")
	#commandHandler = CommandHandler(fatguy)

	world_map = TileMapParser().parse_decode("fase3.tmx")
	world_map.load(ImageLoaderPygame())

	ww = world_map.pixel_width;
	wh = world_map.pixel_height;

	w0 = world(ww, wh, SCREEN_WIDTH, SCREEN_HEIGHT, TILE_WIDTH, TILE_HEIGHT)
	
	ground_objects = get_walk_objects(world_map)
	nonwalk_objects = get_nonwalk_objects(world_map)
	walk_enemy = get_walk_enemy(world_map, 0)
	
	pliers = get_activate_objects(world_map, 'pliers')
	c4 = get_activate_objects(world_map, 'c4')
	
	activate_list = []
	activate_list.append(pliers)
	activate_list.append(c4)
	
	enemies = []
	
	player_x, player_y = get_player(world_map, "player")
	player_x = (player_x / TILE_WIDTH) * TILE_WIDTH;
	player_y = (player_y / TILE_HEIGHT) * TILE_HEIGHT;
	
	enemy_x, enemy_y = get_player(world_map, "enemies")
	enemy_x = (enemy_x / TILE_WIDTH) * TILE_WIDTH;
	enemy_y = (enemy_y / TILE_HEIGHT) * TILE_HEIGHT;
	
	print 'ENEMY: ' + str(enemy_x) + ' ' + str(enemy_y)
	
	
	print 'PLAYER: ' + str(player_x) + ' ' + str(player_y)
	img_s = pygame.image.load(os.path.join('', 'art', 's.png'))
	img_enemy = pygame.image.load(os.path.join('', 'art', 'police.png'))
	
	
	h0 = hud(['pliers', 'c4'])
	
	s = Character(w0, "s", img_s, player_x, player_y, nonwalk_objects, activate_list, h0)
	
	enemy = Enemy(w0, 'enemy', img_enemy, enemy_x, enemy_y, walk_enemy, 0)
	
	npx = player_x + TILE_WIDTH
	npy = player_y + TILE_HEIGHT
	camera_x = npx - SCREEN_WIDTH / 2
	if camera_x < 0:
		camera_x = 0
	elif (camera_x + SCREEN_WIDTH) > ww:
		camera_x -= (camera_x + SCREEN_WIDTH) - ww

	camera_y = npy - SCREEN_HEIGHT / 2
	if camera_y < 0:
		camera_y = 0
	elif (camera_y + SCREEN_HEIGHT) > wh:
		camera_y -= (camera_y + SCREEN_HEIGHT) - wh

	print str(camera_x) + ' ' + str(camera_y)

	
	c0 = camera(camera_x, camera_y, w0, s)


	inputHandler = input_handler(w0, c0, s)

	print str(ww) + ' ' + str(wh)

	#key_timeout = -1

	#create sprites groups for collision detection
	#playerGroup = pygame.sprite.RenderUpdates()
	#playerGroup.add(fatguy)

	#objectGroup = pygame.sprite.Group()
	#enemyGroup = pygame.sprite.Group()
	#sceneGroup = pygame.sprite.Group()

	#pygame.key.set_repeat(REPEAT_DELAY, REPEAT_DELAY)
	
	world_surface = world_map.get_surface()
	while running:
		clock.tick(60)
		
		screen.blit(world_surface.subsurface(pygame.Rect(c0.x, c0.y, SCREEN_WIDTH, SCREEN_HEIGHT)), (0, 0))
		
		screen.blit(s.image, s.draw_pos(c0.x, c0.y))
		
		screen.blit(enemy.image, enemy.draw_pos(c0.x, c0.y))
		if enemy.sees(s.get_pos(), 30, 120, 240):
			print 'TE VEJO!'
		print (s.get_pos())
		#pygame.draw.line(screen, (225,0,0), s.draw_pos(c0.x, c0.y), enemy.draw_pos(c0.x, c0.y), 10)
		
		enemy.draw_sees(screen, c0.x, c0.y, 15, 128,256)
		
		h0.paint(w0, c0)
		#fatguy.update(pygame.time.get_ticks(), SCREEN_WIDTH, SCREEN_HEIGHT, cam_speed)
		
		#obj, col_type = fatguy.collides_with_objects(ground_objects)
		#if  col_type == 1:
		#	fatguy.put_on_ground_running(obj[1])
		#elif col_type ==  2:
		#	running = False
		
		#obj, col_type = fatguy.collides_with_objects(killer_objects)
		#if  col_type != -1:
		#	running = False

		running = inputHandler.handle()

#		pygame.display.update()
		pygame.display.flip()