-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame.py
140 lines (109 loc) · 3.94 KB
/
game.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import pygame
import random
import os
# Setup constants
width = 300
height = 600
player_width = 24
sprite_size = 32
class Player(pygame.sprite.Sprite):
def __init__(self, starting_pos, surface):
pygame.sprite.Sprite.__init__(self)
self.image = surface
self.rect = pygame.Rect(starting_pos, (player_width, sprite_size))
def draw(self, screen):
screen.blit(self.image, self.rect)
def move(self, direction):
if direction == 'left':
new_x = self.rect.x - 10
if new_x < 0:
new_x = 0
self.rect.x = new_x
if direction == 'right':
new_x = self.rect.x+10
if new_x + player_width > width:
new_x = width - player_width
self.rect.x = new_x
class Stracciatella(pygame.sprite.Sprite):
def __init__(self, surface):
pygame.sprite.Sprite.__init__(self)
pos = (random.randrange(width - sprite_size), 0)
self.image = surface
self.rect = pygame.Rect(pos, (sprite_size, sprite_size))
def draw(self, screen):
self.rect.y = self.rect.y+4
screen.blit(self.image, self.rect)
class IceGroup(pygame.sprite.Group):
def __init__(self, *sprites):
pygame.sprite.Group.__init__(self, *sprites)
def draw(self, screen):
for cream in self.sprites():
cream.draw(screen)
def main():
pygame.init()
# Game setup
logo = pygame.image.load(os.path.join('images', 'bee-logo.png'))
pygame.display.set_icon(logo)
pygame.display.set_caption('Bee Game')
screen = pygame.display.set_mode((width,height))
# Add and display background
background = pygame.image.load(os.path.join('images', 'background.png'))
screen.blit(background, (0,0))
pygame.display.update()
# Setup player
bee = pygame.image.load(os.path.join('images', 'bee.png')).convert_alpha()
start_pos = (width/2 - player_width/2,height - sprite_size)
player = Player(start_pos, bee)
# Setup ice-creams
ice_cream = pygame.image.load(os.path.join('images', 'stracciatella.png')).convert_alpha()
ice1 = Stracciatella(ice_cream)
ice_group = IceGroup(ice1)
# Setup counter for ice-cream spawning
counter = 0
running = True
while running:
for event in pygame.event.get():
# If the player quits the game
if event.type == pygame.QUIT:
running = False
# Check key presses
pressed = pygame.key.get_pressed()
if pressed[pygame.K_LEFT]:
player.move('left')
elif pressed[pygame.K_RIGHT]:
player.move('right')
# Do this every loop
screen.blit(background, (0,0))
ice_group.draw(screen)
player.draw(screen)
pygame.display.update()
# Count the loops and spawn ice_cream
counter += 1
if counter == 20:
ice_group.add(Stracciatella(ice_cream))
counter = 0
# Check if game lost
if len(pygame.sprite.spritecollide(player, ice_group, True)) > 0:
running = False
pygame.time.wait(10)
time = pygame.time.get_ticks()
if pygame.font:
font = pygame.font.Font(None, 36)
text = font.render("Your score: {}".format(str(int(time / 1000))), 1, (10, 10, 10))
textpos = text.get_rect(centerx=width/2)
background.blit(text, textpos)
player.image = pygame.image.load(os.path.join('images', 'bee-creamed.png')).convert_alpha()
screen.blit(background, (0,0))
player.draw(screen)
running = True
while running:
for event in pygame.event.get():
# If the player quits the game
if event.type == pygame.QUIT:
running = False
pygame.display.update()
# run the main function only if this module is executed as the main script
# (if you import this as a module then nothing is executed)
if __name__=="__main__":
# call the main function
main()