-
Notifications
You must be signed in to change notification settings - Fork 2
/
world.py
144 lines (120 loc) · 3.82 KB
/
world.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
import pygame
import os
import random
class World():
size = width, height = 1600, 900
gravity = .4
def __init__(self):
self.screen = pygame.display.set_mode(self.size)
self.objects = pygame.sprite.Group() #hold random objects/sprites
self.enemyObjects = pygame.sprite.Group()
self.players = pygame.sprite.Group()
self.player = None
self.clock = pygame.time.Clock()
self.playerFlip = True
self.gameOverFlag = 0
self.gameOverCount = 0
#gets called by main game loop to do everything
def Update(self):
self.getEvents()
if self.player.dead:
self.gameOver()
return -1
if self.player.win:
self.gameWon()
return -1
self.Draw()
self.level.Update()
for obj in self.objects:
if not obj.Update():
obj.kill()
for obj in self.enemyObjects:
if not obj.Update():
obj.kill()
def gameWon(self):
count = 0
while count < 4:
background = pygame.image.load("images/winscreen1.png")
backgroundRect = background.get_rect()
self.screen.blit(background, backgroundRect)
pygame.display.update()
pygame.time.delay(300)
background = pygame.image.load("images/winscreen2.png")
backgroundRect = background.get_rect()
self.screen.blit(background, backgroundRect)
pygame.display.update()
pygame.time.delay(300)
background = pygame.image.load("images/winscreen3.png")
backgroundRect = background.get_rect()
self.screen.blit(background, backgroundRect)
pygame.display.update()
pygame.time.delay(300)
background = pygame.image.load("images/winscreen4.png")
backgroundRect = background.get_rect()
self.screen.blit(background, backgroundRect)
pygame.display.update()
pygame.time.delay(300)
count += 1
def gameOver(self):
while self.gameOverCount < 100:
pygame.font.init()
fontPos = [random.randint(0,1200), random.randint(0,800)]
if self.gameOverFlag == 0:
self.screen.fill(pygame.Color(224,24,13))
fontobj = pygame.font.Font(None,80)
msg = fontobj.render("YOU ARE LOSE", 1, (0,0,0))
self.screen.blit(msg,fontPos, area=None, special_flags=0)
elif self.gameOverFlag == 1:
self.screen.fill(pygame.Color(240,38,240))
fontobj = pygame.font.Font(None,80)
msg = fontobj.render("YOU ARE LOSE", 1, (224,24,13))
self.screen.blit(msg,fontPos, area=None, special_flags=0)
elif self.gameOverFlag == 2:
self.screen.fill(pygame.Color(40,10,230))
fontobj = pygame.font.Font(None,80)
msg = fontobj.render("YOU ARE LOSE", 1, (255,255,255))
self.screen.blit(msg,fontPos, area=None, special_flags=0)
self.gameOverFlag += 1
if self.gameOverFlag == 3:
self.gameOverFlag = 0
pygame.display.update()
pygame.time.delay(10)
self.gameOverCount += 1
self.gameOver()
#pygame.time.delay(3000)
#do all the drawing
def Draw(self):
#self.screen.fill(pygame.Color(255,255,255))
#self.screen.blit(self.bg1,(0,0))
self.level.Draw()
#if player takes damage have him flickr beautifully
if self.player.invulnDuration > 0:
self.playerFlip = not self.playerFlip
else:
self.playerFlip = True
if self.playerFlip:
self.players.draw(self.screen)
self.level.fg.Draw(self.screen)
self.drawGUI()
def drawGUI(self):
imgPath = os.path.dirname(os.path.dirname( os.path.realpath( __file__ ) ) ) + "/OSGCC6/images/bean.png"
img = pygame.image.load(imgPath)
for i in range(self.player.hp):
self.screen.blit(img,(40 * (i + 1), 50))
def getEvents(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.QUIT
sys.exit()
keystate = pygame.key.get_pressed()
if keystate:
self.player.Update(keystate)
def addPlayer(self, player):
self.players.add(player)
self.player = player
def setLevel(self, level):
self.level = level
def addObject(self, object):
self.objects.add(object)
def checkCollision(self, obj, newPos):
return self.level.checkCollision(obj, newPos)