Skip to content

Commit 5e77d88

Browse files
committed
pygame malware
0 parents  commit 5e77d88

18 files changed

+345
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
**/.vscode
2+
**/__pycache__

Malware_Game/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.vscode
2+
__pycache__

Malware_Game/AssetsLibrary.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# export a dictionary which stores the assets used for the backgrounds
2+
3+
backgrounds = {
4+
0: "assets/0-20 Percent Health.png",
5+
20: "assets/20-40 Percent Health.png",
6+
40: "assets/40-60 Percent Health.png",
7+
60: "assets/60-80 Percent Health.png",
8+
80: "assets/80-100 Percent Health.png",
9+
100: "assets/80-100 Percent Health.png"
10+
}
11+
12+
decoderPath = "assets/decoder.png"

Malware_Game/Settings.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# settings should store all of the settings, I want to be able to do Settings.speed, Settings.width, Settings.height, etc.
2+
3+
Settings = {
4+
"speed": 1,
5+
"backgrounds": [
6+
"assets/backgrounds/100.png",
7+
"assets/backgrounds/80.png",
8+
"assets/backgrounds/60.png",
9+
"assets/backgrounds/40.png",
10+
"assets/backgrounds/20.png",
11+
"assets/backgrounds/0.png"
12+
]
13+
}
129 KB
Loading
151 KB
Loading
165 KB
Loading
129 KB
Loading
149 KB
Loading

Malware_Game/assets/Thumbs.db

7.5 KB
Binary file not shown.

Malware_Game/assets/cursor.png

680 Bytes
Loading

Malware_Game/assets/cursor2.png

730 Bytes
Loading

Malware_Game/assets/decoder-dead.png

921 Bytes
Loading

Malware_Game/assets/decoder.png

941 Bytes
Loading

Malware_Game/assets/icon.png

13.4 KB
Loading

Malware_Game/decoder.py

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import random
2+
import pygame
3+
from AssetsLibrary import decoderPath
4+
5+
class Decoder:
6+
def __init__(self, x, y, screen, level):
7+
self.x = x
8+
self.y = y
9+
self.screen = screen
10+
self.health = 10
11+
self.size = 50
12+
self.level = level
13+
self.rotation = 0
14+
self.cooldown = 0
15+
16+
def drawImage(self):
17+
image = self.health > 0 and pygame.image.load(decoderPath) or pygame.image.load("assets/decoder-dead.png")
18+
image = pygame.transform.scale(image, (self.size, self.size))
19+
image = pygame.transform.rotate(image, self.rotation)
20+
# opacity should be 255 - if there is a cooldown then it should increase opacity until the cooldown is over
21+
opacity = (self.cooldown > 0 and self.health > 0) and 255 - self.cooldown * 2 or 255
22+
image.set_alpha(opacity)
23+
self.screen.blit(image, (self.x, self.y))
24+
self.cooldown -= 0.5
25+
26+
def draw(self):
27+
self.drawImage()
28+
29+
def move(self, x, y):
30+
self.x += x
31+
self.y += y
32+
33+
def remove(self):
34+
self.health = 0
35+
self.x = -100
36+
self.y = -100
37+
38+
def getHealth(self):
39+
return self.health
40+
41+
def getSize(self):
42+
return self.size
43+
44+
def getLevel(self):
45+
return self.level
46+
47+
def setLevel(self, level):
48+
self.level = level
49+
return self
50+
51+
def getX(self):
52+
return self.x
53+
54+
def getY(self):
55+
return self.y
56+
57+
def setX(self, x):
58+
self.x = x
59+
return self
60+
61+
def setY(self, y):
62+
self.y = y
63+
return self
64+
65+
def getRect(self):
66+
return pygame.Rect(self.x, self.y, self.size, self.size)
67+
68+
def getRotation(self):
69+
return self.rotation
70+
71+
def getCenter(self):
72+
return (self.x + self.size / 2, self.y + self.size / 2)
73+
74+
def getDistance(self, other):
75+
return ((self.getCenter()[0] - other.getCenter()[0]) ** 2 + (self.getCenter()[1] - other.getCenter()[1]) ** 2) ** 0.5
76+
77+
def shoot(self, malwares): # malwares is a list of malware objects
78+
if self.health <= 0:
79+
return
80+
if self.cooldown > 0:
81+
return
82+
nearestMalware = None
83+
for malware in malwares:
84+
if nearestMalware == None:
85+
nearestMalware = malware
86+
elif self.getDistance(malware) < self.getDistance(nearestMalware):
87+
nearestMalware = malware
88+
if nearestMalware != None:
89+
if self.getDistance(nearestMalware) < 150:
90+
self.shootAt(malwares, nearestMalware)
91+
92+
def shootAt(self, malwares, malware):
93+
if self.health <= 0:
94+
return
95+
if self.cooldown > 0:
96+
return
97+
pygame.draw.line(self.screen, (255, 50, 50), self.getCenter(), malware.getCenter(), 5)
98+
99+
if self.cooldown <= 0:
100+
self.cooldown = 120
101+
malware.health -= 1
102+
self.health -= 1
103+
if malware.health <= 0:
104+
malware.remove()
105+
malwares.remove(malware)

