File: objguard.py

package info (click to toggle)
solarwolf 1.5%2Bdfsg1-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,656 kB
  • sloc: python: 5,353; ansic: 159; makefile: 102; pascal: 50; sh: 27
file content (190 lines) | stat: -rw-r--r-- 5,491 bytes parent folder | download | duplicates (6)
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
#simple enemy class

import os
import pygame, pygame.image
from pygame.locals import *
import game, gfx

images = []
teleimages = []
openframes = 8.0
shootframes = 5.0
totalframes = 15.0

def load_game_resources():
    #load ship graphics
    global images
    i = gfx.load('baddie.png')
    i = pygame.transform.flip(i, 0, 1)
    i.set_colorkey((0,0,0))
    imgs = gfx.animstrip(i, 64)
    del imgs[9] #clean animation a little
    images.append(imgs)

    for x in (90, 180, 270):
        rotimgs = []
        for i in imgs:
            r = pygame.transform.rotate(i, x)
            rotimgs.append(r)
        images.append(rotimgs)

    i = gfx.load('baddie-teleport.png')
    i = pygame.transform.flip(i, 0, 1)
    i.set_colorkey((0,0,0))
    imgs = gfx.animstrip(i, 64)
    teleimages.append(imgs)
    for x in (90, 180, 270):
        rotimgs = []
        for i in imgs:
            r = pygame.transform.rotate(i, x)
            rotimgs.append(r)
        teleimages.append(rotimgs)


class Guard:
    def __init__(self, type):
        self.images = images[type]
        img = self.images[0]
        self.type = type
        self.dead = 0
        self.rect = img.get_rect()
        if type == 0: #top
            self.rect.bottomleft = game.arena.topleft
            self.move = [game.guard_speed, 0]
        elif type == 1: #left
            self.rect.bottomright = game.arena.bottomleft
            self.move = [0, -game.guard_speed]
        elif type == 2: #bottom
            self.rect.topright = game.arena.bottomright
            self.move = [-game.guard_speed, 0]
        else: #right
            self.rect.topleft = game.arena.topright
            self.move = [0, game.guard_speed]
        self.lastrect = None
        self.firenow = 0
        self.bullets = 0
        self.openstate = 0
        self.time = 0.0
        self.frame = 0
        self.pos = list(self.rect.topleft)

        self.killed = 1


    def shotinfo(self):
        if not self.firenow:
            return None, None
        self.firenow = 0
        self.bullets -= 1

        speed = game.shot_speed
        if self.type == 0: #top
            return self.rect.center, (0, speed)
        elif self.type == 1: #left
            return self.rect.center, (speed, 0)
        elif self.type == 2: #bottom
            return self.rect.center, (0, -speed)
        else: #right
            return self.rect.center, (-speed, 0)


    def erase(self, background):
        if self.lastrect:
            r = background(self.lastrect)
            if self.dead:
                gfx.dirty(r)

    def draw(self, gfx):
        if not self.killed:
            img = self.images[self.frame]
            r = gfx.surface.blit(img, self.rect)
            gfx.dirty2(r, self.lastrect)
            self.lastrect = r


    def tick(self, speedadjust = 1.0):
        if self.killed:
            return

        #move
        self.pos[0] += self.move[0] * speedadjust
        self.pos[1] += self.move[1] * speedadjust
        self.rect.topleft = self.pos
        #checkbounds
        if self.type == 0 or self.type == 2:
            if self.rect.left < game.arena.left:
                self.move[0] = abs(self.move[0])
            if self.rect.right > game.arena.right:
                self.move[0] = -abs(self.move[0])
        else:
            if self.rect.top < game.arena.top:
                self.move[1] = abs(self.move[1])
            if self.rect.bottom > game.arena.bottom:
                self.move[1] = -abs(self.move[1])

        if self.bullets and self.openstate < 3:
            self.time += speedadjust * .6
            if self.openstate == 0: #start open
                self.time = 1.0
                self.frame = 1
                self.openstate = 1
            elif self.openstate == 1: #keep opening
                if self.time > openframes:
                    self.openstate = 2
            else:
                if self.time >= openframes + shootframes - 1.0:
                    if self.bullets > 1:
                        self.time -= 3.5
                    else:
                        self.openstate = 3
                    self.firenow = 1
        else:
            if self.openstate: #waiting to close
                self.time += speedadjust * .6
                if self.time >= totalframes:
                    self.openstate = 0
                    self.time = 0.0

        self.frame = int(self.time)
        if self.frame >= len(self.images):
            self.frame = 0


    def nofire(self):
        self.bullets = 0
        self.firenow = 0
        if self.openstate < openframes + shootframes:
            self.time = openframes + shootframes + 1.0


    def fire(self):
        if not self.killed:
            self.bullets += 1



class TeleGuard:
    def __init__(self, guard):
        self.guard = guard
        self.images = teleimages[guard.type]
        self.rect = self.images[0].get_rect()
        self.rect.center = guard.rect.center
        self.dead = 0
        self.time = 0.0
        self.endtime = len(self.images)*2.6

    def erase(self, background):
        r = background(self.rect)
        if self.dead:
            gfx.dirty(r)

    def draw(self, gfx):
        img = self.images[int((self.time / self.endtime) * len(self.images))]
        r = gfx.surface.blit(img, self.rect)
        gfx.dirty(r)

    def tick(self, speedadjust):
        self.time += speedadjust * 1.2
        if self.time >=  self.endtime:
            self.dead = 1
            self.guard.killed = 0