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 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  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. pygame.mixer.Sound.play(self.o.defender_sounds[0])
  139. class Bullet:
  140. def __init__(self, o, game, x):
  141. self.o = o
  142. self.game = game
  143. self.posx = x
  144. self.posy = 110
  145. self.width = 3
  146. self.height = 10
  147. def draw(self):
  148. hw = self.width / 2
  149. hh = self.height / 2
  150. self.o.draw.rectangle(((self.posx - hw), (self.posy - hh), (self.posx + hw), (self.posy + hh)), outline=0, fill=self.o.light_grey)
  151. def update(self):
  152. self.posy -= self.game.bullet_speed * self.game.delta
  153. if self.posy < 0:
  154. self.game.bullets.remove(self)
  155. self.draw()
  156. def check_collision(self):
  157. pass
  158. class Enemy:
  159. def __init__(self, o, game):
  160. self.o = o
  161. self.game = game
  162. self.posx = game.gw / 2
  163. self.posy = -20
  164. self.width = 20
  165. self.height = 15
  166. self.speed = random.randrange(15, 30) * game.speed_mult
  167. self.xspeed = random.randrange(0, 30) * game.speed_mult
  168. self.point_value = 50
  169. self.ydir = random.choice([-1, 1])
  170. def draw(self):
  171. hw = self.width / 2
  172. hh = self.height / 2
  173. 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)
  174. 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)
  175. self.o.draw.rectangle(((self.posx - 8), (self.posy - 4), (self.posx - 2), (self.posy - 3)), outline=self.o.blue, fill=self.o.blue)
  176. self.o.draw.rectangle(((self.posx + 8), (self.posy - 4), (self.posx + 2), (self.posy - 3)), outline=self.o.blue, fill=self.o.blue)
  177. def update(self):
  178. self.posy += self.speed * self.game.delta
  179. self.posx += (self.xspeed * self.ydir * self.game.delta)
  180. self.check_collision()
  181. if self.posy > self.o.height:
  182. self.game.enemies.remove(self)
  183. pygame.mixer.Sound.play(self.o.defender_sounds[2])
  184. self.game.game_over = True
  185. if self.posx > (self.game.gw - (self.width / 2)):
  186. self.ydir = -1
  187. elif self.posx < (0 + (self.width / 2)):
  188. self.ydir = 1
  189. self.draw()
  190. def check_collision(self):
  191. for b in self.game.bullets:
  192. xdist = abs(b.posx - self.posx)
  193. if xdist < self.width / 2:
  194. ydist = abs(b.posy - self.posy)
  195. if ydist < self.height / 2:
  196. #print('hit ', xdist)
  197. self.game.score += self.point_value
  198. if b in self.game.bullets:
  199. self.game.bullets.remove(b)
  200. self.die()
  201. pygame.mixer.Sound.play(self.o.defender_sounds[1])
  202. def die(self):
  203. if self in self.game.enemies:
  204. self.game.enemies.remove(self)
  205. self.game.explodes.append(Explode(self.o, self.game, self.posx, self.posy))
  206. class Explode:
  207. def __init__(self, o, game, x, y):
  208. self.o = o
  209. self.game = game
  210. self.posx = x
  211. self.posy = y
  212. self.width = 16
  213. self.height = 14
  214. self.gap = 5
  215. self.scale_speed = 80
  216. def draw(self):
  217. hw = self.width / 2
  218. hh = self.height / 2
  219. c1 = [self.posx + self.gap, self.posy - self.gap]
  220. c2 = [self.posx - self.gap, self.posy - self.gap]
  221. c3 = [self.posx + self.gap, self.posy + self.gap]
  222. c4 = [self.posx - self.gap, self.posy + self.gap]
  223. self.o.draw.rectangle(((c1[0] - hw), (c1[1] - hh), (c1[0] + hw), (c1[1] + hh)), outline=0, fill=self.o.light_grey)
  224. self.o.draw.rectangle(((c2[0] - hw), (c2[1] - hh), (c2[0] + hw), (c2[1] + hh)), outline=0, fill=self.o.light_grey)
  225. self.o.draw.rectangle(((c3[0] - hw), (c3[1] - hh), (c3[0] + hw), (c3[1] + hh)), outline=0, fill=self.o.light_grey)
  226. self.o.draw.rectangle(((c4[0] - hw), (c4[1] - hh), (c4[0] + hw), (c4[1] + hh)), outline=0, fill=self.o.light_grey)
  227. def update(self):
  228. #self.posy -= self.game.bullet_speed * self.game.delta
  229. self.gap += self.game.explode_speed * self.game.delta
  230. self.width -= self.scale_speed * self.game.delta
  231. self.height -= self.scale_speed * self.game.delta
  232. if self.gap > 20:
  233. self.game.explodes.remove(self)
  234. print('killing explosion ', len(self.game.explodes))
  235. self.draw()
  236. def check_collision(self):
  237. pass
  238. #====================================
  239. State = type("State", (object,), {})
  240. #====================================
  241. class State(object):
  242. def __init__(self, FSM):
  243. self.FSM = FSM
  244. self.timer = 0
  245. self.startTime = 0
  246. def Enter(self):
  247. self.timer = 0
  248. self.startTime = 0
  249. def Execute(self):
  250. print('Executing')
  251. def Exit(self):
  252. print('Exiting')
  253. #====================================
  254. class Example(State):
  255. def __init__(self,FSM):
  256. super(Example, self).__init__(FSM)
  257. def Enter(self):
  258. self.FSM.stateLife = 1
  259. super(Example, self).Enter()
  260. def Execute(self):
  261. self.FSM.stateLife += 1
  262. def Exit(self):
  263. pass
  264. #====================================
  265. class Startup(State):
  266. def __init__(self,FSM):
  267. super(Startup, self).__init__(FSM)
  268. def Enter(self):
  269. self.FSM.stateLife = 1
  270. self.birth = time.perf_counter()
  271. super(Startup, self).Enter()
  272. def Execute(self):
  273. print('defender startup state')
  274. self.FSM.ToTransition('toIntro')
  275. def Exit(self):
  276. pass
  277. #====================================
  278. class Intro(State):
  279. def __init__(self,FSM):
  280. super(Intro, self).__init__(FSM)
  281. def Enter(self):
  282. self.birth = time.perf_counter()
  283. o = self.FSM.owner
  284. o.draw.rectangle((0, 0, o.width, o.height), outline=0, fill=o.blue)
  285. o.center_block("Welcome To Defender", o.h2, [0, 0, o.width, o.height], o.light_grey)
  286. o.update_display(0)
  287. super(Intro, self).Enter()
  288. def Execute(self):
  289. if (time.perf_counter() - self.birth) > 2:
  290. print('time is up')
  291. self.FSM.queued_state = "toMain"
  292. self.FSM.ToTransition('toBlueFade')
  293. def Exit(self):
  294. pass
  295. #====================================
  296. #====================================
  297. class Main(State):
  298. def __init__(self,FSM):
  299. super(Main, self).__init__(FSM)
  300. def Enter(self):
  301. print('hello defender main')
  302. o = self.FSM.owner
  303. o.draw.rectangle((0, 0, o.width, o.height), outline=0, fill=o.blue)
  304. o.update_display(0)
  305. self.game = Game(o)
  306. o.game = self.game
  307. super(Main, self).Enter()
  308. def Execute(self):
  309. self.game.update()
  310. if self.game.game_over:
  311. self.FSM.queued_state = "toGameOver"
  312. self.FSM.ToTransition('toBlueFade')
  313. def Exit(self):
  314. pass
  315. #====================================
  316. class BlueFade(State):
  317. def __init__(self,FSM):
  318. super(BlueFade, self).__init__(FSM)
  319. def Enter(self):
  320. self.FSM.stateLife = 1
  321. self.birth = pygame.time.get_ticks()
  322. self.last_frame_time = time.perf_counter()
  323. self.o = self.FSM.owner
  324. self.x = self.o.width / 2
  325. self.y = self.o.height / 2
  326. self.xsize = 0.0
  327. self.ysize = 0.0
  328. super(BlueFade, self).Enter()
  329. def Execute(self):
  330. self.FSM.stateLife += 1
  331. self.delta = time.perf_counter() - self.last_frame_time
  332. self.last_frame_time = time.perf_counter()
  333. 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)
  334. self.o.update_display(self.o)
  335. self.xsize += self.delta * 150
  336. self.ysize += self.delta * 125
  337. if (pygame.time.get_ticks() - self.birth > 1000):
  338. if self.FSM.queued_state != None:
  339. ns = self.FSM.queued_state
  340. self.FSM.queued_state = None
  341. self.FSM.ToTransition(ns)
  342. def Exit(self):
  343. pass
  344. #====================================
  345. class GreyFade(State):
  346. def __init__(self,FSM):
  347. super(GreyFade, self).__init__(FSM)
  348. def Enter(self):
  349. self.FSM.stateLife = 1
  350. self.birth = pygame.time.get_ticks()
  351. self.last_frame_time = time.perf_counter()
  352. self.o = self.FSM.owner
  353. self.x = self.o.width / 2
  354. self.y = self.o.height / 2
  355. self.xsize = 0.0
  356. self.ysize = 0.0
  357. super(GreyFade, self).Enter()
  358. def Execute(self):
  359. self.FSM.stateLife += 1
  360. self.delta = time.perf_counter() - self.last_frame_time
  361. self.last_frame_time = time.perf_counter()
  362. 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)
  363. self.o.update_display(self.o)
  364. self.xsize += self.delta * 150
  365. self.ysize += self.delta * 125
  366. if (pygame.time.get_ticks() - self.birth > 1000):
  367. print('get outta fade')
  368. if self.FSM.queued_state != None:
  369. ns = self.FSM.queued_state
  370. self.FSM.queued_state = "toMain"
  371. self.FSM.ToTransition(ns)
  372. def Exit(self):
  373. pass
  374. #====================================
  375. class GameOver(State):
  376. def __init__(self,FSM):
  377. super(GameOver, self).__init__(FSM)
  378. def Enter(self):
  379. self.FSM.stateLife = 1
  380. self.o = self.FSM.owner
  381. self.birth = pygame.time.get_ticks()
  382. self.o.draw.rectangle((0, 0, self.o.width, self.o.height), outline=0, fill=self.o.blue)
  383. self.o.center_block("Game Over", self.o.h2, [0, 0, self.o.width, self.o.height], self.o.light_grey)
  384. 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)
  385. self.o.update_display(0)
  386. super(GameOver, self).Enter()
  387. def Execute(self):
  388. if self.o.keyState[0] == 1:
  389. self.FSM.queued_state = "toMain"
  390. self.FSM.ToTransition('toBlueFade')
  391. if (pygame.time.get_ticks() - self.birth) > 3000:
  392. if self.o.game.score > self.o.game.hs_s1:
  393. print('this is a new high score')
  394. self.FSM.queued_state = "toNewHighScore"
  395. else:
  396. print('not a new high score')
  397. self.FSM.queued_state = "toHighScore"
  398. self.FSM.ToTransition('toBlueFade')
  399. def Exit(self):
  400. pass
  401. #====================================
  402. class HighScore(State):
  403. def __init__(self,FSM):
  404. super(HighScore, self).__init__(FSM)
  405. def Enter(self):
  406. self.o = self.FSM.owner
  407. self.o.draw.rectangle((0, 0, self.o.width, self.o.height), outline=0, fill=self.o.blue)
  408. self.o.center_text("HIGH SCORES", self.o.h1, self.o.width, 25, self.o.light_grey)
  409. self.o.center_text("----------------------", self.o.h2, self.o.width, 55, self.o.light_grey)
  410. 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)
  411. 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)
  412. 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)
  413. self.o.center_block("restart", self.o.h2, [0, 50, self.o.width, self.o.height + 50], self.o.light_grey)
  414. self.o.update_display(0)
  415. super(HighScore, self).Enter()
  416. def Execute(self):
  417. if self.o.keyState[0] == 1 or self.o.keyState[1] == 1 or self.o.keyState[2] == 1 or self.o.keyState[3] == 1:
  418. self.FSM.queued_state = "toMain"
  419. self.FSM.ToTransition('toBlueFade')
  420. def Exit(self):
  421. pass
  422. #====================================
  423. class NewHighScore(State):
  424. def __init__(self,FSM):
  425. super(NewHighScore, self).__init__(FSM)
  426. def Enter(self):
  427. self.birth = time.perf_counter()
  428. o = self.FSM.owner
  429. o.draw.rectangle((0, 0, o.width, o.height), outline=0, fill=o.blue)
  430. o.center_block("NEW HIGH SCORE!", o.h2, [0, 0, o.width, o.height], o.light_grey)
  431. o.update_display(0)
  432. super(NewHighScore, self).Enter()
  433. def Execute(self):
  434. if (time.perf_counter() - self.birth) > 2:
  435. self.FSM.queued_state = "toEnterText"
  436. self.FSM.ToTransition('toBlueFade')
  437. def Exit(self):
  438. pass
  439. #====================================
  440. class EnterText(State):
  441. def __init__(self,FSM):
  442. super(EnterText, self).__init__(FSM)
  443. def Enter(self):
  444. o = self.FSM.owner
  445. self.input_string = ""
  446. self.last_action = pygame.time.get_ticks()
  447. self.caps = False
  448. self.cur_but = 18
  449. self.cur_letter = ""
  450. self.skips = [7, 3, 11, 12, 13, 14, 15]
  451. self.lc_labels = [["p", "q", "r", "s"], ["t", "u", "v"], ["w", "x", "y", "z"], ["C", "A", "P", "S"],
  452. ["g", "h", "i"], ["j", "k", "l"], ["m", "n", "o"], ["D", "E", "L"],
  453. ["_", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-"], ["a", "b", "c"], ["d", "e", "f"], ["O", "K"],
  454. [], [], [], []]
  455. self.uc_labels = [["P", "Q", "R", "S"], ["T", "U", "V"], ["W", "X", "Y", "Z"], ["C", "A", "P", "S"],
  456. ["G", "H", "I"], ["J", "K", "L"], ["M", "N", "O"], ["D", "E", "L"],
  457. ["_", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-"], ["A", "B", "C"], ["D", "E", "F"], ["O", "K"],
  458. [], [], [], []]
  459. self.labels = self.lc_labels
  460. self.cycles = []
  461. for l in self.labels:
  462. self.cycles.append(cycle(l))
  463. self.draw(o)
  464. o.update_display(0)
  465. super(EnterText, self).Enter()
  466. def Execute(self):
  467. o = self.FSM.owner
  468. y_size = o.height / 4
  469. if o.keyState[11] == 1:
  470. if self.input_string != 'blank':
  471. o.game.hs_name = self.input_string
  472. if o.game.score > o.game.hs_s3:
  473. print('new first')
  474. o.game.hs_s1 = o.game.hs_s2
  475. o.game.hs_n1 = o.game.hs_n2
  476. o.game.hs_s2 = o.game.hs_s3
  477. o.game.hs_n2 = o.game.hs_n3
  478. o.game.hs_s3 = o.game.score
  479. o.game.hs_n3 = o.game.hs_name
  480. elif o.game.score > o.game.hs_s2:
  481. print('new second')
  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.score
  485. o.game.hs_n2 = o.game.hs_name
  486. else:
  487. print('new third')
  488. o.game.hs_s1 = o.game.score
  489. o.game.hs_n1 = o.game.hs_name
  490. print('new high score saving')
  491. self.FSM.queued_state = 'toHighScore'
  492. self.FSM.ToTransition('toBlueFade')
  493. o.game.save_highscore()
  494. if o.keyState[16] > 0 and o.keyState[17] > 0:
  495. pass
  496. else:
  497. _iter = 0
  498. for k in o.keyState:
  499. if k == 1:
  500. cur_time = pygame.time.get_ticks()
  501. if _iter not in self.skips:
  502. self.last_action = cur_time
  503. self.cur_letter = next(self.cycles[_iter])
  504. self.cur_but = _iter
  505. self.draw(o)
  506. o.center_block(self.input_string + self.cur_letter, o.h2, [0, 0, o.width, y_size], o.light_grey)
  507. if _iter == 7:
  508. self.input_string = self.input_string[:-1]
  509. print('deleting')
  510. self.last_action = 100000000
  511. self.draw(o)
  512. o.center_block(self.input_string, o.h2, [0, 0, o.width, y_size], o.light_grey)
  513. elif _iter == 3:
  514. print('caps')
  515. if self.caps:
  516. self.caps = False
  517. self.labels = self.lc_labels
  518. else:
  519. self.caps = True
  520. self.labels = self.uc_labels
  521. self.draw(o)
  522. o.center_block(self.input_string, o.h2, [0, 0, o.width, y_size], o.light_grey)
  523. self.cycles = []
  524. for l in self.labels:
  525. self.cycles.append(cycle(l))
  526. o.update_display(0)
  527. _iter += 1
  528. cur_time = pygame.time.get_ticks()
  529. delay = cur_time - self.last_action
  530. if delay > 1000 and delay < 100000:
  531. self.input_string += self.cur_letter
  532. self.last_action = 100000000
  533. self.reset_cycles()
  534. self.draw(o)
  535. o.center_block(self.input_string, o.h2, [0, 0, o.width, y_size], o.light_grey)
  536. o.update_display(0)
  537. def draw(self, o):
  538. x_size = o.width / 4
  539. y_size = o.height / 4
  540. og_x = 0
  541. o_x = og_x
  542. o_y = o.height
  543. text_padding = 6
  544. _id = 0
  545. while _id < 16:
  546. lab = ""
  547. for i in self.labels[_id]:
  548. lab += i
  549. if _id == 8:
  550. lab = "_12"
  551. o.draw.rectangle((o_x, o_y, o_x + x_size, o_y - y_size), outline=0, fill=o.blue)
  552. o.center_block(lab, o.h2, [o_x, o_y, o_x + x_size, o_y - y_size], o.light_grey)
  553. o_x = o_x + x_size
  554. _id += 1
  555. if _id % 4 == 0:
  556. o_y -= y_size
  557. o_x = og_x
  558. o.draw.rectangle((0, 0, o.width, y_size), outline=0, fill=o.blue)
  559. def reset_cycles(self):
  560. self.cycles = []
  561. for l in self.labels:
  562. self.cycles.append(cycle(l))
  563. def Exit(self):
  564. pass
  565. #====================================
  566. def timer_event():
  567. pass