News

Flappy bird in python vex v5

Building a Flappy Bird Game in Python for VEX V5

Creating a Flappy Bird game on a VEX V5 robot is an exciting way to combine programming with gaming. This guide will walk you through the process of building a simplified version of Flappy Bird using Python, leveraging the VEX V5 Brain and its screen capabilities.

Overview of Flappy Bird

Flappy Bird is a simple but addictive game where a bird navigates through gaps in pipes by “flapping” upward each time the player taps a button. The challenge lies in keeping the bird afloat while avoiding obstacles.

Key Features:

  1. Bird Movement: The bird falls due to gravity and rises when the player presses a button.
  2. Pipes: Obstacles that scroll horizontally with gaps for the bird to pass through.
  3. Scoring: Points are awarded for successfully navigating through gaps.

Requirements

  • VEX V5 Brain: For rendering graphics on the screen.
  • Python Programming: To implement game logic.
  • VEX V5 Controller: For user input (flapping).

Step-by-Step Implementation

1. Initialize the VEX Environment

First, set up the basic environment to use the VEX V5 Brain screen for graphics.

python
from vex import *
import random
import time

# Initialize the VEX Brain
brain = Brain()

# Game Constants
SCREEN_WIDTH = 480
SCREEN_HEIGHT = 240
BIRD_RADIUS = 10
PIPE_WIDTH = 40
PIPE_GAP = 80
GRAVITY = 1
FLAP_STRENGTH = -10
PIPE_SPEED = 3

2. Define Game Variables

Create variables for the bird’s position, velocity, pipes, and score.

python
# Bird variables
bird_y = SCREEN_HEIGHT // 2
bird_velocity = 0

# Pipes list
pipes = []

# Score
score = 0

# Game state
is_game_over = False

3. Draw the Game Elements

Write functions to draw the bird, pipes, and score on the screen.

python
def draw_bird():
brain.screen.draw_circle(SCREEN_WIDTH // 4, bird_y, BIRD_RADIUS)

def draw_pipe(pipe_x, pipe_top_height):
pipe_bottom_y = pipe_top_height + PIPE_GAP
brain.screen.draw_rectangle(pipe_x, 0, PIPE_WIDTH, pipe_top_height)
brain.screen.draw_rectangle(pipe_x, pipe_bottom_y, PIPE_WIDTH, SCREEN_HEIGHT - pipe_bottom_y)

def draw_score():
brain.screen.set_cursor(1, 1)
brain.screen.print(f"Score: {score}")

4. Handle Bird Movement

Implement the logic for bird movement, including gravity and user input.

python
def update_bird():
global bird_y, bird_velocity, is_game_over
bird_velocity += GRAVITY
bird_y += bird_velocity

# Check if bird hits the top or bottom of the screen
if bird_y - BIRD_RADIUS < 0 or bird_y + BIRD_RADIUS > SCREEN_HEIGHT:
is_game_over = True

5. Generate and Update Pipes

Add new pipes and move them across the screen.

python
def update_pipes():
global pipes, score, is_game_over
new_pipes = []

for pipe_x, pipe_top_height in pipes:
# Move pipe
pipe_x -= PIPE_SPEED

# Check collision
if SCREEN_WIDTH // 4 + BIRD_RADIUS > pipe_x and SCREEN_WIDTH // 4 - BIRD_RADIUS < pipe_x + PIPE_WIDTH:
if bird_y - BIRD_RADIUS < pipe_top_height or bird_y + BIRD_RADIUS > pipe_top_height + PIPE_GAP:
is_game_over = True

# Add pipe if still on screen
if pipe_x + PIPE_WIDTH > 0:
new_pipes.append((pipe_x, pipe_top_height))
else:
score += 1 # Score increases when a pipe exits

pipes = new_pipes

# Add new pipe
if len(pipes) == 0 or pipes[-1][0] < SCREEN_WIDTH - 200:
pipe_top_height = random.randint(30, SCREEN_HEIGHT - PIPE_GAP - 30)
pipes.append((SCREEN_WIDTH, pipe_top_height))

6. Main Game Loop

Combine all components into the main game loop.

python
def main():
global is_game_over, bird_velocity

while not is_game_over:
brain.screen.clear_screen()

# Update game elements
update_bird()
update_pipes()

# Draw game elements
draw_bird()
for pipe_x, pipe_top_height in pipes:
draw_pipe(pipe_x, pipe_top_height)
draw_score()

# Refresh screen
brain.screen.render()

# Flap input
if brain.controller.buttonA.pressing():
bird_velocity = FLAP_STRENGTH

# Delay for smooth gameplay
time.sleep(0.03)

# Game over screen
brain.screen.clear_screen()
brain.screen.set_cursor(2, 2)
brain.screen.print("Game Over!")
brain.screen.set_cursor(3, 2)
brain.screen.print(f"Final Score: {score}")
brain.screen.render()

7. Run the Game

Call the main function to start the game.

python
if __name__ == "__main__":
main()

Conclusion

Congratulations! You’ve created a Flappy Bird game using Python for the VEX V5 system. This project showcases how game mechanics like gravity, collision detection, and scoring can be implemented with simple logic and graphics.

Feel free to expand on this project by adding new features, such as:

  • Increasing difficulty (faster pipes, smaller gaps).
  • Restart functionality after a game over.
  • Sound effects or animations.

Happy coding! 🎮

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Check Also
Close
Back to top button