Infinity Engine 2D Platformer Game Tutorial
Welcome to the Infinity Engine 2D Platformer Game Tutorial! In this guide, we will cover the steps needed to create a basic 2D platformer game using Infinity Engine. You will learn to set up the environment, create a player character, add platforms, and implement basic physics for jumping and movement.
Getting Started
Prerequisites
Before starting, ensure you have the following:
- Infinity Engine Installed Make sure your Infinity Engine setup is working without any errors. Refer to the installation guide if needed.
- Python Installed You will need Python 3.x installed on your system. If you haven't already, install Python from the official website: Python.
- Libraries Install the required libraries using pip. Open your terminal or command prompt and run the following:
bash Copy code pip install pygame ursina
Step 1: Setting Up the Game Environment
1. Initialize Infinity Engine and Import Libraries
Start by importing the necessary libraries and initializing the game window.
python Copy code import pygame as Infinity2D from ursina import * # Initialize Ursina Engine app = Ursina() # Set up the game window size WIDTH, HEIGHT = 800, 600 screen = Infinity2D.display.set_mode((WIDTH, HEIGHT)) Infinity2D.display.set_caption("Infinity Engine 2D Platformer")
2. Create the Player Character
We’ll create a simple player character using a basic cube entity.
python Copy code player = Entity(model='cube', color=color.red, scale=(1, 2), position=(0, 1, 0))
- Model: The player is represented as a cube.
- Color: The color is set to red.
- Scale: The scale is adjusted to make the player taller than wide.
- Position: The player starts at coordinates
(0, 1, 0)
.
Step 2: Create Platforms
Next, let’s create platforms for the player to jump on. Platforms are simply static entities that the player can collide with.
python Copy code platform1 = Entity(model='cube', color=color.green, scale=(5, 0.5), position=(0, -1, 0)) platform2 = Entity(model='cube', color=color.green, scale=(5, 0.5), position=(7, -3, 0))
We created two platforms:
platform1
: Positioned at(0, -1, 0)
platform2
: Positioned at(7, -3, 0)
Step 3: Add Player Movement and Jumping
We will use basic keyboard inputs to allow the player to move left and right, as well as jump.
1. Define Movement Speed
Let’s define how fast the player can move.
python Copy code player_speed = 5
2. Update the Player’s Position
We will add the ability to move the player left and right using the arrow keys, and make the player jump using the spacebar.
python Copy code def update(): if held_keys['right arrow']: player.x += player_speed * time.dt # Move right if held_keys['left arrow']: player.x -= player_speed * time.dt # Move left if held_keys['space'] and player.y == 1: # Check if on the ground player.y += 5 # Make the player jump
In the above code:
held_keys['right arrow']
: Moves the player to the right.held_keys['left arrow']
: Moves the player to the left.held_keys['space']
: Makes the player jump if they’re on the ground.
Step 4: Apply Gravity
To simulate gravity, we will slowly decrease the player's vertical position to simulate falling, unless the player is on the ground.
python Copy code gravity = 9.8 def update(): # Handle horizontal movement if held_keys['right arrow']: player.x += player_speed * time.dt if held_keys['left arrow']: player.x -= player_speed * time.dt # Jumping and gravity if held_keys['space'] and player.y == 1: player.y += 5 if player.y > 1: player.y -= gravity * time.dt # Apply gravity to fall
1. Collision with Ground
We will add a condition to stop the player’s downward movement when they reach the ground.
python Copy code if player.y <= 1: player.y = 1 # Stop falling when the player touches the ground
This keeps the player grounded once they land after a jump.
Step 5: Add a Camera and Finalize
Now, let’s add a camera to follow the player and make sure everything looks good.
python Copy code camera.parent = player camera.position = Vec3(0, 3, -10) # Position the camera behind the player camera.rotation = Vec3(20, 0, 0) # Slight angle to view the player
Now that the camera is attached to the player, it will follow their movements. The camera’s position is offset a bit to give a better perspective.
Step 6: Run the Game
Finally, start the game loop to run the game.
python Copy code app.run()
Complete Code Example
Here’s the full code to create your simple 2D platformer game:
python Copy code from ursina import * import pygame as Infinity2D # Initialize the Ursina Engine app = Ursina() # Game window setup WIDTH, HEIGHT = 800, 600 screen = Infinity2D.display.set_mode((WIDTH, HEIGHT)) Infinity2D.display.set_caption("Infinity Engine 2D Platformer") # Create the player character player = Entity(model='cube', color=color.red, scale=(1, 2), position=(0, 1, 0)) # Create platforms platform1 = Entity(model='cube', color=color.green, scale=(5, 0.5), position=(0, -1, 0)) platform2 = Entity(model='cube', color=color.green, scale=(5, 0.5), position=(7, -3, 0)) # Define movement speed player_speed = 5 gravity = 9.8 # Update function to handle movement and jumping def update(): if held_keys['right arrow']: player.x += player_speed * time.dt if held_keys['left arrow']: player.x -= player_speed * time.dt if held_keys['space'] and player.y == 1: player.y += 5 # Jumping if player.y > 1: player.y -= gravity * time.dt # Apply gravity to fall if player.y <= 1: player.y = 1 # Stop falling when the player touches the ground # Add a camera to follow the player camera.parent = player camera.position = Vec3(0, 3, -10) camera.rotation = Vec3(20, 0, 0) # Run the game app.run()
Conclusion
Now you have a basic 2D platformer set up in Infinity Engine! You can move the player left and right, jump on platforms, and experience simple gravity effects. This tutorial can be expanded with more features like animations, additional levels, enemies, and more! Happy coding!