-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcatching_apple.py
More file actions
65 lines (47 loc) · 1.59 KB
/
catching_apple.py
File metadata and controls
65 lines (47 loc) · 1.59 KB
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
import pygame
import random
pygame.init()
WIDTH, HEIGHT = 800,700
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("catch the falling apple")
LIGHT_BLUE = (176, 216, 230)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
basket_width,basket_height = 100, 15
basket_x = WIDTH // - basket_width //2
basket_y = HEIGHT - 20
basket_speed = 10
apple_radius = 10
apple_x = random.randint(apple_radius, WIDTH - apple_radius)
apple_y = -apple_radius
apple_speed = 5
score = 0
font = pygame.font.Font(None , 30)
clock = pygame.time.Clock()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and basket_x > 0:
basket_x -= basket_speed
if keys[pygame.K_RIGHT] and basket_x < WIDTH - basket_width:
basket_x += basket_speed
apple_y += apple_speed
if basket_y < apple_y + apple_radius < basket_y + \
basket_height and basket_x < apple_x < basket_x + basket_width:
score += 1
apple_x = random.randint(apple_radius, WIDTH - apple_radius)
apple_y = - apple_radius
if apple_y > HEIGHT:
running = False
print(f"Game Over! Your score is: {score}")
screen.fill(LIGHT_BLUE)
pygame.draw.rect(screen, BLACK,(basket_x, basket_y, basket_width, basket_height))
pygame.draw.circle(screen, RED,(apple_x, apple_y), apple_radius)
score_text = font.render(f"Score: {score}", True, BLACK)
screen.blit(score_text, (10, 10))
pygame.display.flip()
clock.tick(60)
pygame.quit()