-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
135 lines (117 loc) · 4.3 KB
/
main.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
# -*- coding: utf-8 -*-
import pygame
import random
from Player import Player
from Enemy import Enemy
from Projectile import Projectile
pygame.init()
size = (800, 600)
BGCOLOR = (255, 255, 255)
screen = pygame.display.set_mode(size)
scoreFont = pygame.font.Font("fonts/UpheavalPro.ttf", 30)
healthFont = pygame.font.Font("fonts/OmnicSans.ttf", 50)
healthRender = healthFont.render('z', True, pygame.Color('red'))
pygame.display.set_caption("Top Down")
done = False
hero = pygame.sprite.GroupSingle(Player(screen.get_size()))
enemies = pygame.sprite.Group()
lastEnemy = 0
score = 0
clock = pygame.time.Clock()
def move_entities(hero, enemies, timeDelta):
score = 0
hero.sprite.move(screen.get_size(), timeDelta)
for enemy in enemies:
enemy.move(enemies, hero.sprite.rect.topleft, timeDelta)
enemy.shoot(hero.sprite.rect.topleft)
for proj in Enemy.projectiles:
proj.move(screen.get_size(), timeDelta)
if pygame.sprite.spritecollide(proj, hero, False):
proj.kill()
hero.sprite.health -= 1
if hero.sprite.health <= 0:
hero.sprite.alive = False
for proj in Player.projectiles:
proj.move(screen.get_size(), timeDelta)
enemiesHit = pygame.sprite.spritecollide(proj, enemies, True)
if enemiesHit:
proj.kill()
score += len(enemiesHit)
return score
def render_entities(hero, enemies):
hero.sprite.render(screen)
for proj in Player.projectiles:
proj.render(screen)
for proj in Enemy.projectiles:
proj.render(screen)
for enemy in enemies:
enemy.render(screen)
def process_keys(keys, hero):
if keys[pygame.K_w]:
hero.sprite.movementVector[1] -= 1
if keys[pygame.K_a]:
hero.sprite.movementVector[0] -= 1
if keys[pygame.K_s]:
hero.sprite.movementVector[1] += 1
if keys[pygame.K_d]:
hero.sprite.movementVector[0] += 1
if keys[pygame.K_1]:
hero.sprite.equippedWeapon = hero.sprite.availableWeapons[0]
if keys[pygame.K_2]:
hero.sprite.equippedWeapon = hero.sprite.availableWeapons[1]
if keys[pygame.K_3]:
hero.sprite.equippedWeapon = hero.sprite.availableWeapons[2]
def process_mouse(mouse, hero):
if mouse[0]:
hero.sprite.shoot(pygame.mouse.get_pos())
def game_loop():
done = False
hero = pygame.sprite.GroupSingle(Player(screen.get_size()))
enemies = pygame.sprite.Group()
lastEnemy = pygame.time.get_ticks()
score = 0
while hero.sprite.alive and not done:
keys = pygame.key.get_pressed()
mouse = pygame.mouse.get_pressed()
currentTime = pygame.time.get_ticks()
for event in pygame.event.get():
if event.type == pygame.QUIT:
return True
screen.fill(BGCOLOR)
process_keys(keys, hero)
process_mouse(mouse, hero)
# Enemy spawning process
if lastEnemy < currentTime - 200 and len(enemies) < 50:
spawnSide = random.random()
if spawnSide < 0.25:
enemies.add(Enemy((0, random.randint(0, size[1]))))
elif spawnSide < 0.5:
enemies.add(Enemy((size[0], random.randint(0, size[1]))))
elif spawnSide < 0.75:
enemies.add(Enemy((random.randint(0, size[0]), 0)))
else:
enemies.add(Enemy((random.randint(0, size[0]), size[1])))
lastEnemy = currentTime
score += move_entities(hero, enemies, clock.get_time()/17)
render_entities(hero, enemies)
# Health and score render
for hp in range(hero.sprite.health):
screen.blit(healthRender, (15 + hp*35, 0))
scoreRender = scoreFont.render(str(score), True, pygame.Color('black'))
scoreRect = scoreRender.get_rect()
scoreRect.right = size[0] - 20
scoreRect.top = 20
screen.blit(scoreRender, scoreRect)
pygame.display.flip()
clock.tick(120)
done = game_loop()
while not done:
keys = pygame.key.get_pressed()
mouse = pygame.mouse.get_pressed()
currentTime = pygame.time.get_ticks()
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if keys[pygame.K_r]:
done = game_loop()
pygame.quit()