raspberry pi zero based drum machine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

StatesDefender.py 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. import random
  2. import time
  3. import pygame
  4. import sys
  5. import pyaudio
  6. import wave
  7. import glob
  8. import os
  9. import ast
  10. from configobj import ConfigObj
  11. from itertools import cycle
  12. class Game:
  13. def __init__(self, o):
  14. self.o = o
  15. self.last_frame_time = time.perf_counter()
  16. self.bullets = []
  17. self.bullet_speed = 80
  18. self.delta = 0.00001
  19. self.sidebar_width = 80
  20. self.gw = o.width - self.sidebar_width
  21. self.score = 0
  22. self.time = 0
  23. self.player = Player(o, self)
  24. self.enemies = []
  25. self.birth = pygame.time.get_ticks()
  26. self.last_enemy_spawn = pygame.time.get_ticks()
  27. self.enemy_spawn_delay = 2000
  28. self.explodes = []
  29. self.explode_speed = 80
  30. self.hs_name = ""
  31. self.speed_mult = 1.0
  32. self.speed_mult_inc = .1
  33. self.mult_state = 3
  34. self.load_highscore()
  35. self.game_over = False
  36. def update(self):
  37. self.delta = time.perf_counter() - self.last_frame_time
  38. self.last_frame_time = time.perf_counter()
  39. self.o.draw.rectangle((0, 0, self.o.width, self.o.height), outline=0, fill=self.o.blue)
  40. self.mstime = (pygame.time.get_ticks() - self.birth)
  41. self.time = int(self.mstime * .001)
  42. if self.time > self.mult_state:
  43. self.mult_state += 3
  44. if self.speed_mult < 2.5:
  45. self.speed_mult += self.speed_mult_inc
  46. if self.enemy_spawn_delay > 1000:
  47. self.enemy_spawn_delay -= 50
  48. self.enemy_spawner()
  49. self.draw_bullets()
  50. self.draw_enemies()
  51. self.draw_explodes()
  52. self.player.update(self.delta)
  53. self.draw_sidebar()
  54. self.o.update_display(self.o)
  55. def draw_bullets(self):
  56. for x in self.bullets:
  57. x.check_collision()
  58. x.update()
  59. x.draw()
  60. def draw_enemies(self):
  61. for x in self.enemies:
  62. x.check_collision()
  63. x.update()
  64. x.draw()
  65. def draw_explodes(self):
  66. for x in self.explodes:
  67. x.update()
  68. x.draw()
  69. def draw_sidebar(self):
  70. w = self.o.width - self.sidebar_width
  71. self.o.draw.rectangle((w, 0, self.o.width, self.o.height), outline=0, fill=self.o.light_grey)
  72. inc = 40
  73. h = 10
  74. self.o.center_block("Score", self.o.h2, [w, h, self.o.width, h + 10], self.o.blue)
  75. h += inc
  76. self.o.center_block(str(self.score), self.o.h2, [w, 15, self.o.width, h + 10], self.o.blue)
  77. h += (inc * 2)
  78. self.o.center_block("Time", self.o.h2, [w, 30, self.o.width, h + 10], self.o.blue)
  79. h += inc
  80. self.o.center_block(str(self.time), self.o.h2, [w, 40, self.o.width, h + 10], self.o.blue)
  81. def enemy_spawner(self):
  82. sl = pygame.time.get_ticks() - self.last_enemy_spawn
  83. if len(self.enemies) < 1 or sl > self.enemy_spawn_delay:
  84. new_e = Enemy(self.o, self)
  85. self.enemies.append(new_e)
  86. self.last_enemy_spawn = pygame.time.get_ticks()
  87. def save_highscore(self):
  88. base_dir = "/home/pi/zpc_ct/"
  89. title = "defender_scores"
  90. fname = base_dir + title + '.sco'
  91. if os.path.exists(fname):
  92. os.utime(fname, None)
  93. else:
  94. open(fname, 'a').close()
  95. self.sconf = ConfigObj(fname)
  96. self.sconf['name1'] = self.hs_n1
  97. self.sconf['name2'] = self.hs_n2
  98. self.sconf['name3'] = self.hs_n3
  99. self.sconf['score1'] = self.hs_s1
  100. self.sconf['score2'] = self.hs_s2
  101. self.sconf['score3'] = self.hs_s3
  102. self.sconf.write()
  103. def load_highscore(self):
  104. base_dir = "/home/pi/zpc_ct/"
  105. self.sconf = ConfigObj(base_dir + 'defender_scores.sco')
  106. self.hs_n1 = self.sconf['name1']
  107. self.hs_n2 = self.sconf['name2']
  108. self.hs_n3 = self.sconf['name3']
  109. self.hs_s1 = self.sconf.as_int('score1')
  110. self.hs_s2 = self.sconf.as_int('score2')
  111. self.hs_s3 = self.sconf.as_int('score3')
  112. class Player:
  113. def __init__(self, o, game):
  114. self.o = o
  115. self.game = game
  116. self.width = 28
  117. self.height = 12
  118. self.speed = 80
  119. self.posx = game.gw / 2
  120. self.posy = o.height - self.height
  121. def update(self, delta):
  122. self.check_keys(delta)
  123. self.draw_player()
  124. def draw_player(self):
  125. hw = self.width / 2
  126. hh = self.height / 2
  127. self.o.draw.rectangle(((self.posx - hw), (self.posy - hh), (self.posx + hw), (self.posy + hh)), outline=self.o.light_grey, fill=self.o.light_grey)
  128. self.o.draw.rectangle(((self.posx - (hw/4)), (self.posy - self.height), (self.posx + (hw/4)), (self.posy + hh)), outline=self.o.light_grey, fill=self.o.light_grey)
  129. def check_keys(self, delta):
  130. if self.o.keyState[0] == 2:
  131. if self.posx > 0:
  132. self.posx -= self.speed * delta
  133. elif self.o.keyState[3] == 2:
  134. if self.posx < self.o.width:
  135. self.posx += self.speed * delta
  136. elif self.o.keyState[1] == 1 or self.o.keyState[2] == 1:
  137. self.game.bullets.append(Bullet(self.o, self.game, self.posx))
  138. self.o.defender_sounds[0].set_volume(self.o.volume / 16)
  139. pygame.mixer.Sound.play(self.o.defender_sounds[0])
  140. # if self.file != None:
  141. # if vol != 0:
  142. # vol = (vol / 16) * (self.volume / 16) * (self.o.volume / 16)
  143. # self.mixerSound.set_volume(vol)
  144. # pygame.mixer.Sound.play(self.mixerSound)
  145. class Bullet:
  146. def __init__(self, o, game, x):
  147. self.o = o
  148. self.game = game
  149. self.posx = x
  150. self.posy = 110
  151. self.width = 3
  152. self.height = 10
  153. def draw(self):
  154. hw = self.width / 2
  155. hh = self.height / 2
  156. self.o.draw.rectangle(((self.posx - hw), (self.posy - hh), (self.posx + hw), (self.posy + hh)), outline=0, fill=self.o.light_grey)
  157. def update(self):
  158. self.posy -= self.game.bullet_speed * self.game.delta
  159. if self.posy < 0:
  160. self.game.bullets.remove(self)
  161. self.draw()
  162. def check_collision(self):
  163. pass
  164. class Enemy:
  165. def __init__(self, o, game):
  166. self.o = o
  167. self.game = game
  168. self.posx = game.gw / 2
  169. self.posy = -20
  170. self.width = 20
  171. self.height = 15
  172. self.speed = random.randrange(15, 30) * game.speed_mult
  173. self.xspeed = random.randrange(0, 30) * game.speed_mult
  174. self.point_value = 50
  175. self.ydir = random.choice([-1, 1])
  176. def draw(self):
  177. hw = self.width / 2
  178. hh = self.height / 2
  179. self.o.draw.rectangle(((self.posx - hw), (self.posy - hh), (self.posx + hw), (self.posy + hh)), outline=self.o.light_grey, fill=self.o.light_grey)
  180. self.o.draw.rectangle(((self.posx - hw + 5), (self.posy - hh + 10), (self.posx + hw - 5), (self.posy + hh)), outline=self.o.blue, fill=self.o.blue)
  181. self.o.draw.rectangle(((self.posx - 8), (self.posy - 4), (self.posx - 2), (self.posy - 3)), outline=self.o.blue, fill=self.o.blue)
  182. self.o.draw.rectangle(((self.posx + 8), (self.posy - 4), (self.posx + 2), (self.posy - 3)), outline=self.o.blue, fill=self.o.blue)
  183. def update(self):
  184. self.posy += self.speed * self.game.delta
  185. self.posx += (self.xspeed * self.ydir * self.game.delta)
  186. self.check_collision()
  187. if self.posy > self.o.height:
  188. self.game.enemies.remove(self)
  189. self.o.defender_sounds[2].set_volume(self.o.volume / 16)
  190. pygame.mixer.Sound.play(self.o.defender_sounds[2])
  191. self.game.game_over = True
  192. if self.posx > (self.game.gw - (self.width / 2)):
  193. self.ydir = -1
  194. elif self.posx < (0 + (self.width / 2)):
  195. self.ydir = 1
  196. self.draw()
  197. def check_collision(self):
  198. for b in self.game.bullets:
  199. xdist = abs(b.posx - self.posx)
  200. if xdist < self.width / 2:
  201. ydist = abs(b.posy - self.posy)
  202. if ydist < self.height / 2:
  203. #print('hit ', xdist)
  204. self.game.score += self.point_value
  205. if b in self.game.bullets:
  206. self.game.bullets.remove(b)
  207. self.die()
  208. self.o.defender_sounds[1].set_volume(self.o.volume / 16)
  209. pygame.mixer.Sound.play(self.o.defender_sounds[1])
  210. def die(self):
  211. if self in self.game.enemies:
  212. self.game.enemies.remove(self)
  213. self.game.explodes.append(Explode(self.o, self.game, self.posx, self.posy))
  214. class Explode:
  215. def __init__(self, o, game, x, y):
  216. self.o = o
  217. self.game = game
  218. self.posx = x
  219. self.posy = y
  220. self.width = 16
  221. self.height = 14
  222. self.gap = 5
  223. self.scale_speed = 80
  224. def draw(self):
  225. hw = self.width / 2
  226. hh = self.height / 2
  227. c1 = [self.posx + self.gap, self.posy - self.gap]
  228. c2 = [self.posx - self.gap, self.posy - self.gap]
  229. c3 = [self.posx + self.gap, self.posy + self.gap]
  230. c4 = [self.posx - self.gap, self.posy + self.gap]
  231. self.o.draw.rectangle(((c1[0] - hw), (c1[1] - hh), (c1[0] + hw), (c1[1] + hh)), outline=0, fill=self.o.light_grey)
  232. self.o.draw.rectangle(((c2[0] - hw), (c2[1] - hh), (c2[0] + hw), (c2[1] + hh)), outline=0, fill=self.o.light_grey)
  233. self.o.draw.rectangle(((c3[0] - hw), (c3[1] - hh), (c3[0] + hw), (c3[1] + hh)), outline=0, fill=self.o.light_grey)
  234. self.o.draw.rectangle(((c4[0] - hw), (c4[1] - hh), (c4[0] + hw), (c4[1] + hh)), outline=0, fill=self.o.light_grey)
  235. def update(self):
  236. #self.posy -= self.game.bullet_speed * self.game.delta
  237. self.gap += self.game.explode_speed * self.game.delta
  238. self.width -= self.scale_speed * self.game.delta
  239. self.height -= self.scale_speed * self.game.delta
  240. if self.gap > 20:
  241. self.game.explodes.remove(self)
  242. print('killing explosion ', len(self.game.explodes))
  243. self.draw()
  244. def check_collision(self):
  245. pass
  246. #====================================
  247. State = type("State", (object,), {})
  248. #====================================
  249. class State(object):
  250. def __init__(self, FSM):
  251. self.FSM = FSM
  252. self.timer = 0
  253. self.startTime = 0
  254. def Enter(self):
  255. self.timer = 0
  256. self.startTime = 0
  257. def Execute(self):
  258. print('Executing')
  259. def Exit(self):
  260. print('Exiting')
  261. #====================================
  262. class Example(State):
  263. def __init__(self,FSM):
  264. super(Example, self).__init__(FSM)
  265. def Enter(self):
  266. self.FSM.stateLife = 1
  267. super(Example, self).Enter()
  268. def Execute(self):
  269. self.FSM.stateLife += 1
  270. def Exit(self):
  271. pass
  272. #====================================
  273. class Startup(State):
  274. def __init__(self,FSM):
  275. super(Startup, self).__init__(FSM)
  276. def Enter(self):
  277. self.FSM.stateLife = 1
  278. self.birth = time.perf_counter()
  279. super(Startup, self).Enter()
  280. def Execute(self):
  281. print('defender startup state')
  282. self.FSM.ToTransition('toIntro')
  283. def Exit(self):
  284. pass
  285. #====================================
  286. class Intro(State):
  287. def __init__(self,FSM):
  288. super(Intro, self).__init__(FSM)
  289. def Enter(self):
  290. self.birth = time.perf_counter()
  291. o = self.FSM.owner
  292. o.draw.rectangle((0, 0, o.width, o.height), outline=0, fill=o.blue)
  293. o.center_block("Welcome To Defender", o.h2, [0, 0, o.width, o.height], o.light_grey)
  294. o.update_display(0)
  295. super(Intro, self).Enter()
  296. def Execute(self):
  297. if (time.perf_counter() - self.birth) > 2:
  298. print('time is up')
  299. self.FSM.queued_state = "toMain"
  300. self.FSM.ToTransition('toBlueFade')
  301. def Exit(self):
  302. pass
  303. #====================================
  304. #====================================
  305. class Main(State):
  306. def __init__(self,FSM):
  307. super(Main, self).__init__(FSM)
  308. def Enter(self):
  309. print('hello defender main')
  310. o = self.FSM.owner
  311. o.draw.rectangle((0, 0, o.width, o.height), outline=0, fill=o.blue)
  312. o.update_display(0)
  313. self.game = Game(o)
  314. o.game = self.game
  315. super(Main, self).Enter()
  316. def Execute(self):
  317. self.game.update()
  318. if self.game.game_over:
  319. self.FSM.queued_state = "toGameOver"
  320. self.FSM.ToTransition('toBlueFade')
  321. def Exit(self):
  322. pass
  323. #====================================
  324. class BlueFade(State):
  325. def __init__(self,FSM):
  326. super(BlueFade, self).__init__(FSM)
  327. def Enter(self):
  328. self.FSM.stateLife = 1
  329. self.birth = pygame.time.get_ticks()
  330. self.last_frame_time = time.perf_counter()
  331. self.o = self.FSM.owner
  332. self.x = self.o.width / 2
  333. self.y = self.o.height / 2
  334. self.xsize = 0.0
  335. self.ysize = 0.0
  336. super(BlueFade, self).Enter()
  337. def Execute(self):
  338. self.FSM.stateLife += 1
  339. self.delta = time.perf_counter() - self.last_frame_time
  340. self.last_frame_time = time.perf_counter()
  341. self.o.draw.rectangle((self.x - self.xsize, self.y - self.ysize, self.x + self.xsize, self.y + self.ysize), outline=self.o.blue, fill=self.o.blue)
  342. self.o.update_display(self.o)
  343. self.xsize += self.delta * 150
  344. self.ysize += self.delta * 125
  345. if (pygame.time.get_ticks() - self.birth > 1000):
  346. if self.FSM.queued_state != None:
  347. ns = self.FSM.queued_state
  348. self.FSM.queued_state = None
  349. self.FSM.ToTransition(ns)
  350. def Exit(self):
  351. pass
  352. #====================================
  353. class GreyFade(State):
  354. def __init__(self,FSM):
  355. super(GreyFade, self).__init__(FSM)
  356. def Enter(self):
  357. self.FSM.stateLife = 1
  358. self.birth = pygame.time.get_ticks()
  359. self.last_frame_time = time.perf_counter()
  360. self.o = self.FSM.owner
  361. self.x = self.o.width / 2
  362. self.y = self.o.height / 2
  363. self.xsize = 0.0
  364. self.ysize = 0.0
  365. super(GreyFade, self).Enter()
  366. def Execute(self):
  367. self.FSM.stateLife += 1
  368. self.delta = time.perf_counter() - self.last_frame_time
  369. self.last_frame_time = time.perf_counter()
  370. self.o.draw.rectangle((self.x - self.xsize, self.y - self.ysize, self.x + self.xsize, self.y + self.ysize), outline=self.o.light_grey, fill=self.o.light_grey)
  371. self.o.update_display(self.o)
  372. self.xsize += self.delta * 150
  373. self.ysize += self.delta * 125
  374. if (pygame.time.get_ticks() - self.birth > 1000):
  375. print('get outta fade')
  376. if self.FSM.queued_state != None:
  377. ns = self.FSM.queued_state
  378. self.FSM.queued_state = "toMain"
  379. self.FSM.ToTransition(ns)
  380. def Exit(self):
  381. pass
  382. #====================================
  383. class GameOver(State):
  384. def __init__(self,FSM):
  385. super(GameOver, self).__init__(FSM)
  386. def Enter(self):
  387. self.FSM.stateLife = 1
  388. self.o = self.FSM.owner
  389. self.birth = pygame.time.get_ticks()
  390. self.o.draw.rectangle((0, 0, self.o.width, self.o.height), outline=0, fill=self.o.blue)
  391. self.o.center_block("Game Over", self.o.h2, [0, 0, self.o.width, self.o.height], self.o.light_grey)
  392. self.o.center_block("Score: " + str(self.o.game.score), self.o.h2, [0, 20, self.o.width, self.o.height + 20], self.o.light_grey)
  393. self.o.update_display(0)
  394. super(GameOver, self).Enter()
  395. def Execute(self):
  396. if self.o.keyState[0] == 1:
  397. self.FSM.queued_state = "toMain"
  398. self.FSM.ToTransition('toBlueFade')
  399. if (pygame.time.get_ticks() - self.birth) > 3000:
  400. if self.o.game.score > self.o.game.hs_s1:
  401. print('this is a new high score')
  402. self.FSM.queued_state = "toNewHighScore"
  403. else:
  404. print('not a new high score')
  405. self.FSM.queued_state = "toHighScore"
  406. self.FSM.ToTransition('toBlueFade')
  407. def Exit(self):
  408. pass
  409. #====================================
  410. class HighScore(State):
  411. def __init__(self,FSM):
  412. super(HighScore, self).__init__(FSM)
  413. def Enter(self):
  414. self.o = self.FSM.owner
  415. self.o.draw.rectangle((0, 0, self.o.width, self.o.height), outline=0, fill=self.o.blue)
  416. self.o.center_text("HIGH SCORES", self.o.h1, self.o.width, 25, self.o.light_grey)
  417. self.o.center_text("----------------------", self.o.h2, self.o.width, 55, self.o.light_grey)
  418. self.o.center_block(self.o.game.hs_n3 + ': ' + str(self.o.game.hs_s3), self.o.h2, [0, -20, self.o.width, self.o.height-20], self.o.light_grey)
  419. self.o.center_block(self.o.game.hs_n2 + ': ' + str(self.o.game.hs_s2), self.o.h2, [0, 0, self.o.width, self.o.height], self.o.light_grey)
  420. self.o.center_block(self.o.game.hs_n1 + ': ' + str(self.o.game.hs_s1), self.o.h2, [0, 20, self.o.width, self.o.height+20], self.o.light_grey)
  421. self.o.center_block("restart", self.o.h2, [0, 50, self.o.width, self.o.height + 50], self.o.light_grey)
  422. self.o.update_display(0)
  423. super(HighScore, self).Enter()
  424. def Execute(self):
  425. if self.o.keyState[0] == 1 or self.o.keyState[1] == 1 or self.o.keyState[2] == 1 or self.o.keyState[3] == 1:
  426. self.FSM.queued_state = "toMain"
  427. self.FSM.ToTransition('toBlueFade')
  428. def Exit(self):
  429. pass
  430. #====================================
  431. class NewHighScore(State):
  432. def __init__(self,FSM):
  433. super(NewHighScore, self).__init__(FSM)
  434. def Enter(self):
  435. self.birth = time.perf_counter()
  436. o = self.FSM.owner
  437. o.draw.rectangle((0, 0, o.width, o.height), outline=0, fill=o.blue)
  438. o.center_block("NEW HIGH SCORE!", o.h2, [0, 0, o.width, o.height], o.light_grey)
  439. o.update_display(0)
  440. super(NewHighScore, self).Enter()
  441. def Execute(self):
  442. if (time.perf_counter() - self.birth) > 2:
  443. self.FSM.queued_state = "toEnterText"
  444. self.FSM.ToTransition('toBlueFade')
  445. def Exit(self):
  446. pass
  447. #====================================
  448. class EnterText(State):
  449. def __init__(self,FSM):
  450. super(EnterText, self).__init__(FSM)
  451. def Enter(self):
  452. o = self.FSM.owner
  453. self.input_string = ""
  454. self.last_action = pygame.time.get_ticks()
  455. self.caps = False
  456. self.cur_but = 18
  457. self.cur_letter = ""
  458. self.skips = [7, 3, 11, 12, 13, 14, 15]
  459. self.lc_labels = [["p", "q", "r", "s"], ["t", "u", "v"], ["w", "x", "y", "z"], ["C", "A", "P", "S"],
  460. ["g", "h", "i"], ["j", "k", "l"], ["m", "n", "o"], ["D", "E", "L"],
  461. ["_", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-"], ["a", "b", "c"], ["d", "e", "f"], ["O", "K"],
  462. [], [], [], []]
  463. self.uc_labels = [["P", "Q", "R", "S"], ["T", "U", "V"], ["W", "X", "Y", "Z"], ["C", "A", "P", "S"],
  464. ["G", "H", "I"], ["J", "K", "L"], ["M", "N", "O"], ["D", "E", "L"],
  465. ["_", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-"], ["A", "B", "C"], ["D", "E", "F"], ["O", "K"],
  466. [], [], [], []]
  467. self.labels = self.lc_labels
  468. self.cycles = []
  469. for l in self.labels:
  470. self.cycles.append(cycle(l))
  471. self.draw(o)
  472. o.update_display(0)
  473. super(EnterText, self).Enter()
  474. def Execute(self):
  475. o = self.FSM.owner
  476. y_size = o.height / 4
  477. if o.keyState[11] == 1:
  478. if self.input_string != 'blank':
  479. o.game.hs_name = self.input_string
  480. if o.game.score > o.game.hs_s3:
  481. print('new first')
  482. o.game.hs_s1 = o.game.hs_s2
  483. o.game.hs_n1 = o.game.hs_n2
  484. o.game.hs_s2 = o.game.hs_s3
  485. o.game.hs_n2 = o.game.hs_n3
  486. o.game.hs_s3 = o.game.score
  487. o.game.hs_n3 = o.game.hs_name
  488. elif o.game.score > o.game.hs_s2:
  489. print('new second')
  490. o.game.hs_s1 = o.game.hs_s2
  491. o.game.hs_n1 = o.game.hs_n2
  492. o.game.hs_s2 = o.game.score
  493. o.game.hs_n2 = o.game.hs_name
  494. else:
  495. print('new third')
  496. o.game.hs_s1 = o.game.score
  497. o.game.hs_n1 = o.game.hs_name
  498. print('new high score saving')
  499. self.FSM.queued_state = 'toHighScore'
  500. self.FSM.ToTransition('toBlueFade')
  501. o.game.save_highscore()
  502. if o.keyState[16] > 0 and o.keyState[17] > 0:
  503. pass
  504. else:
  505. _iter = 0
  506. for k in o.keyState:
  507. if k == 1:
  508. cur_time = pygame.time.get_ticks()
  509. if _iter not in self.skips:
  510. self.last_action = cur_time
  511. self.cur_letter = next(self.cycles[_iter])
  512. self.cur_but = _iter
  513. self.draw(o)
  514. o.center_block(self.input_string + self.cur_letter, o.h2, [0, 0, o.width, y_size], o.light_grey)
  515. if _iter == 7:
  516. self.input_string = self.input_string[:-1]
  517. print('deleting')
  518. self.last_action = 100000000
  519. self.draw(o)
  520. o.center_block(self.input_string, o.h2, [0, 0, o.width, y_size], o.light_grey)
  521. elif _iter == 3:
  522. print('caps')
  523. if self.caps:
  524. self.caps = False
  525. self.labels = self.lc_labels
  526. else:
  527. self.caps = True
  528. self.labels = self.uc_labels
  529. self.draw(o)
  530. o.center_block(self.input_string, o.h2, [0, 0, o.width, y_size], o.light_grey)
  531. self.cycles = []
  532. for l in self.labels:
  533. self.cycles.append(cycle(l))
  534. o.update_display(0)
  535. _iter += 1
  536. cur_time = pygame.time.get_ticks()
  537. delay = cur_time - self.last_action
  538. if delay > 1000 and delay < 100000:
  539. self.input_string += self.cur_letter
  540. self.last_action = 100000000
  541. self.reset_cycles()
  542. self.draw(o)
  543. o.center_block(self.input_string, o.h2, [0, 0, o.width, y_size], o.light_grey)
  544. o.update_display(0)
  545. def draw(self, o):
  546. x_size = o.width / 4
  547. y_size = o.height / 4
  548. og_x = 0
  549. o_x = og_x
  550. o_y = o.height
  551. text_padding = 6
  552. _id = 0
  553. while _id < 16:
  554. lab = ""
  555. for i in self.labels[_id]:
  556. lab += i
  557. if _id == 8:
  558. lab = "_12"
  559. o.draw.rectangle((o_x, o_y, o_x + x_size, o_y - y_size), outline=0, fill=o.blue)
  560. o.center_block(lab, o.h2, [o_x, o_y, o_x + x_size, o_y - y_size], o.light_grey)
  561. o_x = o_x + x_size
  562. _id += 1
  563. if _id % 4 == 0:
  564. o_y -= y_size
  565. o_x = og_x
  566. o.draw.rectangle((0, 0, o.width, y_size), outline=0, fill=o.blue)
  567. def reset_cycles(self):
  568. self.cycles = []
  569. for l in self.labels:
  570. self.cycles.append(cycle(l))
  571. def Exit(self):
  572. pass
  573. #====================================
  574. def timer_event():
  575. pass