-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstartrek.py
176 lines (134 loc) · 5.08 KB
/
startrek.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
import os
import pygame
import pygame.constants as const
from pygame.compat import geterror
main_dir = os.path.split(os.path.abspath(__file__))[0]
data_dir = os.path.join(main_dir, 'data')
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
def load_image(name, colorkey=None):
fullname = os.path.join(data_dir, name)
try:
image = pygame.image.load(fullname)
except pygame.error:
print('Cannot load image:', fullname)
raise SystemExit(str(geterror()))
image = image.convert()
if colorkey is not None:
if colorkey is -1:
colorkey = image.get_at((0, 0))
image.set_colorkey(colorkey, const.RLEACCEL)
return image, image.get_rect()
class Enterprise(pygame.sprite.Sprite):
def __init__(self):
super(). __init__()
self.image, self.rect = load_image("ship_enterprise.png", WHITE)
def update(self):
pos = pygame.mouse.get_pos()
self.rect.midtop = pos
class Torpedo(pygame.sprite.Sprite):
def __init__(self):
super(). __init__()
self.image, self.rect = load_image("federation_torpedo.png", BLACK)
self.speed = 10
def update(self):
self.rect.x += self.speed
class Warbird(pygame.sprite.Sprite):
def __init__(self):
super(). __init__()
self.image, self.rect = load_image("ship_warbird.png", WHITE)
screen = pygame.display.get_surface()
self.area = screen.get_rect()
self.rect.right = self.area.right
self.rect.top = 10
self.move_y = 4
def update(self):
newpos = self.rect.move((0, self.move_y))
if newpos.bottom > self.area.bottom or \
newpos.top < self.area.top:
self.move_y = -self.move_y
newpos = self.rect.move((0, self.move_y))
self.rect = newpos
class Score(pygame.sprite.Sprite):
def __init__(self, xy):
pygame.sprite.Sprite.__init__(self)
self.xy = xy
self.font = pygame.font.Font(None, 50)
# Our font color in rgb
self.color = (255, 165, 0)
self.score = 0 # start at zero
self.render()
def update(self):
pass
def add(self, points):
"""Adds the given number of points to the score."""
self.score += points
self.render()
def render(self):
"""Updates the score. Renders a new image and re-centers at the initial coordinates."""
self.image = self.font.render("Score: {}".format(self.score), True, self.color)
self.rect = self.image.get_rect()
self.rect.topleft = self.xy
class TheGame:
DISPLAY_SIZE = (1200, 700)
def __init__(self):
self.screen = None
self.bg_image = None
pygame.init()
def init_display(self, display_width, display_height):
mode = pygame.RESIZABLE
size = (display_width, display_height)
self.screen = pygame.display.set_mode(size, mode)
self.bg_image, _ = load_image('background2.jpg')
# Apply the background
self.screen.blit(self.bg_image, (0, 0))
def run(self):
# initial display setup
self.init_display(*self.DISPLAY_SIZE)
pygame.display.set_caption('STAR TREK WARS I')
pygame.display.update()
self.handle_events()
pygame.quit()
def handle_events(self):
clock = pygame.time.Clock()
enterprise = Enterprise()
warbird = Warbird()
score = Score((0, 0))
allsprites = pygame.sprite.RenderUpdates((enterprise, warbird, score))
torpedo_list = pygame.sprite.Group()
while True:
full_screen_update = False
# limit loop to 60 frames per second
clock.tick(60)
# clear previous draws to avoid trailing effect
allsprites.clear(self.screen, self.bg_image)
# Handle Input Events
for event in pygame.event.get():
if event.type == const.QUIT:
return
elif event.type == const.KEYDOWN and event.key == const.K_ESCAPE:
return
elif event.type == pygame.VIDEORESIZE:
self.init_display(event.w, event.h)
full_screen_update = True
elif event.type == pygame.MOUSEBUTTONDOWN:
torpedo = Torpedo()
# Set the torpedo so it is where the player is
torpedo.rect.center = enterprise.rect.center
# Add the torpedo to the lists
allsprites.add(torpedo)
torpedo_list.add(torpedo)
allsprites.update()
# Collision mechanics for torpedos & warbird
for torpedo in pygame.sprite.spritecollide(warbird, torpedo_list, True):
torpedo_list.remove(torpedo)
allsprites.remove(torpedo)
score.add(1)
if full_screen_update:
pygame.display.update()
else:
changes = allsprites.draw(self.screen)
pygame.display.update(changes)
if __name__ == '__main__':
TheGame().run()