-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaimTrainer.py
More file actions
206 lines (179 loc) · 7.42 KB
/
aimTrainer.py
File metadata and controls
206 lines (179 loc) · 7.42 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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import pygame
import random
import sys
from pygame.locals import *
pygame.init()
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 211, 67)
GREEN = (0, 100, 0)
DEEPSKYEBLUE = (0, 191, 255)
DARKBLUE = (0, 0, 40)
WINDOWHEIGHT = 720
WINDOWWIDTH = 1280
FONT = pygame.font.SysFont(None, 48)
def terminate():
pygame.quit()
sys.exit()
def Menu():
timer = 0
color = BLUE
switch = False
while True:
windowSurface.fill(DARKBLUE)
nameRects = []
difficultyRects = []
nameRects.append(pygame.Rect(393, 630, 463, 60))
nameRects.append(pygame.Rect(20, 130, 1240, 20))
difficultyRects.append(pygame.Rect(255, 370, 240, 100))
difficultyRects.append(pygame.Rect(520, 370, 240, 100))
difficultyRects.append(pygame.Rect(785, 370, 240, 100))
for event in pygame.event.get():
if event.type == QUIT:
terminate()
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
terminate()
if event.type == MOUSEBUTTONDOWN:
if difficultyRects[0].collidepoint(pygame.mouse.get_pos()):
game("easy")
if difficultyRects[1].collidepoint(pygame.mouse.get_pos()):
game("medium")
if difficultyRects[2].collidepoint(pygame.mouse.get_pos()):
game("hard")
for rect in difficultyRects:
pygame.draw.rect(windowSurface, RED, rect)
for rect in nameRects:
pygame.draw.rect(windowSurface, DEEPSKYEBLUE, rect)
drawText("WELCOME TO AIM TRAINER", windowSurface, 20, 60, pygame.font.SysFont(None, 112, True, True), YELLOW)
drawText("PICK A DIFFICULTY", windowSurface, 345, 260, pygame.font.SysFont(None, 95), color)
drawText("EASY", windowSurface, 332, 400, FONT, WHITE)
drawText("MEDIUM", windowSurface, 575, 400, FONT, WHITE)
drawText("HARD", windowSurface, 855, 400, FONT, WHITE)
drawText("-Aaditya Panda, CSE(IOT)-A", windowSurface, 400, 641, FONT, WHITE)
mainClock.tick(50)
timer += 1
if timer % 100 == 0:
color = BLUE
elif timer % 50 == 0:
color = RED
pygame.display.update()
def drawText(text, surface, x, y, font=FONT, color=RED):
textObject = font.render(text, 1, color)
textRect = textObject.get_rect()
textRect.topleft = (x, y)
surface.blit(textObject, textRect)
def gameOver(totalShots, hitShots, difficulty, score):
pygame.mouse.set_visible(True)
if totalShots != 0 and hitShots != 0:
accuracy = round(hitShots / totalShots * 100)
else:
accuracy = 0
windowSurface.fill(DARKBLUE)
drawText("GAME OVER", windowSurface, 420, 125, pygame.font.SysFont(None, 95, True))
drawText("Click anywhere to restart", windowSurface, 500, 650, pygame.font.SysFont(None, 30, True))
drawText("Accuracy: " + str(accuracy) + "%", windowSurface, 540, 235)
drawText("Score: " + str(score), windowSurface, 580, 270)
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type == QUIT:
terminate()
if event.type == MOUSEBUTTONDOWN:
windowSurface.fill(WHITE)
Menu()
if event.type == KEYDOWN:
terminate()
def populateConfig(difficulty):
global targetImage, difficultyFile
targetImage = pygame.image.load("assets/images/targetblue.jpg")
config = {}
if difficulty == "easy":
difficultyFile = open("assets/texts/easy.txt", "r")
elif difficulty == "medium":
difficultyFile = open("assets/texts/medium.txt", "r")
elif difficulty == "hard":
difficultyFile = open("assets/texts/hard.txt", "r")
for line in difficultyFile:
splitLine = line.split(":")
splitLine[1] = splitLine[1].strip("\n")
config[splitLine[0]] = int(splitLine[1])
targetImage = pygame.transform.scale(targetImage, (config["enemySize"], config["enemySize"]))
difficultyFile.close()
return config
mainClock = pygame.time.Clock()
windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
pygame.display.set_caption("sniper")
shootSound = pygame.mixer.Sound("assets/sounds/snipersound.wav")
hitSound = pygame.mixer.Sound("assets/sounds/metalHit.wav")
shootSound.set_volume(0.1)
hitSound.set_volume(0.1)
enemies = []
def game(difficulty):
config = populateConfig(difficulty)
pygame.mouse.set_visible(False)
mouseY = (round(WINDOWHEIGHT / 2))
mouseX = (round(WINDOWWIDTH / 2))
tickCounter = 0
enemies = []
amountOfEnemies = 0
score = 0
FPS = 75
hitShots = 0
totalShots = 0
STARTINGTIME = config.get("time")
CIRCLERADIUS = 50
while True:
if config.get("time") <= 0:
gameOver(totalShots, hitShots, difficulty, score)
tickCounter += 1
if tickCounter % FPS == 0:
config["time"] -= 1
windowSurface.fill(WHITE)
if amountOfEnemies == 0:
config["time"] = STARTINGTIME
while amountOfEnemies != config.get("maxAmountOfEnemies"):
enemies.append(pygame.Rect((random.randint(0, WINDOWWIDTH - config.get("enemySize"))),
(random.randint(0, WINDOWHEIGHT - config.get("enemySize"))),
config.get("enemySize"), config.get("enemySize")))
if enemies[amountOfEnemies].topleft[0] < 135 and enemies[amountOfEnemies].topleft[1] < 65:
enemies.pop(amountOfEnemies)
else:
amountOfEnemies += 1
for event in pygame.event.get():
if event.type == QUIT:
terminate()
if event.type == KEYDOWN:
pass
if event.type == KEYUP:
if event.key == K_ESCAPE:
terminate()
if event.type == MOUSEMOTION:
mouseX = event.pos[0]
mouseY = event.pos[1]
if event.type == MOUSEBUTTONDOWN:
pygame.mixer.Channel(0).play(shootSound)
totalShots += 1
for enemy in enemies[:]:
if mouseX > enemy.topleft[0] and mouseX < enemy.bottomright[0] \
and mouseY > enemy.topleft[1] and mouseY < enemy.bottomright[1]:
pygame.mixer.Channel(1).play(hitSound)
enemies.remove(enemy)
amountOfEnemies -= 1
score += 1
hitShots += 1
for enemy in enemies:
windowSurface.blit(targetImage, enemy)
pygame.draw.circle(windowSurface, GREEN, (mouseX, mouseY),
CIRCLERADIUS + 1, 3)
pygame.draw.line(windowSurface, GREEN, (mouseX, mouseY + 50),
(mouseX, mouseY - 50), 2)
pygame.draw.line(windowSurface, GREEN, (mouseX + 50, mouseY),
(mouseX - 50, mouseY), 2)
drawText("Time: " + str(config.get("time")), windowSurface, 8, 8)
drawText("Score: " + str(score), windowSurface, 8, 38)
pygame.display.update()
mainClock.tick(FPS)
Menu()