-
Notifications
You must be signed in to change notification settings - Fork 3
/
08_06_rasp_game_final.py
executable file
·80 lines (63 loc) · 1.84 KB
/
08_06_rasp_game_final.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
80
#08_06_rasp_game_final
import pygame
from pygame.locals import *
import random
score = 0
screen_width = 600
screen_height = 400
spoon_x = 300
spoon_y = screen_height - 100
class Raspberry:
x = 0
y = 0
dy = 0
def __init__(self):
self.x = random.randint(10, screen_width)
self.y = 0
self.dy = random.randint(3, 10)
def update(self):
self.y += self.dy
if self.y > spoon_y:
self.y = 0
self.x = random.randint(10, screen_width)
self.x += random.randint(-5, 5)
if self.x < 10:
self.x = 10
if self.x > screen_width - 20:
self.x = screen_width - 20
screen.blit(raspberry_image, (self.x, self.y))
def is_caught(self):
return self.y >= spoon_y and self.x >= spoon_x and self.x < spoon_x + 50
clock = pygame.time.Clock()
rasps = [Raspberry(), Raspberry(), Raspberry()]
pygame.init()
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('Raspberry Catching')
spoon = pygame.image.load('prog_pi_ed3/spoon.jpg').convert()
raspberry_image = pygame.image.load('prog_pi_ed3/raspberry.jpg').convert()
def update_spoon():
global spoon_x
global spoon_y
spoon_x, ignore = pygame.mouse.get_pos()
screen.blit(spoon, (spoon_x, spoon_y))
def check_for_catch():
global score
for r in rasps:
if r.is_caught():
score += 1
def display(message):
font = pygame.font.Font(None, 36)
text = font.render(message, 1, (10, 10, 10))
screen.blit(text, (0, 0))
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
screen.fill((255, 255, 255))
for r in rasps:
r.update()
update_spoon()
check_for_catch()
display("Score: " + str(score))
pygame.display.update()
clock.tick(30)