forked from UAB-ACM-Hackathon/ACMHackathon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShroomClicker.py
178 lines (151 loc) · 5.75 KB
/
ShroomClicker.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import os, pygame
from pygame.locals import *
from pygame.compat import geterror
if not pygame.font: print ('Warning, fonts disabled')
if not pygame.mixer: print ('Warning, sound disabled')
main_dir = os.path.split(os.path.abspath(__file__))[0]
def load_image(name): #Used to load the cursor and Toad image files
fullname = os.path.join(main_dir, name)
try:
image = pygame.image.load(fullname)
except pygame.error:
print ('Cannot load image:', fullname)
raise SystemExit(str(geterror()))
return image, image.get_rect()
def load_sound(name): #Used to load the Toad's noises
if not pygame.mixer or not pygame.mixer.get_init():
return NoneSound()
fullname = os.path.join(main_dir, name)
try:
sound = pygame.mixer.Sound(fullname)
except pygame.error:
print ('Cannot load sound: %s' % fullname)
raise SystemExit(str(geterror()))
return sound
class Cursor(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image, self.rect = load_image('cursor.png')
self.clicking = 0
def update(self):
position = pygame.mouse.get_pos()
self.rect.midtop = position
if self.clicking:
self.rect.move_ip(10, 10)
def click(self, target):
if not self.clicking:
self.clicking = 1
hitbox = self.rect.inflate(-5, -5)
return hitbox.colliderect(target.rect)
def unclick(self):
self.clicking = 0
class Toad(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image, self.rect = load_image('blue-toad.png')
screen = pygame.display.get_surface()
self.area = screen.get_rect()
self.rect.topleft = 0, 250
self.move = 9
self.dizzy = 0
def update(self):
if self.dizzy:
self._spin()
else:
self._walk()
def _walk(self):
newPosition = self.rect.move((self.move, 0))
if self.rect.left < self.area.left or \
self.rect.right > self.area.right:
self.move = -self.move
newPosition = self.rect.move((self.move, 0))
self.image = pygame.transform.flip(self.image, 1, 0)
self.rect = newPosition
def _spin(self):
center = self.rect.center
self.dizzy = self.dizzy + 12
if self.dizzy >= 360:
self.dizzy = 0
self.image = self.original
else:
rotate = pygame.transform.rotate
self.image = rotate(self.original, self.dizzy)
self.rect = self.image.get_rect(center=center)
def clicked(self):
if not self.dizzy:
self.dizzy = 1
self.original = self.image
def main():
pygame.init()
size = width, height = 800, 500
screen = pygame.display.set_mode(size)
pygame.display.set_caption('Shroom Clicker')
pygame.mouse.set_visible(0)
background = pygame.image.load("game-background.jpg")
screen.blit(background,(0, 0))
pygame.display.flip()
clock = pygame.time.Clock()
miss_sound = load_sound('Toad3.wav')
click_sound = load_sound('Toad7.wav')
toad = Toad()
cursor = Cursor()
sprites = pygame.sprite.RenderPlain((cursor, toad))
score = 0
font = pygame.font.Font(None, 36)
text = font.render("Click Toad to collect coins!", 1, (255, 255, 255))
textpos = (235, 450)
background.blit(text, textpos)
going = True
while going:
clock.tick(60)
for event in pygame.event.get():
if event.type == QUIT:
going = False
elif event.type == KEYDOWN and event.key == K_ESCAPE:
going = False
elif event.type == MOUSEBUTTONDOWN:
if cursor.click(toad):
click_sound.play()
toad.clicked()
score = score + 1
surface = pygame.image.load("banner.png")
coin = pygame.image.load("coin.png")
font = pygame.font.Font(None, 36)
text = font.render("x" + str(score), 1, (255, 255, 255))
textpos = (60, 10)
bannerpos = (-25, -10)
coinpos = (20, 8)
background.blit(surface, bannerpos)
background.blit(text, textpos)
background.blit(coin, coinpos)
else:
miss_sound.play()
if (score != 0):
score = score - 1
surface = pygame.image.load("banner.png")
coin = pygame.image.load("coin.png")
font = pygame.font.Font(None, 36)
text = font.render("x" + str(score), 1, (255, 0, 0))
textpos = (60, 10)
bannerpos = (-25, -10)
coinpos = (20, 8)
background.blit(surface, bannerpos)
background.blit(text, textpos)
background.blit(coin, coinpos)
elif event.type == MOUSEBUTTONUP:
cursor.unclick()
sprites.update()
time = round(pygame.time.get_ticks()*0.001, 2)
surface = pygame.image.load("banner.png")
font = pygame.font.Font(None, 36)
text = font.render("Timer: " + str(time), 1, (255, 255, 255))
textpos = (600, 10)
bannerpos = (400, -10)
background.blit(surface, bannerpos)
background.blit(text, textpos)
screen.blit(background, (0, 0))
sprites.draw(screen)
pygame.display.flip()
pygame.quit()
if __name__ == '__main__':
main()