Malware_Game/main.py

+144
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
import random
2+
import pygame
3+
from malware import Malware
4+
from AssetsLibrary import backgrounds
5+
from decoder import Decoder
6+
7+
# pygame setup
8+
pygame.init()
9+
screen = pygame.display.set_mode((1280, 720))
10+
pygame.display.set_caption("The Malware Game")
11+
pygame.display.set_icon(pygame.image.load("assets/icon.png"))
12+
pygame.mouse.set_visible(False)
13+
14+
clock = pygame.time.Clock()
15+
running = True
16+
lastClickedKeybind = "None"
17+
malwares = []
18+
cpuHealth = 100
19+
level = 1
20+
decoders = []
21+
22+
def chanceOfSpawningMalware():
23+
return level / 100
24+
25+
def shouldSpawnMalware():
26+
return random.random() < chanceOfSpawningMalware()
27+
28+
def checkIfAndSpawnMalware():
29+
if shouldSpawnMalware():
30+
malwares.append(Malware(151, 675, screen))
31+
32+
def drawSidebar():
33+
sidebarWidth = screen.get_width() / 4
34+
sidebarHeight = screen.get_height()
35+
sidebar = pygame.Rect(screen.get_width() - sidebarWidth, 0, sidebarWidth, sidebarHeight)
36+
sidebarColor = pygame.Color("black")
37+
pygame.draw.rect(screen, sidebarColor, sidebar)
38+
39+
pygame.font.init()
40+
font = pygame.font.SysFont('Arial', 30)
41+
text = font.render("CPU Health: " + str(cpuHealth) + "%", False, (255, 255, 255))
42+
screen.blit(text, (sidebar.x + 20, sidebar.y + 20))
43+
44+
levelFont = pygame.font.SysFont("Arial", 25)
45+
subtitle = pygame.font.SysFont("Arial", 20)
46+
levelText = levelFont.render("Level " + str(level), False, (255, 255, 255))
47+
screen.blit(levelText, (sidebar.x + 20, sidebar.y + 60))
48+
49+
selectYourDecoderText = subtitle.render("Select Your Decoder", False, (200, 200, 200))
50+
screen.blit(selectYourDecoderText, (sidebar.x + 20, sidebar.y + 90))
51+
52+
decoderOpacity = len(decoders) >= 5 and 100 or 255
53+
decoderPath = "assets/decoder.png"
54+
decoderImage = pygame.image.load(decoderPath)
55+
decoderImage = pygame.transform.scale(decoderImage, (50, 50))
56+
decoderImage.set_alpha(decoderOpacity)
57+
screen.blit(decoderImage, (sidebar.x + 20, sidebar.y + 110))
58+
59+
cursorPosition = [0, 0]
60+
clickedDecoder = False
61+
alert = {
62+
"time": 0,
63+
"message": ""
64+
}
65+
while running:
66+
for event in pygame.event.get():
67+
if event.type == pygame.QUIT:
68+
running = False
69+
elif event.type == pygame.KEYDOWN:
70+
lastClickedKeybind = pygame.key.name(event.key)
71+
72+
73+
checkIfAndSpawnMalware()
74+
75+
screen.fill("white")
76+
if cpuHealth <= 0:
77+
cpuHealth = 0
78+
running = False
79+
80+
current_background = backgrounds[cpuHealth // 20 * 20]
81+
screen.blit(pygame.image.load(current_background), (0, 0))
82+
83+
drawSidebar()
84+
85+
for malware in malwares:
86+
malware.draw()
87+
malware.move()
88+
if malware.y < -20:
89+
malware.remove()
90+
cpuHealth -= 1
91+
malwares.remove(malware)
92+
93+
for decoder in decoders:
94+
decoder.shoot(malwares)
95+
decoder.draw()
96+
97+
mousePosition = pygame.Vector2(pygame.mouse.get_pos())
98+
for malware in malwares:
99+
if malware.getRect().collidepoint(mousePosition):
100+
malware.remove()
101+
malwares.remove(malware)
102+
103+
# to render over everything else
104+
for event in pygame.event.get():
105+
if event.type == pygame.MOUSEMOTION:
106+
cursorPosition = event.pos
107+
elif event.type == pygame.MOUSEBUTTONDOWN:
108+
if len(decoders) < 5:
109+
if clickedDecoder:
110+
clickedDecoder = False
111+
decoders.append(Decoder(cursorPosition[0] - 25, cursorPosition[1] - 25, screen, level))
112+
if pygame.Rect(screen.get_width() - screen.get_width() / 4 + 20, 110, 50, 50).collidepoint(event.pos):
113+
clickedDecoder = True
114+
elif pygame.Rect(screen.get_width() - screen.get_width() / 4 + 20, 110, 50, 50).collidepoint(event.pos):
115+
alert = {
116+
"time": 60 * 3,
117+
"message": "You can only have 5 decoders!"
118+
}
119+
120+
121+
122+
if not clickedDecoder:
123+
cursorPath = "assets/cursor.png" # w=12 h=19
124+
cursorImage = pygame.image.load(cursorPath)
125+
cursorImage = pygame.transform.scale(cursorImage, (12, 19))
126+
screen.blit(cursorImage, (cursorPosition[0], cursorPosition[1]))
127+
else:
128+
cursorPath = "assets/decoder.png"
129+
cursorImage = pygame.image.load(cursorPath)
130+
cursorImage = pygame.transform.scale(cursorImage, (50, 50))
131+
screen.blit(cursorImage, (cursorPosition[0] - 25, cursorPosition[1] -25))
132+
133+
if alert["time"] > 0:
134+
alert["time"] -= 1
135+
alertFont = pygame.font.SysFont("Arial", 20)
136+
alertText = alertFont.render(alert["message"], False, (255, 255, 255))
137+
screen.blit(alertText, (screen.get_width() - screen.get_width() / 4 + 20, 170))
138+
139+
# flip() the display to put your work on screen
140+
pygame.display.flip()
141+
142+
clock.tick(60) # limits FPS to 60
143+
144+
pygame.quit()

Malware_Game/malware.py

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# object for malware
2+
3+
import random
4+
import pygame
5+
from Settings import Settings
6+
7+
8+
class Malware:
9+
# Store the x, y, and rectangle from pygame, also store the health: 1-10
10+
def __init__(self, x, y, screen):
11+
self.x = x
12+
self.y = y
13+
self.screen = screen
14+
self.rect = pygame.Rect(self.x, self.y, 20, 20)
15+
self.health = random.randint(1, 2) == 1 and random.randint(1, 2) or 1
16+
self.size = 20
17+
self.aliveTime = 0
18+
19+
def draw(self):
20+
color = self.health > 1 and (255, 0, 0) or (0, 0, 0)
21+
scale = min(20, self.aliveTime * 0.1 + 20)
22+
self.rect.width = scale
23+
self.rect.height = scale
24+
self.rect.x = self.x - scale / 2
25+
self.rect.y = self.y - scale / 2
26+
shape = pygame.draw.rect(self.screen, color, self.rect)
27+
self.aliveTime += 1
28+
29+
def move(self):
30+
# we want it to follow a path, it should start at 151, 675 - then go up to 151, 545. Once at this point it should go to 777, 545, then up to 777, 415, then to 256, 415, then to 253, 415, then to 253, 270, then finally 854, 260.
31+
if self.x == 151 and self.y > 545:
32+
self.y -= Settings["speed"]
33+
elif self.x < 777 and self.y == 545:
34+
self.x += Settings["speed"]
35+
elif self.x == 777 and self.y > 415:
36+
self.y -= Settings["speed"]
37+
elif self.x > 256 and self.y == 415:
38+
self.x -= Settings["speed"]
39+
elif self.x == 256 and self.y > 270:
40+
self.y -= Settings["speed"]
41+
elif self.x == 253 and self.y > 260:
42+
self.y -= Settings["speed"]
43+
elif self.x < 854 and self.y == 260:
44+
self.x += Settings["speed"]
45+
elif self.x == 854 and self.y > 80:
46+
self.y -= Settings["speed"]
47+
# then reduce the x between 854 and 50 - and then just reduce the y
48+
elif self.x > 50 and self.y == 80:
49+
self.x -= Settings["speed"]
50+
else:
51+
self.y -= Settings["speed"]
52+
53+
self.rect.x = self.x
54+
self.rect.y = self.y
55+
56+
def remove(self):
57+
self.health = 0
58+
self.x = -100
59+
self.y = -100
60+
self.rect.x = self.x
61+
self.rect.y = self.y
62+
63+
def getRect(self):
64+
return self.rect
65+
66+
def getCenter(self):
67+
return (self.x + self.size / 2, self.y + self.size / 2)

0 commit comments

Comments
 (0)