Certainly! Below is a simplified version of "Coinbox Hero" implemented using Pygame. This version includes:
- Coinbox: A box that the player can hit to produce coins.
- Coin System: Collecting and counting coins.
- Jump Hit System: The player can jump and hit the coinbox from below.
Let's break down the implementation step by step.
Prerequisites
Make sure you have Pygame installed:
pip install pygame
Code Overview
- Player: Can move left, right, and jump.
- Coinbox: When hit from below by the player, it produces a coin.
- Coin: Falls down and gets collected automatically, incrementing the coin count.
- Game Loop: Handles events, updates game state, and renders graphics.
The Code
import pygame
import sys
# Initialize Pygame
pygame.init()
# Screen dimensions
WIDTH, HEIGHT = 800, 600
SCREEN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Coinbox Hero Simplified")
# Clock for controlling frame rate
CLOCK = pygame.time.Clock()
# Colors
WHITE = (255, 255, 255)
GRAY = (100, 100, 100)
GOLD = (255, 215, 0)
BLACK = (0, 0, 0)
# Font
FONT = pygame.font.SysFont(None, 36)
# Player properties
player_width = 50
player_height = 60
player_x = WIDTH // 2
player_y = HEIGHT - player_height - 50
player_speed = 5
player_vy = 0
gravity = 0.5
is_jumping = False
# Coinbox properties
coinbox_width = 60
coinbox_height = 60
coinbox_x = WIDTH // 2 - coinbox_width // 2
coinbox_y = HEIGHT // 2 - coinbox_height // 2
# Coin properties
coins = []
coin_radius = 10
coin_speed = 3
coin_count = 0
def draw_player(x, y):
pygame.draw.rect(SCREEN, GRAY, (x, y, player_width, player_height))
def draw_coinbox(x, y):
pygame.draw.rect(SCREEN, GOLD, (x, y, coinbox_width, coinbox_height))
def spawn_coin(x, y):
coins.append({'x': x + coinbox_width // 2, 'y': y + coinbox_height})
def draw_coins():
for coin in coins:
pygame.draw.circle(SCREEN, GOLD, (int(coin['x']), int(coin['y'])), coin_radius)
def move_coins():
global coin_count
for coin in coins[:]:
coin['y'] += coin_speed
# Auto-collect coins when they reach the ground level
if coin['y'] >= HEIGHT - 50:
coins.remove(coin)
coin_count += 1
def draw_text(text, x, y):
img = FONT.render(text, True, BLACK)
SCREEN.blit(img, (x, y))
# Main game loop
def main():
global player_x, player_y, player_vy, is_jumping
running = True
while running:
CLOCK.tick(60) # Limit to 60 frames per second
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
keys = pygame.key.get_pressed()
# Horizontal movement
if keys[pygame.K_LEFT]:
player_x -= player_speed
if player_x < 0:
player_x = 0
if keys[pygame.K_RIGHT]:
player_x += player_speed
if player_x > WIDTH - player_width:
player_x = WIDTH - player_width
# Jumping
if not is_jumping and keys[pygame.K_SPACE]:
is_jumping = True
player_vy = -10 # Initial jump velocity
if is_jumping:
player_vy += gravity
player_y += player_vy
if player_y >= HEIGHT - player_height - 50:
player_y = HEIGHT - player_height - 50
is_jumping = False
# Collision detection with coinbox from below
player_rect = pygame.Rect(player_x, player_y, player_width, player_height)
coinbox_rect = pygame.Rect(coinbox_x, coinbox_y, coinbox_width, coinbox_height)
if player_rect.colliderect(coinbox_rect):
if player_vy < 0 and player_y > coinbox_y + coinbox_height - player_vy:
player_y = coinbox_y + coinbox_height
player_vy = 0
# Spawn a coin when hit from below
spawn_coin(coinbox_x, coinbox_y)
move_coins()
# Drawing
SCREEN.fill(WHITE)
draw_player(player_x, player_y)
draw_coinbox(coinbox_x, coinbox_y)
draw_coins()
draw_text(f"Coins: {coin_count}", 10, 10)
pygame.display.flip()
pygame.quit()
sys.exit()
if __name__ == "__main__":
main()
How to Run the Code
-
Save the code in a file named, for example, coinbox_hero.py.
-
Ensure you have Pygame installed (pip install pygame).
-
Run the script using Python:
python coinbox_hero.py
Controls
- Left Arrow: Move left.
- Right Arrow: Move right.
- Spacebar: Jump.
Explanation
- Player Movement: The player can move left and right within the screen boundaries. Jumping is implemented with basic physics using gravity.
- Collision Detection: When the player collides with the coinbox from below, a coin is spawned.
- Coins: Coins fall down and are automatically collected when they reach the ground level, incrementing the
coin_count.
- Rendering: All game objects are drawn each frame, and the coin count is displayed on the screen.
Customization
You can enhance the game by:
- Adding animations or images for the player and coinbox.
- Implementing manual coin collection by the player.
- Adding sound effects when the coinbox is hit or a coin is collected.
- Introducing obstacles or other interactive elements.
Feel free to ask if you need further assistance or modifications!