-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathworld.py
executable file
·157 lines (138 loc) · 5.85 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
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#----------------------------------------------------------------------
# Author:
# Bruno Dilly <[email protected]>
#
# Copyright (C) 2008 Bruno Dilly
#
# Released under GNU GPL, read the file 'COPYING' for more information
# ----------------------------------------------------------------------
import pygame
from vector import Vector
from game_entity import GameEntity
from cannon import Cannon
class World(object):
def __init__(self, image, bullet_image, screen_size, font_size, game_font, score_font, hi_score):
self.entities = {}
self.entity_id = 0
self.targets = []
self.targets_count = 0
self.background = image
self.bullet_image = bullet_image
self.size = screen_size
# spaceships can fly in 4 different altitudes
self.altitude_max = 3
self.altitudes = []
wh = self.size[1]
distance = wh/6
for alt in range(self.altitude_max+1):
self.altitudes.append([wh - (distance * (alt+2) + distance / 2), False])
self.hi_score = hi_score
self.score = 0
self.font_size = font_size
self.game_font = game_font
self.score_font = score_font
def add_entity(self, entity):
self.entities[self.entity_id] = entity
entity.id = self.entity_id
self.entity_id += 1
def remove_entity(self, entity):
del self.entities[entity.id]
def get(self, entity_id):
if entity_id in self.entities:
return self.entities[entity_id]
else:
return None
def actions(self, time_passed, use_bomb, active_cannon, shoot):
for entity in self.entities.values():
entity.act(time_passed)
if shoot:
if active_cannon == 0:
self.left_cannon.fire()
else:
self.right_cannon.fire()
if use_bomb:
self.destroy_spaceships()
if self.targets_count == 0:
return True
return False
def render(self, surface):
surface.blit(self.background, (0,0))
for entity in self.entities.values():
entity.render(surface)
self.display_score(surface)
def create_targets(self, target1_image, target2_image, city_image, shield_image):
images = (city_image, shield_image, pygame.transform.flip(target1_image, 1, 0),
target1_image, pygame.transform.flip(target2_image, 1, 0), target2_image)
locs_x = [int(x / 32. * self.size[0]) for x in [16, 16, 22, 10, 26, 6]]
locs_y = [int(y / 24. * self.size[1]) for y in [20, 20, 20, 20, 19, 19]]
for target_i in range(6):
location = Vector(locs_x[target_i], locs_y[target_i])
target = GameEntity(self, "target", images[target_i], location, 0)
self.add_entity(target)
self.targets.append(target)
self.targets_count += 1
def add_target(self):
if self.targets_count < len(self.targets):
t = self.targets[self.targets_count]
target = GameEntity(self, "target", t.image, t.location, 0)
self.add_entity(target)
self.targets[self.targets_count] = target
self.targets_count += 1
def get_next_target(self):
if self.targets_count > 0:
return self.targets[self.targets_count-1]
else:
return None
def remove_target(self, target):
self.targets_count -= 1
target.destroy()
def display_text(self, message, screen, font, font_size, location, color):
text_surface = font.render(message, True, (0,0,0))
x = location[0] + font_size/2 - text_surface.get_width()/2
y = location[1] + font_size/2 - text_surface.get_height()/2
screen.blit(text_surface, (x, y))
text_surface = font.render(message, True, color)
x = location[0] - text_surface.get_width()/2
y = location[1] - text_surface.get_height()/2
screen.blit(text_surface, (x, y))
def display_score(self, screen):
y = self.size[1] - 1.5 * self.font_size
x = 10 * self.font_size
message = "HI-SCORE %08d" % self.hi_score
self.display_text(message, screen, self.score_font, self.font_size/2, [x, y], (0, 255, 0))
x = self.size[0] - 10 * self.font_size
message = "SCORE %08d" % self.score
self.display_text(message, screen, self.score_font, self.font_size/2, [x, y], (0, 255, 0))
def create_cannons(self, l_cannon_image, r_cannon_image):
l_location = Vector( 1 / 16. * self.size[0], 19 / 24. * self.size[1])
r_location = Vector( 15 / 16. * self.size[0], 19 / 24. * self.size[1])
self.left_cannon = Cannon(self, l_cannon_image, l_location)
self.add_entity(self.left_cannon)
self.right_cannon = Cannon(self, r_cannon_image, r_location)
self.add_entity(self.right_cannon)
def test_colision(self, bullet):
for entity in self.entities.values():
if entity.kind == "spaceship":
bx, by = bullet.location
bw, bh = bullet.image.get_size()
sx, sy = entity.location
sw, sh = entity.image.get_size()
if (bx < sx + sw/2 + bw/2 and
bx > sx - sw/2 - bw/2 and
by < sy + sh/2 + bh/2 and
by > sy - sh/2 - bh/2):
# colision detected
# score incremented
self.inc_score(entity)
bullet.destroy()
entity.destroy()
def destroy_spaceships(self):
for entity in self.entities.values():
if entity.kind == "spaceship":
# score incremented
self.inc_score(entity)
entity.destroy()
def inc_score(self, entity):
self.score += int(100*entity.speed)