-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
68 lines (55 loc) · 1.57 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
import pygame
import random
import sys
from player import Player
from obstacle import Obstacle
from button import Button
# Initialize Pygame
pygame.init()
# Set up the screen
SCREEN_WIDTH = 1000
SCREEN_HEIGHT = 800
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("JJ's Game")
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
OBSTACLE_FREQ = 0.01 # Probability of obstacle appearance per frame
# Score
score = 0
font = pygame.font.Font(None, 36)
all_sprites = pygame.sprite.Group()
player = Player(SCREEN_WIDTH,SCREEN_HEIGHT,WHITE)
all_sprites.add(player)
obstacles = pygame.sprite.Group()
quit_button = Button(RED, 700, 10, 80, 40,'Quit')
all_sprites = pygame.sprite.Group()
all_sprites.add(quit_button)
# Main game loop
running = True
clock = pygame.time.Clock()
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Create new obstacles with a certain probability
if random.random() < OBSTACLE_FREQ:
obstacle = Obstacle(SCREEN_WIDTH,SCREEN_HEIGHT,BLACK)
obstacles.add(obstacle)
all_sprites.add(obstacle)
# Update
all_sprites.update()
# Check for collisions
hits = pygame.sprite.spritecollide(player, obstacles, True)
for hit in hits:
# Handle collision (e.g., decrease score)
pass
# Draw
screen.fill(WHITE)
all_sprites.draw(screen)
text = font.render("Score: " + str(score), True, BLACK)
screen.blit(text, (10, 10))
pygame.display.flip()
clock.tick(60)
pygame.quit()