fork download
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import curses
  5. import locale
  6. import random
  7. import time
  8. import copy
  9.  
  10. width, height, offx, offy = 20, 24, 18, 0
  11. colors = ('CYAN', 'BLUE', 'WHITE', # foreground, background, wall
  12. 'RED', 'GREEN', 'RED', 'GREEN', 'MAGENTA', 'YELLOW', 'CYAN') # tetris
  13. blocklist = (
  14. ((1, 1), ('** ', ' **')), ((1, 1), (' **', '** ')),
  15. ((1, 2), (' *', '***')), ((0, 2), ('***', ' *')),
  16. ((0, 2), ('****')), ((1, 1), (' * ', '***')), ((1, 1), ('**', '**')))
  17. blocks = []
  18. scrmap = [[0 for i in xrange(width)] for j in xrange(height)]
  19.  
  20. def makecoords(bl):
  21. r = []
  22. for k, (o, b) in enumerate(bl):
  23. if not isinstance(b, tuple): b = (b, )
  24. p = []
  25. for j, s in enumerate(b):
  26. for i, c in enumerate(s):
  27. if c == '*': p.append([j - o[0], i - o[1]])
  28. r.append(p)
  29. return r
  30.  
  31. def showblock(y, x, b, n):
  32. for p in b: scrmap[y + p[0]][x + p[1]] = 2 + n
  33.  
  34. def hideblock(y, x, b):
  35. showblock(y, x, b, -2)
  36.  
  37. def checkspace(y, x, b):
  38. for p in b:
  39. ny, nx = y + p[0], x + p[1]
  40. if ny < 0 or ny >= height - 1 or nx <= 0 or nx >= width - 1: return False
  41. if scrmap[ny][nx]: return False
  42. return True
  43.  
  44. def rotateblock(y, x, b, n, r):
  45. if n == len(blocks) - 1: return
  46. hideblock(y, x, b)
  47. for i in xrange(len(b)): b[i] = [r * b[i][1], -r * b[i][0]]
  48. if not checkspace(y, x, b):
  49. for i in xrange(len(b)): b[i] = [-r * b[i][1], r * b[i][0]]
  50. showblock(y, x, b, n)
  51.  
  52. def moveblock(y, x, b, n, dy, dx):
  53. hideblock(y, x, b)
  54. ny, nx = y + dy, x + dx
  55. if not checkspace(ny, nx, b):
  56. showblock(y, x, b, n)
  57. return y, x
  58. showblock(ny, nx, b, n)
  59. return ny, nx
  60.  
  61. def display(v):
  62. for j in xrange(height):
  63. for i in xrange(width):
  64. v.addstr(j, i * 2, ' ', curses.color_pair(scrmap[j][i] + 1))
  65. t = time.time()
  66. v.addstr(0, 8,
  67. '%s.%03d' % (time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(t)),
  68. int(t * 1000) % 1000),
  69. curses.color_pair(1))
  70. v.refresh(0, 0, offy, offx, offy + height, offx + width * 2)
  71.  
  72. def main(stdscr):
  73. locale.setlocale(locale.LC_ALL, '')
  74. enc = locale.getpreferredencoding()
  75. w = curses.initscr()
  76. curses.curs_set(0)
  77. curses.noecho()
  78. w.keypad(1)
  79. curses.cbreak()
  80. curses.start_color()
  81. for i, c in enumerate(colors):
  82. if i: curses.init_pair(i, getattr(curses, 'COLOR_%s' % colors[0]) + 8,
  83. getattr(curses, 'COLOR_%s' % c) + (0 if i == 1 else 8))
  84. v = curses.newpad(height + 1, (width + 1) * 2) # double buffering
  85. v.keypad(1)
  86. global blocks, scrmap
  87. blocks = makecoords(blocklist)
  88. for j in xrange(height):
  89. if j < height - 1:
  90. scrmap[j][0] = scrmap[j][width - 1] = 1
  91. else:
  92. for i in xrange(width): scrmap[j][i] = 1
  93. random.seed()
  94. speed, tp, stat, y, x, n, b = 0.5, 0, 0, 3, 10, -1, []
  95. while True:
  96. curses.flushinp() # clear key buffer
  97. if stat == 0:
  98. stat += 1
  99. y, x = 3, 10
  100. n = random.randint(0, len(blocks) - 1)
  101. b = copy.deepcopy(blocks[n])
  102. showblock(y, x, b, n)
  103. tp = time.time()
  104. elif stat == 1:
  105. if time.time() - tp >= speed:
  106. ny, nx = moveblock(y, x, b, n, 1, 0)
  107. if ny != y: y, x = ny, nx
  108. else: stat = 2
  109. tp = time.time()
  110. elif stat == 2:
  111. if time.time() - tp >= speed:
  112. stat = 0
  113. tp = time.time()
  114. display(v)
  115. v.timeout(50)
  116. c = v.getch()
  117. if c == -1: pass
  118. elif c == ord('q'): break
  119. elif c == ord('z'): rotateblock(y, x, b, n, -1) # counter clockwise
  120. elif c == ord('x'): rotateblock(y, x, b, n, 1) # clockwise
  121. elif c == curses.KEY_UP: rotateblock(y, x, b, n, 1) # clockwise
  122. elif c == curses.KEY_LEFT: y, x = moveblock(y, x, b, n, 0, -1)
  123. elif c == curses.KEY_RIGHT: y, x = moveblock(y, x, b, n, 0, 1)
  124. elif c == curses.KEY_DOWN: y, x = moveblock(y, x, b, n, 1, 0)
  125. elif c == ord(' '): y, x = moveblock(y, x, b, n, 3, 0)
  126. else: pass
  127. v.keypad(0)
  128. curses.nocbreak()
  129. w.keypad(0)
  130. curses.echo()
  131. curses.curs_set(1)
  132. curses.endwin()
  133.  
  134. if __name__ == '__main__':
  135. curses.wrapper(main)
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty