-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject.py
More file actions
146 lines (120 loc) · 4.62 KB
/
project.py
File metadata and controls
146 lines (120 loc) · 4.62 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
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
import pygame, os, random
from projectClasses import Gun, Bat, Background
def main():
pygame.init()
clock = pygame.time.Clock()
pygame.display.set_caption('BatShooterGame')
GameScore = 0
GameState = True
max_width = 1366
max_height = 766
screen = pygame.display.set_mode((max_width, max_height))
pygame.mouse.set_visible(False)
gun, bat, everythingBackground = loading(screen, max_width, max_height)
font_size = 30
text_font = pygame.font.Font('font/Pixeltype.ttf', font_size)
# Intro
everythingBackground.draw(0)
batshootCount = random.randint(12, 24)
background = pygame.image.load('images/background.jpg')
background = pygame.transform.scale(background, (max_width, max_height))
# Game
while True:
if Bat.batShot > batshootCount - 1:
GameState = False
screen.blit(background, (0, 0))
extWhenLoss = bat.update()
gun.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.MOUSEBUTTONUP:
gun.bulletShot(gun.bulletInGun)
if bat.hitTest(gun.gunCrossImage, pygame.mouse.get_pos()[0], pygame.mouse.get_pos()[1]):
GameScore += 1
bat.spawn()
Bat.batShot += 1
gun.update(True)
if event.type == pygame.KEYUP:
if event.key == pygame.K_r:
if gun.bulletInGun < 12:
if gun.gunReload():
gun.update()
Gun.timeDelayGun = 24 * 1
if extWhenLoss:
loser(screen, max_width, max_height, clock)
if not GameState:
break
text = f"You have shot {Bat.batShot} monsters"
text_surface = text_font.render(text, False, 'White')
screen.blit(text_surface, (len(text), font_size))
text = f"Goal: {batshootCount}"
text_surface = text_font.render(text, False, 'White')
screen.blit(text_surface, (len(text) + 10, font_size * 2))
pygame.display.flip()
clock.tick(24)
Background.intro = False
text = f"You have shot {Bat.batShot} monsters"
# Outro
everythingBackground.draw(1)
# function to load images in directory and resize
def loadImages(folderPath, imageSize):
image_files = [imageFile for imageFile in os.listdir(folderPath) if imageFile.lower().endswith('.png')]
images = []
for imageFile in image_files:
image_path = os.path.join(folderPath, imageFile)
image = pygame.image.load(image_path).convert_alpha()
image = pygame.transform.scale(image, imageSize)
images.append(image)
return images
# unnecessary function created to meet the cs50p requirement because of how useless the requirement truly is
# created to return the bat, gun and background variables
def loading(screen, max_width, max_height):
gun = Gun(
12, loadImages('images/handGun', (200, 250)),
screen, 25,
'images/cross.png',
'images/bullet.png',
12,
max_width,
max_height
)
bat = Bat(loadImages('images/batHorizontal', (20, 20)),
loadImages('images/batHorizontalRed', (20, 20)),
20,
screen,
max_width,
max_height)
everythingBackground = Background(
loadImages('images/intro', (max_width, max_height)),
loadImages('images/outro', (max_width, max_height)),
screen,
max_width,
max_height
)
return gun, bat, everythingBackground
# If the player loses the game
def loser(screen, max_width, max_height, clock):
i = 0
while 1:
font_size = 60
text_font = pygame.font.Font('font/Pixeltype.ttf', font_size)
screen.fill((0, 0, 0))
text = "YOU DIED LOSER!"
text_surface = text_font.render(text[:i], False, 'Red')
screen.blit(text_surface, (max_width / 2 - len(text) * 8, max_height / 2 - 1/8 * max_height))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
pygame.quit()
exit()
if i < len(text) - 1:
i += 1
pygame.display.flip()
clock.tick(10)
if __name__ == "__main__":
main()