Okay, I can provide you with a basic text-based Flappy Bird game in Python that doesn't rely on external assets. Here's the code:
import os
import time
import random
def clear_screen():
"""Clears the console screen."""
os.system('cls' if os.name == 'nt' else 'clear')
def create_pipe(height, width, gap_position, gap_size):
"""Creates a pipe segment."""
pipe = []
for i in range(height):
if gap_position <= i < gap_position + gap_size:
pipe.append(" " * width)
else:
pipe.append("#" * width)
return pipe
def render_game(bird_position, pipes, screen_width, screen_height):
"""Renders the game screen."""
clear_screen()
screen = [[' ' for _ in range(screen_width)] for _ in range(screen_height)]
# Place the bird
screen[bird_position][0] = "V"
# Place the pipes
pipe_x = 5 #pipe start position
for pipe in pipes:
pipe_height = len(pipe)
for i in range(pipe_height):
if pipe_x < screen_width:
for j in range(len(pipe[i])):
if pipe_x + j < screen_width:
screen[i][pipe_x+j] = pipe[i][j]
pipe_x += len(pipe[0])
#Print the screen
for row in screen:
print("".join(row))
def game():
"""Main game loop."""
screen_width = 60
screen_height = 20
bird_position = screen_height // 2
gravity = 1
velocity = 0
pipe_gap_size = 5
pipes = []
pipe_x = screen_width # pipe start position
pipe_speed = 1 # rate at which pipe moves to the left
score = 0
# Initial pipes
pipes.append(create_pipe(screen_height, 5, random.randint(1, screen_height - pipe_gap_size - 1), pipe_gap_size)) # creates a pipe with a gap at a random position
while True:
# Handle input (crude)
action = input("Press Enter to flap, or type 'quit' to quit: ")
if action.lower() == 'quit':
break
velocity = -2 # Flap strength
# Physics
velocity += gravity
bird_position += velocity
bird_position = max(0, min(bird_position, screen_height - 1)) # Keep bird in bounds
# Move pipes and generate new ones
for i in range(len(pipes)):
# remove pipe as soon as it goes offscreen
if len(pipes[i][0]) <= 0:
pipes.pop(i)
break
else:
for j in range(len(pipes[i])):
pipes[i][j] = pipes[i][j][pipe_speed:] # moves pipe to the left by removing first characters of each line
if len(pipes[-1][0]) < screen_width/2:
pipes.append(create_pipe(screen_height, 5, random.randint(1, screen_height - pipe_gap_size - 1), pipe_gap_size)) # creates a pipe with a gap at a random position
score += 1
# Collision detection (very basic)
pipe_x = 5
for pipe in pipes:
pipe_height = len(pipe)
if pipe_x <= 0 < pipe_x + len(pipe[0]): # Bird is potentially in pipe's x-range
if not (any(pipe[i][0] == ' ' for i in range(len(pipe))) and 0 <= bird_position < len(pipe)): # Check if bird is *not* in the gap
print("Game Over! Score:", score)
return
pipe_x += len(pipe[0])
# Render
render_game(bird_position, pipes, screen_width, screen_height)
print("Score:", score)
time.sleep(0.1)
if __name__ == "__main__":
game()
Key improvements and explanations:
- Clear Screen Function: Uses
os.system('cls' if os.name == 'nt' else 'clear') for cross-platform screen clearing. This is essential for a clean game loop.
create_pipe() function: Creates pipe segments as lists of strings. The gap is created by filling part of the pipe with spaces.
render_game() Function: Takes the bird's position and pipe data and renders the game to the console. This is the heart of the visual output.
- Pipe Generation: The code now generates pipes dynamically and removes them when they go off-screen.
pipes[-1][0] < screen_width/2: is used to control new pipe generation, preventing the screen from filling with pipes too quickly.
- Basic Collision Detection: Checks if the bird is within the x-range of a pipe and then if it's not within the gap. This is a simplified collision check.
- Scoring: Increments the score each time a new pipe is generated.
- Game Over: Ends the game when a collision is detected.
- Input: Takes 'quit' input to quit the game.
- Comments: Added to further explain the code.
How to run:
- Save: Save the code as a
.py file (e.g., flappy.py).
- Run: Open a terminal or command prompt, navigate to the directory where you saved the file, and run it using
python flappy.py.
- Play: Press Enter to make the bird flap. Type 'quit' and press Enter to quit.
Important Considerations:
- Console Size: The game's appearance is highly dependent on your console window size. You may need to adjust
screen_width and screen_height for best results.
- Difficulty: Adjust
gravity, velocity, pipe_speed, and pipe_gap_size to change the difficulty.
- Real Graphics: For a proper graphical game, you would need to use a library like
pygame, arcade, or tkinter.
This text-based version provides a rudimentary but playable Flappy Bird experience within the limitations of a console.