make me a platformer in python using only the standard library
- User
make me a platformer in python using only the standard library
- Assistant
Sure! Here's a simple platformer game using only the standard library in Python:
import os import sys import time import msvcrt def clear_screen(): os.system('cls' if os.name == 'nt' else 'clear') def get_input(): if msvcrt.kbhit(): return msvcrt.getch().decode("utf-8").lower() else: return "" def draw_game(player_x, player_y): clear_screen() game_map = [ "#############################", "# #", "# #", "# #", "# #", "# #", "# #", "# #", "# #", "# #", "#############################" ] game_map[player_y] = game_map[player_y][:player_x] + "@" + game_map[player_y][player_x + 1:] for row in game_map: print(row) def play_game(): player_x = 10 player_y = 9 while True: draw_game(player_x, player_y) input_key = get_input() if input_key == "q": sys.exit() if input_key == "a": player_x -= 1 elif input_key == "d": player_x += 1 if player_x < 0: player_x = 0 elif player_x >= len(game_map[player_y]): player_x = len(game_map[player_y]) - 1 if __name__ == "__main__": play_game() - User
Do this via turtle
- Assistant
Certainly! Here's an example of a platformer game using the turtle library in Python:
import turtle # Create the screen screen = turtle.Screen() screen.title("Turtle Platformer") screen.bgcolor("white") screen.setup(width=800, height=600) screen.tracer(0) # Create the player turtle player = turtle.Turtle() player.shape("square") player.color("blue") player.penup() player.speed(0) player.goto(0, -250) player.direction = "stop" # Create the platforms platforms = [] platform_y = -200 for i in range(-350, 400, 70): platform = turtle.Turtle() platform.shape("square") platform.color("green") platform.penup() platform.speed(0) platform.goto(i, platform_y) platforms.append(platform) # Set gravity constant gravity = 0.2 # Define player movement functions def move_left(): player.direction = "left" def move_right(): player.direction = "right" def stop_movement(): player.direction = "stop" # Bind keyboard events screen.listen() screen.onkeypress(move_left, "Left") screen.onkeypress(move_right, "Right") screen.onkeyrelease(stop_movement, "Left") screen.onkeyrelease(stop_movement, "Right") # Main game loop while True: screen.update() # Move the player if player.direction == "left": player.setx(player.xcor() - 3) elif player.direction == "right": player.setx(player.xcor() + 3) # Apply gravity to the player player.sety(player.ycor() - gravity) # Check for collision with platforms for platform in platforms: if player.distance(platform) < 30: # Adjust collision distance if needed player.sety(platform.ycor() + 20) break # Check if player fell out of the screen if player.ycor() < -300: player.goto(0, -250) # Exit the game window turtle.done() - User
How can I add my own platforms using the platforms list?
- Assistant
To add your own platforms to the game using the
platformslist, you can modify the loop that creates the default platforms or add new platforms manually. Here's an example of how you can do it:# Create the platforms platforms = [] platform_y = -200 # Create default platforms using a loop for i in range(-350, 400, 70): platform = turtle.Turtle() platform.shape("square") platform.color("green") platform.penup() platform.speed(0) platform.goto(i, platform_y) platforms.append(platform) # Add new platforms manually new_platform = turtle.Turtle() new_platform.shape("square") new_platform.color("green") new_platform.penup() new_platform.speed(0) new_platform.goto(-200, -100) platforms.append(new_platform)In the above code, we first create the default platforms using a loop that runs through the desired x-coordinate positions. Each platform turtle is created and added to the
platformslist.If you want to add additional platforms manually, you can create a new turtle object for each platform and configure its position, shape, color, etc. Finally, append the new platform turtle to the
platformslist.Feel free to adjust the positions, colors, and other properties of the platforms to fit your game design.