File: game.py

package info (click to toggle)
oneisenough 0.40-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 620 kB
  • ctags: 102
  • sloc: python: 697; makefile: 7; sh: 2
file content (238 lines) | stat: -rw-r--r-- 6,122 bytes parent folder | download | duplicates (3)
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import pygame
import os
import time

from pygame.locals import *

from locals import *

from background import Background
from player import Player
from follower import Follower
from enemy import Enemy
from bank import Bank
from camp import Camp

pygame.font.init()
smallfont = pygame.font.Font(os.path.join("/usr/share/fonts/truetype/dejavu","DejaVuSansMono.ttf"), 14)

dismiss_delay = 0

# The function sends one of the objects of the corresponding type to the labour camp.
def dismiss(objects, object_type):
  print "Sending 1 ball to camp"
  lowest_hp = DEFAULT_HP + 1
  i = 0
  for object in objects:
    if object.get_type() == object_type:
      if not object.to_camp:
        if object.hp < lowest_hp:
          lowest_hp = object.hp
          lowest_i = i
    i += 1
  try:
    objects[lowest_i].to_camp = True
    objects[lowest_i].dec(5)
  except:
    return 0
  return DISMISS_INTERVAL

def render_score(screen, score):
  image = smallfont.render("Score: "+ str(score), 1, (200,100,10))
  rect = image.get_rect()
  rect.left = 18
  rect.top = 140
  screen.blit(image, rect)
  return

# Main game loop
def run(screen):

  idletime = 0
  comptime = 0
  t1 = time.clock()


  enemies_left = 0
  friends_left = 0
  total_spawned = 1

  t = 0
  spawndelay = INITIAL_SPAWN_DELAY

  dismiss_delay = 0

  clock = pygame.time.Clock()

  objects = []

  player = Player(screen)

  objects.append(player)
  objects.append(Enemy(screen))

  done = False
  lose = False
  lose_delay = LOSE_DELAY
  score = 0

  playingfield = Rect(10, 137, SCREEN_WIDTH - 20, SCREEN_HEIGHT - 149)

  #Pre-rendering the background:
  original_background = Background(screen)
  original_background.render()

  pygame.display.flip()

  #Setting the background to the playing field - quite hackish, but works
  background = Background(screen)
  background.image = pygame.Surface((playingfield.width, playingfield.height))
  background.rect = playingfield

  while not done:

    #Spawning banks
    t += 1
    if t > spawndelay:
      t = 0
      spawndelay = int((spawndelay + 10) * 0.9)
      if enemies_left < MAX_ENEMIES_ON_SCREEN:
        new_enemies = int((friends_left + 1) / 3)
        if new_enemies < MIN_NEW_ENEMIES:
          new_enemies = MIN_NEW_ENEMIES
        objects.append(Bank(screen, None, new_enemies))
        total_spawned += new_enemies

    #Dismiss delay:
    if dismiss_delay > 0:
      dismiss_delay -= 1

    refresh_bg = False

    #Event handling
    for event in pygame.event.get():
      if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
        done = True
        refresh_bg = True
      if event.type == VIDEOEXPOSE or event.type == VIDEORESIZE:
        refresh_bg = True

    keys = pygame.key.get_pressed()

    no_acc = True

    if keys[K_UP]:
      no_acc = False
      player.acc((0,-1))
    if keys[K_DOWN]:
      no_acc = False
      player.acc((0,1))
    if keys[K_LEFT]:
      no_acc = False
      player.acc((-1,0))
    if keys[K_RIGHT]:
      no_acc = False
      player.acc((1,0))
    if keys[K_z]:
      player.provocate()
    if keys[K_c] and dismiss_delay == 0 and friends_left > 0:
      dismiss_delay = dismiss(objects, OBJECT_FOLLOWER)
      camp_exists = False
      for object in objects:
        if object.get_type() == OBJECT_CAMP:
          camp_exists = True
          break
      if not camp_exists:
        objects.append(Camp(screen))

    if no_acc:
      player.dec()

    #Background
    if refresh_bg or lose:
      original_background.render()
    else:
      background.render()

    #Objects
    enemies_left = 0
    friends_left = 0

    for object in objects:
      object.render_back()
      if object.get_type() == OBJECT_DOLLAR:
        enemies_left += 1

      if object.get_type() == OBJECT_FOLLOWER:
        friends_left += 1

    score_mod = 0

    i = 0
    for object in objects:
      # The first parameter is for the playing field, other for object collisions
      if lose:
        object.update(playingfield, objects, float(lose_delay)/LOSE_DELAY)
      else:
        object.update(playingfield, objects)
      change_to = object.get_change()
      coords = object.get_coords()

      if object.get_type() == OBJECT_BANK and object.spawn:
          objects.append(Enemy(screen, coords))

      if lose:
        if object.get_type() == OBJECT_FOLLOWER:
          object.change = OBJECT_DOLLAR
      if change_to:
        if object.get_type() == OBJECT_PLAYER:
          lose = True
          if (score < 5000):
            print "The communist balls lost."
          else:
            if (score < 10000):
              print "The communist balls lost, but didn't go down without a fight. Well done."
            else:
              print "Well, that was quite an upheaval! Not bad, number one, not bad at all."
          print "Now, press ESC."
        del objects[i]
        if change_to == OBJECT_FOLLOWER:
          score_mod += 1
          objects.append(Follower(screen, player, coords))
        if change_to == OBJECT_DOLLAR:
          if not lose:
            score_mod -= 1
          objects.append(Enemy(screen, coords))
      else:
        object.render()
        if object.dead:
          score += object.score
          del objects[i]

      i += 1

    score += score_mod * (100 + total_spawned)

    render_score(screen, score)

    if lose:
      lose_delay -= 1
      if lose_delay == 0:
        done = True

    #Generic
    if refresh_bg:
      pygame.display.flip()
    else:
      pygame.display.update(playingfield)

    #Processing time calculation
    comptime += time.clock() - t1
    t1 = time.clock()
    clock.tick(FPS)
    idletime += time.clock() - t1
    t1 = time.clock()

  print "Idle time: " + str(idletime) + "s, time spent processing: " + str(comptime) + "s"
  print "Average processor time taken: " + str(comptime/(idletime+comptime)*100) + "%"
  return score