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

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