-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
79 lines (58 loc) · 1.71 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import logging
import sys
import pygame
from enemy import Enemy, spawn_enemies
from player import Player
logging.basicConfig(level=10, format="%(asctime)s - %(levelname)5s - %(message)s") #, filename="output.log")
# Initialize Pygame
pygame.init()
WIDTH = 800
HEIGHT = 600
FPS = 60
# Colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
YELLOW = (255, 255, 153)
# Create the window
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Collect the coins!")
# Font setup
large_font = pygame.font.Font(None, 100)
# Initialize player
player = Player(start_x=WIDTH // 2, start_y=HEIGHT // 2)
# Generate enemies
enemies = spawn_enemies(board_width=WIDTH, board_height=HEIGHT, count=10)
enemy_sprites = pygame.sprite.Group()
enemy_sprites.add(enemies)
clock = pygame.time.Clock()
# Game loop
running = True
while running:
screen.fill(BLACK)
# Quit game event handler
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Update and draw player
player.update()
screen.blit(player.image, player.rect)
# Draw all enemies
enemy_sprites.draw(screen)
# Detect enemy collisions, increase player xp and render a new enemy
if enemy := pygame.sprite.spritecollideany(player, enemy_sprites):
player.xp += enemy.xp_value
enemy_sprites.remove(enemy)
enemy_sprites.add(Enemy(WIDTH, HEIGHT))
# Render score
text = large_font.render(f"XP: {player.xp}", True, WHITE)
text_rect = text.get_rect()
text_rect.topleft = (5, 5)
screen.blit(text, text_rect)
# Update screen
pygame.display.flip()
clock.tick(FPS)
# Quit Pygame
pygame.quit()
sys.exit()