python - Why does this code have the "ball" seem to slide along the edge -


My son asked me if I could write a small program to bounce a ball around the screen, So I have been explained this. Opening the opportunity of a clean father-son, I said, "Yes, there is no problem", so I dug my dragon skills and wrote it.

  #! / Usr / bin / python # # We have to tell Python what are we going to use? We do this by asking for import # import that we import # import shirms to import random import time # # We are using a screen handling # package called "Curses", so we started the screen # Stdscr = Curses.initscr () # We need to make sure that the type of # key that we type will not be able to spoil the # displayed in #, which we call # curbs.nooecho () Spoiling Are there. # We need to curse that we do not want # when we ask the keys from the # user to wait, if not there, then # we want curses to continue stdscr.nodelay (True) # Something difficult to explain is difficult. Actually this keyboard puts in # mode which allows the curse to do # things with it ... curses.cbreak () # OK, now we can do fun stuff # What we need to do # Find out how big our screen is max_y, max_x = stdscr.getmaxyx () stdscr.box () min_x = 1 min_y = 1 max_y = max_y - 2 max_x = max_x - 2 stdscr.addstr (min_y, min_x, "1") stdscr .addstr (# can choose a random point on the screen, so we can choose a random point on one screen, both y (up-down) and X # (left- Right) in directions y = random.randint (0, max_y) x = random.randint (0, max_x) # OK, it basically says, while trying to # get a key from the user # with an error Is coming back, which means that no key # returns to the back, while code inside the loop (stdscr.getch () == curses.ERR): # OK, we have got to the place, " 0 "stdscr.addstr (y, x," O ") # stdscr.addstr (str (y, x)) moves the e cursor to a new position so that we get the" o "better stdscr.move (0, 0) see screen updates stdscr.refresh () at # Central # Now a new location x = x + dir_x y = y + dir_y # We are very far if x & gt; Max_X or X & LT; Min_x: # Change direction dir_x = -dir_x x = x + dir_x if y> Max_y or y & lt; Min_y: # Direction change direction dir_y = -dir_y y = y + dir_y # Wait a little while so that we can see the drawn screen Time.sleep (0.05) # OK, we're finished. Clean up curses.nocbreak () stdscr.keypad (false) curses.echo () curses.endwin ()  

I ran the program and was quite happy. However, when the ball comes to the side, it looks like "slide" and does not bounce properly.

This evening is too late, so I could not take my head around it and I thought that I can see it outside and see why no one can explain why after fixing the code No, I'm pretty sure that & lt; = Or> = Test will work with I can not figure out what it does ...

Thx Mark

You have to add dir_ * twice in the "change direction" code.


Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -