-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame0.py
212 lines (168 loc) · 5.9 KB
/
game0.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
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
207
208
209
210
211
212
import pygame
import random
import sys
import time
from pygame.locals import *
pygame.init()
FPS = 60
FramePerSec = pygame.time.Clock()
# Predefined some colors
BLUE = (0, 0, 255)
RED = (255, 0, 0)
GREEN = (0, 125, 0)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
#font creation
font = pygame.font.SysFont('calibri', 14)
font1 = pygame.font.SysFont('calibri', 40)
# Screen information
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 800
DISPLAYSURF = pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT))
DISPLAYSURF.fill(BLUE)
pygame.display.set_caption("Game")
class Enemy(pygame.sprite.Sprite):
def __init__(self,v = [0,0]):
super().__init__()
self.vel = v
self.image = pygame.image.load("sprites/mario2.png")
self.rect = self.image.get_rect()
self.rect.center = (SCREEN_WIDTH//2, 0)
def accelerate(self,v):
self.vel=[self.vel[i]+v[i] for i in range(2)]
def move(self):
self.rect.move_ip(self.vel[0],self.vel[1])
if (self.rect.bottom > SCREEN_HEIGHT + self.rect.height):
self.rect.top = 0
self.rect.center = (random.randint(self.rect.width//2,SCREEN_WIDTH-self.rect.width//2),-self.rect.height//2)
self.vel = [0,0]
def hit(self, p):
if p.collision(self) == 1:
w=self.rect.center[0] - p.rect.center[0]
h=self.rect.center[1] - p.rect.center[1]
x=(self.rect.width + p.rect.width)/2
y=(self.rect.height + p.rect.height)/2
if w == 0:
push = abs(y/h)
elif h == 0:
push = abs(x/w)
else:
push=min([abs(x/w),abs(y/h)])
l=(p.rect.center[0] + push*w, p.rect.center[1] + push*h)
self.rect.center = l
#velocity correction:
self.vel = [p.vx,p.vy]
def draw(self, surface):
surface.blit(self.image, self.rect)
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.image.load("sprites/mario2.png")
self.rect = self.image.get_rect()
self.rect.center = (SCREEN_WIDTH//2, 3*SCREEN_HEIGHT//4)
self.health=100
self.speed = 5
self.vx = 0
self.vy = 0
def update(self):
pressed_keys = pygame.key.get_pressed()
#speed
if pressed_keys[K_SPACE]:
self.speed = 10
elif pressed_keys[K_LSHIFT]:
self.speed = 2
else:
self.speed = 5
#vertical movement
if pressed_keys[K_w]:
if self.rect.top > 0:
self.rect.move_ip(0, -self.speed)
self.vy = - self.speed
elif pressed_keys[K_s]:
if self.rect.bottom < SCREEN_HEIGHT:
self.rect.move_ip(0,self.speed)
self.vy = self.speed
else:
self.vy = 0
#horizontal movement
if pressed_keys[K_a]:
if self.rect.left > 0:
self.rect.move_ip(-self.speed, 0)
self.vx = -self.speed
elif pressed_keys[K_d]:
if self.rect.right < SCREEN_WIDTH:
self.rect.move_ip(self.speed, 0)
self.vx = self.speed
else:
self.vx = 0
def collision(self,E):
if E.rect.bottom > self.rect.top and E.rect.top < self.rect.bottom:
if abs(E.rect.center[0]-self.rect.center[0]) < 0.5*(E.rect.width + self.rect.width):
return 1
else:
return 0
else:
return 0
def draw(self, surface):
surface.blit(self.image, self.rect)
surface.blit(font.render(f'Health: {round(self.health,1)}', True, WHITE), (20,20) )
class grass(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.image.load("sprites/grass.png")
self.rect = self.image.get_rect()
self.rect.center = (SCREEN_WIDTH/2,SCREEN_HEIGHT)
self.vx = 0
self.vy = 0
def move(self,x,y):
l=(self.rect.center + x, self.rect.center + y)
self.rect.center = l
def collision(self,E):
if E.rect.bottom > self.rect.top and E.rect.top < self.rect.bottom:
if abs(E.rect.center[0]-self.rect.center[0]) < 0.5*(E.rect.width + self.rect.width):
return 1
else:
return 0
else:
return 0
def draw(self, surface):
surface.blit(self.image, self.rect)
class clock:
def __init__(self):
self.start = time.time()
self.dur = 0
self.image = font.render(f'Time: {round(self.dur,1)} seconds.', True, WHITE)
def update(self):
self.dur = time.time()-self.start
self.image = font.render(f'Time: {round(self.dur,1)} seconds.', True, WHITE)
self.en = font.render(f'Stayed alive for {round(self.dur,4)} seconds!', True, WHITE)
def draw(self, surface):
surface.blit(self.image,(20,40))
def end(self, surface):
x=self.en.get_rect()
surface.blit(self.en,((SCREEN_WIDTH-x.width)//2, 2*(SCREEN_HEIGHT-x.height)//3 ))
P1 = Player()
E1 = Enemy()
C = clock()
g = grass()
#game loop begins
while True:
C.update()
E1.move()
if P1.collision(E1) == 0 and g.collision(E1) ==0 :
E1.accelerate([0,5/60])
else:
E1.hit(P1)
E1.hit(g)
P1.update()
DISPLAYSURF.fill(BLUE)
g.draw(DISPLAYSURF)
P1.draw(DISPLAYSURF)
E1.draw(DISPLAYSURF)
pygame.display.update()
FramePerSec.tick(FPS)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()