473,416 Members | 1,558 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

python game with curses

Hi,

I have wrote a game with python curses. The problem is that I want to
confirm before quitting, while my implementation doesn't seem to work.
Anyone can help me?
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/python
  2. #
  3. # Brick & Ball in Python
  4. # by Jerry Fleming <je**********@etang.com>
  5. #
  6. # This is a small game adapted from that in Motorola Mobile C289, and my
  7. first game in python :)
  8. #
  9. # This progrma is best run under linux. Since Windows port of Python has
  10. poor curses support,
  11. # play it under Windows is not recommended. If you have no linux box
  12. available, try Cygwin,
  13. # though it too has poor curses support.
  14. #
  15. # As I am a newbie to python, please tell me if you have a better
  16. implementation or any suggestions.
  17. #
  18. # TODO:
  19. # re-implemente it with wxPython, so one does not have to rely on the
  20. ugly curses.
  21. # session support.
  22. # pausing, especially when confirming
  23. # resize terminal at run time
  24. #
  25. # HISTORY
  26. # 2006-04-19: first version
  27. #
  28. #
  29.  
  30. import curses
  31. import _curses
  32. import thread
  33. from time import sleep
  34. from string import split
  35. from random import randint
  36.  
  37. # parameters: adjust them to fit you terminal
  38. brick_width = 7
  39. brick_gap_x = 1
  40. brick_gap_y = 1
  41. speed = 0.05 # sleep time to control moving speed of the ball
  42. pause = 1 # time to pause
  43.  
  44. # terminal initialization
  45. stdscr = curses.initscr()
  46. curses.noecho()
  47. curses.cbreak()
  48. curses.curs_set(0)
  49. stdscr.keypad(1)
  50. screen_height, screen_width = stdscr.getmaxyx()
  51. screen_height = screen_height - 1
  52. screen_width = screen_width - 1
  53. brick_rows = screen_height / 4
  54. if brick_rows > 7: brick_rows = 7
  55. brick_cols = (screen_width + brick_gap_x) / (brick_width + brick_gap_x)
  56. brick_margin = (screen_width - brick_cols * (brick_width + brick_gap_x)
  57. + brick_gap_x)/2
  58. pad_position = randint(0, screen_width - brick_width)
  59. ball_position = [screen_height - 3, randint(0, screen_width - brick_width)]
  60. ball_direction = [-1, 1] # abs(tan(a)) must be 1
  61. char = ''
  62. bricks = []
  63. game_score = 0
  64. ScreenSizeError = 'ScreenSizeError'
  65.  
  66. tStart = '''
  67. ______        _       _         ___      ______        _ _     _
  68. ______             _
  69. (____  \      (_)     | |       / _ \    (____  \      | | |   (_)
  70. (_____ \        _  | |
  71. ____)  ) ____ _  ____| |  _   ( (_) )    ____)  )_____| | |    _ ____
  72. _____) )   _ _| |_| |__   ___  ____
  73. |  __  ( / ___) |/ ___) |_/ )   ) _ (    |  __  ((____ | | |   | |  _ \
  74. |  ____/ | | (_   _)  _ \ / _ \|  _ \
  75. | |__)  ) |   | ( (___|  _ (   ( (/  \   | |__)  ) ___ | | |   | | | | |
  76. | |    | |_| | | |_| | | | |_| | | | |
  77. |______/|_|   |_|\____)_| \_)   \__/\_)  |______/\_____|\_)_)  |_|_| |_|
  78. |_|     \__  |  \__)_| |_|\___/|_| |_|
  79.  
  80. (____/
  81.  
  82.  
  83. by Jerry Fleming <je**********@etang.com>
  84.  
  85.  
  86. GAME STARTING ...
  87.  
  88. (this assumes that your terminal be larger that
  89. 130x40)
  90. '''
  91.  
  92. tExit = '''
  93. 88888
  94. 88888
  95. 88888888888             88       ad88888ba     88 8b        d8      d8
  96. 88
  97. 88                      ""   ,d d8"     "8b    88  Y8,    ,8P     ,8P'
  98. 88
  99. 88                           88 ""      a8P    88   Y8,  ,8P     d8"
  100. 88
  101. 88aaaaa     8b,     ,d8 88 MM88MMM   ,a8P"     88    "8aa8"    ,8P'
  102. 8b,dPPYba,  88
  103. 88"""""      `Y8, ,8P'  88   88     d8"        88     `88'    d8"   88P'
  104. `"8a 88
  105. 88             )888(    88   88     ""         88      88   ,8P'    88
  106. 88 88
  107. 88           ,d8" "8b,  88   88,    aa         88      88  d8"      88
  108. 88 88
  109. 88888888888 8P'     `Y8 88   "Y888  88         88      88 8P'       88
  110. 88 88
  111. 88888
  112. 88888
  113. '''
  114.  
  115. tOver = '''
  116. ,ad8888ba,
  117. 88
  118. d8"'    `"8b
  119. 88
  120. d8'
  121. 88
  122. 88            ,adPPYYba, 88,dPYba,,adPYba,   ,adPPYba,     ,adPPYba,  8b
  123. d8  ,adPPYba, 8b,dPPYba, 88
  124. 88      88888 ""     `Y8 88P'   "88"    "8a a8P_____88    a8"     "8a
  125. `8b     d8' a8P_____88 88P'   "Y8 88
  126. Y8,        88 ,adPPPPP88 88      88      88 8PP"""""""    8b       d8
  127. `8b   d8'  8PP""""""" 88         ""
  128. Y8a.    .a88 88,    ,88 88      88      88 "8b,   ,aa    "8a,   ,a8"
  129. `8b,d8'   "8b,   ,aa 88         aa
  130. `"Y88888P"  `"8bbdP"Y8 88      88      88  `"Ybbd8"'     `"YbbdP"'
  131. "8"      `"Ybbd8"' 88         88
  132. '''
  133.  
  134. tGoon = '''
  135. 88888
  136. 88888
  137. ,ad8888ba,                                      ad88888ba     88 8b
  138. d8      d8          88
  139. d8"'    `"8b                                    d8"     "8b    88  Y8,
  140. ,8P     ,8P'          88
  141. d8'                                              ""      a8P    88   Y8,
  142. ,8P     d8"            88
  143. 88             ,adPPYba,      ,adPPYba,  8b,dPPYba,   ,a8P"     88
  144. "8aa8"    ,8P' 8b,dPPYba,  88
  145. 88      88888 a8"     "8a    a8"     "8a 88P'   `"8a d8"        88
  146. `88'    d8"   88P'   `"8a 88
  147. Y8,        88 8b       d8    8b       d8 88       88 ""         88
  148. 88   ,8P'    88       88 88
  149. Y8a.    .a88 "8a,   ,a8"    "8a,   ,a8" 88       88 aa         88
  150. 88  d8"      88       88 88
  151. `"Y88888P"   `"YbbdP"'      `"YbbdP"'  88       88 88         88
  152. 88 8P'       88       88 88
  153. 88888
  154. 88888
  155. '''
  156.  
  157. def init_game():
  158. '''Game initializing.'''
  159. global bricks
  160. # display the bricks
  161. for row in range(brick_rows):
  162. y = row * (1 + brick_gap_y)
  163. for col in range(brick_cols):
  164. x = col * (brick_gap_x + brick_width) + brick_margin
  165. stdscr.addstr(y, x, ' ' * brick_width, curses.A_REVERSE)
  166. bricks.append([y, x])
  167. # move the pad to center of bottom at starting up
  168. stdscr.addstr(screen_height - 1, pad_position, ' ' * brick_width,
  169. curses.A_REVERSE)
  170. # move the ball to left bottom side at starting up
  171. stdscr.addstr(ball_position[0], ball_position[1], ' ', curses.A_REVERSE)
  172. # display score board
  173. stdscr.addstr(screen_height, 0, ' SCORE: '+ ('%03d' % game_score) + '
  174. '* 4, curses.A_REVERSE)
  175. stdscr.addstr(screen_height, 15, 'USE q to quit' + ' '* (screen_width -
  176. 28), curses.A_REVERSE)
  177. # final step to init display
  178. stdscr.refresh()
  179.  
  180. def move_pad(lock):
  181. '''Move the pad to catch the ball.'''
  182. global char, pad_position
  183. char = stdscr.getch()
  184. if char == ord('q'): quit_game(lock)
  185. if char == curses.KEY_LEFT: pad_position = pad_position - 1
  186. if char == curses.KEY_RIGHT: pad_position = pad_position + 1
  187. if pad_position < 0: pad_position = 0
  188. if pad_position >= screen_width - brick_width: pad_position =
  189. screen_width - brick_width
  190. stdscr.addstr(screen_height - 1, 0, ' ' * screen_width)
  191. stdscr.addstr(screen_height - 1, pad_position, ' ' * brick_width,
  192. curses.A_REVERSE)
  193. stdscr.refresh()
  194.  
  195. def move_ball(lock):
  196. '''Move the ball to a direction.'''
  197. global ball_position, ball_direction
  198. # clear the old position, do not if in pad
  199. if ball_position[0] != screen_height - 1:
  200. stdscr.addstr(ball_position[0], ball_position[1], ' ')
  201. ball_position[0] = ball_position[0] + ball_direction[0]
  202. ball_position[1] = ball_position[1] + ball_direction[1]
  203. stdscr.addstr(ball_position[0], ball_position[1], ' ', curses.A_REVERSE)
  204. detect_collision(lock)
  205. stdscr.refresh()
  206. sleep(speed)
  207.  
  208. def detect_collision(lock):
  209. '''Detect whether the ball has hit something, change direction if yes.'''
  210. global bricks, ball_direction, game_score
  211. # hit upper wall
  212. if ball_position[0] == 0 :
  213. ball_direction = [- ball_direction[0], ball_direction[1]]
  214. # hit left and right wall
  215. if ball_position[1] == 0 or ball_position[1] == screen_width:
  216. ball_direction = [ball_direction[0], - ball_direction[1]]
  217. # hit brick
  218. for brick in bricks:
  219. # hit from bottom or upper direction
  220. if brick[1] <= ball_position[1] + ball_direction[1] <= brick[1] +
  221. brick_width \
  222. and ball_position[0] + ball_direction[0] == brick[0]:
  223. ball_direction[0] = - ball_direction[0]
  224. stdscr.addstr(brick[0], brick[1], ' '* brick_width)
  225. game_score = game_score + 10
  226. stdscr.addstr(screen_height, 8, '%03d' % game_score, curses.A_REVERSE)
  227. bricks.remove(brick)
  228. # another level to continue the game
  229. if not len(bricks): another_level()
  230. # hit body of pad
  231. if ball_position[0] == screen_height - 2 and pad_position <=
  232. ball_position[1] <= pad_position + brick_width:
  233. ball_direction[0] = - ball_direction[0]
  234. # hit bottom
  235. elif ball_position[0] == screen_height - 1:
  236. ball_direction[0] = - ball_direction[0]
  237. # hit left side of pad (hit bottom already)
  238. if ball_position[1] == pad_position and  char == curses.KEY_LEFT and
  239. ball_direction[1]:
  240. ball_direction[1] = - ball_direction[1]
  241. # hit right side of pad (hit bottom already)
  242. if ball_position[1] == pad_position + brick_width and  char ==
  243. curses.KEY_RIGHT and not ball_direction[1]:
  244. ball_direction[1] = - ball_direction[1]
  245. # hit bottom wall, game over
  246. if ball_position[0] == screen_height - 1 and not pad_position <=
  247. ball_position[1] <= pad_position + brick_width:
  248. line_num = 0
  249. for line in split(tOver, "\n"):
  250. stdscr.addstr(screen_height / 2 + line_num, 10, line)
  251. line_num = line_num + 1
  252. stdscr.refresh()
  253. curses.flash()
  254. sleep(1)
  255. curses.flash()
  256. sleep(1)
  257. lock.release()
  258.  
  259. def quit_game(lock):
  260. '''Confirm quit the game.'''
  261. global char, speed
  262. if char != ord('q'): return
  263. speed_old = speed # store the value for resume
  264. speed = pause # wait for a long time
  265. line_num = 0
  266. for line in split(tExit, "\n"):
  267. stdscr.addstr(screen_height / 2 + line_num, 10, line)
  268. line_num = line_num + 1
  269. stdscr.addstr(screen_height, 15, 'USE n to continue, any other key to
  270. quit' + ' '* (screen_width - 40), curses.A_REVERSE)
  271. stdscr.refresh()
  272. #char = stdscr.getch() ## fix me: why can't we use the global char?
  273. if char == ord('n'):
  274. speed = speed_old
  275. line_num = 0
  276. for line in split(tExit, "\n"):
  277. stdscr.addstr(screen_height / 2 + line_num, 10, ' ' * len(line) )
  278. line_num = line_num + 1
  279. stdscr.refresh()
  280. else:
  281. lock.release()
  282.  
  283. def another_level():
  284. '''Confirm another level of the game.'''
  285. global speed, char
  286. speed_old = speed # store the value for resume
  287. speed = pause # wait for a long time
  288. line_num = 0
  289. for line in split(tGoon, "\n"):
  290. stdscr.addstr(screen_height / 2 + line_num, 10, line)
  291. line_num = line_num + 1
  292. stdscr.addstr(screen_height, 15, 'USE n to quit, any other key to
  293. continue' + ' '* (screen_width - 40), curses.A_REVERSE)
  294. stdscr.refresh()
  295. #char = stdscr.getch()
  296. if char == ord('n'):
  297. lock.release()
  298. else:
  299. stdscr.erase()
  300. speed = speed / 2
  301. init_game()
  302.  
  303. def looper(fun, lock):
  304. '''Dispatcher to drive th ball and pad.'''
  305. try:
  306. while lock.locked(): globals()[fun](lock)
  307. except _curses.error, diag:
  308. if lock.locked(): lock.release()
  309.  
  310. # main loop starts here
  311. if __name__ == "__main__":
  312. try:
  313. line_num = 0
  314. for line in split(tStart, "\n"):
  315. stdscr.addstr(screen_height /4 + line_num, 10, line)
  316. line_num = line_num + 1
  317. stdscr.refresh()
  318. sleep(1)
  319. stdscr.erase()
  320. init_game()
  321. locks = []
  322. for i in range(2):
  323. lock = thread.allocate_lock()
  324. lock.acquire()
  325. locks.append(lock)
  326. thread.start_new_thread(looper, ('move_ball', locks[0]))
  327. thread.start_new_thread(looper, ('move_pad', locks[1]))
  328. while locks[0].locked() and locks[1].locked: pass # main loop
  329. except _curses.error, diag:
  330. msg = 'Your terminal is too small: ' + str(diag)
  331. except 'dd':
  332. msg = 'Game exit abnormally.'
  333. else:
  334. msg = 'Game stopped.'
  335. # out of loop means stop
  336. for i in range(2):
  337. if locks[i].locked(): locks[i].release()
  338. height, width = stdscr.getmaxyx()
  339. if height - 1 < screen_height or width - 1 < screen_width:
  340. msg = 'Your terminal is shrinked.'
  341. # out of loop means stop
  342. curses.curs_set(1)
  343. stdscr.keypad(0)
  344. curses.nocbreak()
  345. curses.echo()
  346. curses.endwin()
  347. print msg
  348.  
  349.  
Apr 28 '06 #1
1 3669
Jerry,

if you want anyone to answer your question, please read this:
http://www.catb.org/~esr/faqs/smart-questions.html

Apr 28 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Brian | last post by:
Hello; I'm writing a program with curses in python and having a bit of trouble understanding how to use unittest. So far, I have used testing successfully -- as long as the report goes to stdout...
0
by: Matthew Alton | last post by:
The appended program freaks python 2.2 & 2.3 completely out. To reproduce the wierdness: i) copy the source to a file called consarn.py ii) $ python consarn.py; iii) the program is now doing a...
9
by: David Bear | last post by:
I need python 2.3. I have freebsd 4.10-releng. when configuring python I received the following: ../configure --prefix=/home/webenv > config-results configure: WARNING: curses.h: present but...
7
by: Gasten | last post by:
Hello. The last weeks I've been coding a roguelike (you know, like nethack) in python using the nCurses library. Some week ago I ran into a problem: When I made the object for messagebar-output, I...
3
by: Maxim Veksler | last post by:
Hi list, I'm working on writing sanity check script, and for aesthetic reasons I would like the output be in the formatted like the gentoo init script output, that is: """ Check for something...
15
by: pinkfloydhomer | last post by:
I need to develop a cross-platform text-mode application. I would like to do it in Python and I would like to use a mature text-mode library for the UI stuff. The obvious choice, I thought, was...
1
by: shrek2099 | last post by:
Hi All, Recently I ran into a problem with UTF-8 surrport when using curses library in python 2.5 in Fedora 7. I found out that the program using curses cannot print out unicode characters...
3
by: Mdonle | last post by:
Seeing the 7DRL start up recently, i wanted to see what one was made of. Python is the language i'm most familiar with so i searched for some code to look at, but i couldn't find any. Can anyone...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.