Python Programming Tutorial: Apple Shooter Game176


In this tutorial, we will create a simple but fun game called Apple Shooter using Python and the Pygame library. We will cover the basics of game development, such as creating a game window, handling user input, drawing graphics, and detecting collisions.

Let's get started!

1. Setting up the Game

First, we need to install the Pygame library. Open your terminal or command prompt and type the following command:pip install pygame

Once Pygame is installed, we can start coding. Create a new Python file and add the following code to it:```python
import pygame
import random
# Initialize the game engine
()
# Define the game window size
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
# Create the game window
screen = .set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
# Set the window title
.set_caption("Apple Shooter")
# Define the colors we will use in RGB format
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
# Define the player and apple objects
player = (300, 500, 50, 50)
apple = ((0, SCREEN_WIDTH), (0, SCREEN_HEIGHT), 50, 50)
# Define the game loop flag
running = True
# Run the game loop
while running:
# Handle events
for event in ():
if == :
running = False
# Clear the screen
(WHITE)
# Draw the player
(screen, BLACK, player)
# Draw the apple
(screen, RED, apple)
# Update the screen
()
```

This code creates a simple game window with a white background, a black player rectangle, and a red apple rectangle. The game loop is running until the user closes the window.

2. Handling User Input

Next, we need to handle user input so that the player can move around the screen. Add the following code to the event loop:```python
# Handle key presses
keys = .get_pressed()
if keys[pygame.K_LEFT]:
player.x -= 5
if keys[pygame.K_RIGHT]:
player.x += 5
if keys[pygame.K_UP]:
player.y -= 5
if keys[pygame.K_DOWN]:
player.y += 5
```

This code checks if the left, right, up, or down arrow keys are being pressed. If so, it moves the player rectangle in the corresponding direction.

3. Detecting Collisions

Now, we need to detect when the player collides with the apple. Add the following code to the game loop:```python
# Check for collision between the player and the apple
if (apple):
# If there is a collision, reset the apple to a new random position
apple = ((0, SCREEN_WIDTH), (0, SCREEN_HEIGHT), 50, 50)
```

This code checks if the player rectangle is colliding with the apple rectangle. If so, it resets the apple to a new random position.

4. Finishing Touches

Finally, let's add some finishing touches to the game, such as a score counter and a game over screen. Add the following code to the top of the script:```python
score = 0
```

And add the following code to the game loop:```python
# Draw the score
font = ('Arial', 30)
text = ("Score: " + str(score), True, BLACK)
(text, (10, 10))
# Check if the player has left the game window
if player.x < 0 or player.x > SCREEN_WIDTH or player.y < 0 or player.y > SCREEN_HEIGHT:
# If the player has left the game window, display a game over message
font = ('Arial', 50)
text = ("Game Over", True, BLACK)
(text, (SCREEN_WIDTH / 2 - 100, SCREEN_HEIGHT / 2 - 25))
running = False
```

This code adds a score counter to the game and displays a game over message if the player leaves the game window.

Conclusion

Congratulations! You have now created a simple but fun Apple Shooter game using Python and Pygame. You can now customize the game by adding different graphics, levels, and enemies.

2025-01-25


Previous:Government Cloud Computing: Transforming Public Sector Services

Next:Create Stunning Mobile Posters with Photoshop