-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMasteroids.py
executable file
·153 lines (125 loc) · 3.99 KB
/
Masteroids.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
#!/usr/bin/env python2
import os, pygame, random
from pygame.locals import *
from Settings import *
from Spaceship import *
from Asteroids import *
import Utils
class FpsMeter():
def __init__(self):
self.font = pygame.font.Font(None, 20)
def update(self):
self.fps = self.font.render(str(int(clock.get_fps()))+"fps", 1, (70, 70, 70))
def draw(self, surface, x=10, y=10):
surface.blit(self.fps, (x,y))
return self.fps.get_rect().move((x,y))
class Background():
def __init__(self):
self.surface = pygame.Surface(Settings.map_size)
self.surface.fill(Color(0,0,0))
for i in range(0, random.randint(30,500)):
c = random.randint(50, 245)
rc = c+random.randint(-10, 10)
gc = c+random.randint(-10, 10)
bc = c+random.randint(-10, 10)
pos = (random.randint(0, Settings.map_size[0]), random.randint(0, Settings.map_size[1]))
pygame.draw.circle(self.surface, Color(rc, gc, bc), pos, 0)
def update(self):
pass
def draw(self, surface):
surface.blit(self.surface, (0,0))
def main():
"""main function"""
if not pygame.font:
print("Cannot initialize fonts.")
return
global clock
clock = pygame.time.Clock()
pygame.init()
if Settings.fullscreen:
screenFlags = pygame.FULLSCREEN
#screen_size is the resolution of the screen
else:
screenFlags = pygame.RESIZABLE
screen = pygame.display.set_mode(Settings.screen_size, screenFlags)
pygame.display.set_caption('Multiplayer Asteroids')
pygame.mouse.set_visible(0)
background = Background()
fpsMeter = FpsMeter()
#set up sprites
allsprites = pygame.sprite.RenderUpdates()
spaceships = pygame.sprite.RenderUpdates()
missisles = pygame.sprite.RenderUpdates()
asteroids = pygame.sprite.RenderUpdates()
spaceship = Spaceship([Settings.map_size[0]//3, Settings.map_size[1]//2])
spaceship2 = Spaceship([(Settings.map_size[0]*2) //3, Settings.map_size[1]//2])
asteroid = Asteroid()
allsprites.add(spaceship, spaceship2, asteroid)
asteroids.add(asteroid)
spaceships.add(spaceship, spaceship2)
pygame.display.flip()
while True:
#main game loop
clock.tick(50) #fps
for event in pygame.event.get():
if event.type == USEREVENT and event.code == 0: #someone fired a missisle
event.missisle.add(allsprites, missisles)
elif event.type == KEYDOWN:
if event.key == 273: #up
spaceship.startAccel()
elif event.key == 119: #w
spaceship2.startAccel()
elif event.key == 274: #down
pass
elif event.key == 115: #s
pass
elif event.key == 275: #right
spaceship.startTurnRight()
elif event.key == 97: #a
spaceship2.startTurnRight()
elif event.key == 276: #left
spaceship.startTurnLeft()
elif event.key == 100: #d
spaceship2.startTurnLeft()
elif event.key == 109: #m
spaceship.shoot()
elif event.key == 114: #r
spaceship2.shoot()
elif event.key == 27: #esc
return
elif event.type == KEYUP:
if event.key == 273: #up
spaceship.stopAccel()
elif event.key == 119: #w
spaceship2.stopAccel()
elif event.key == 274: #down
pass
elif event.key == 115: #s
pass
elif event.key == 275: #right
spaceship.stopTurning()
elif event.key == 97: #a
spaceship2.stopTurning()
elif event.key == 276: #left
spaceship.stopTurning()
elif event.key == 100: #d
spaceship2.stopTurning()
elif event.type == QUIT:
return
fpsMeter.update()
allsprites.update()
#pygame.sprite.spritecollide(spaceship, asteroids, True)
pygame.sprite.groupcollide(asteroids, missisles, True, True)
pygame.sprite.groupcollide(asteroids, spaceships, True, True)
pygame.sprite.groupcollide(spaceships, missisles, True, True)
if pygame.sprite.collide_rect(spaceship, spaceship2):
spaceship.kill()
spaceship2.kill()
if not len(asteroids.sprites())>0:#no asteroids
asteroid = Asteroid()
asteroids.add(asteroid)
allsprites.add(asteroid)
pygame.display.update([background.draw(screen), fpsMeter.draw(screen)] + allsprites.draw(screen))
#pygame.display.flip()
if __name__ == '__main__':
main()