Python script using Pygame, which includes character movement, NPC interaction, and simple programming challenges.
Game Features Overview:
- Players can move their character around.
- Players will encounter NPCs that provide simple programming tasks.
- Completing tasks will earn players points.
Complete Code Example:
import pygame
import sys
# Initialize Pygame
pygame.init()
# Set up the screen
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Code Adventure")
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
# Character settings
player_pos = [400, 300]
player_size = 50
player_speed = 5
# NPC settings
npc_pos = [200, 200]
npc_size = 50
# Game score
score = 0
font = pygame.font.SysFont("Arial", 24)
# Task list
tasks = [
("Add a and b:\na = 5\nb = 10\n# Your code:", "5 + 10"),
("Sum the elements in the list:\nlist = [1, 2, 3]\n# Your code:", "sum(list)"),
]
# Task index
current_task = 0
# Main loop
def main():
global score, current_task
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and player_pos[0] > 0:
player_pos[0] -= player_speed
if keys[pygame.K_RIGHT] and player_pos[0] < 800 - player_size:
player_pos[0] += player_speed
if keys[pygame.K_UP] and player_pos[1] > 0:
player_pos[1] -= player_speed
if keys[pygame.K_DOWN] and player_pos[1] < 600 - player_size:
player_pos[1] += player_speed
# Fill background
screen.fill(WHITE)
# Draw player
pygame.draw.rect(screen, GREEN, (player_pos[0], player_pos[1], player_size, player_size))
# Draw NPC
pygame.draw.rect(screen, BLACK, (npc_pos[0], npc_pos[1], npc_size, npc_size))
# Check for collision with NPC
if (player_pos[0] < npc_pos[0] + npc_size and
player_pos[0] + player_size > npc_pos[0] and
player_pos[1] < npc_pos[1] + npc_size and
player_pos[1] + player_size > npc_pos[1]):
show_task(screen, tasks[current_task])
# Display score
score_text = font.render(f"Score: {score}", True, BLACK)
screen.blit(score_text, (10, 10))
pygame.display.flip()
clock.tick(60)
def show_task(screen, task):
global score, current_task
task_text, answer = task
# Display task content
task_surface = font.render(task_text, True, BLACK)
screen.blit(task_surface, (100, 100))
# Simulate code input (in a real game, you would need to implement an input box)
# Here we simulate player inputting the correct answer
user_input = answer # Simulated player input
if user_input == answer:
score += 10
current_task += 1
if current_task >= len(tasks):
current_task = 0 # Restart tasks
if __name__ == "__main__":
main()
Game Features:
- Character Movement: Players can use the arrow keys to move the character.
- NPC Interaction: When the character approaches an NPC, a programming task is displayed on the screen.
- Programming Tasks: Players need to complete simple programming challenges, such as adding numbers and summing a list.
- Scoring System: Players earn points for completing tasks, encouraging them to keep playing.
How to Use:
- Install Pygame:
pip install pygame
. - Save the code as
code_adventure.py
. - Run the game:
python code_adventure.py
.
This script is a basic example that can be further expanded to include more features, such as additional tasks, power-ups, enemies, or even a simple level system.