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

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