我的儿子问我是否可以写一个小程序让球在屏幕上反弹然后让我解释一下。 发现一个干净的父子机会,我说“是的!,没问题”。所以我挖出了我的python技能并写了这个..
#!/usr/bin/python
#
# We have to tell python what stuff we're
# going to use. We do this by asking to import
# the code we'll be making use of
#
import curses
import random
import time
#
# We'll be using a screen handling
# package called "curses" so we need
# to initialise the screen
stdscr = curses.initscr()
# We need to make sure that
# keys we type will not show up
# on the screen spoiling the
# stuff we're drawing
curses.noecho()
# We need to tell curses that we don't
# want to wait when we ask for keys from
# the user, if there's non there then
# we want curses to carry on
stdscr.nodelay( True )
# This ones a bit tricky to explain.
# Basically it puts the keyboard into
# a mode which allows curses to do
# things with it...
curses.cbreak()
# Ok, now we can do the fun stuff
# First thing we need to do is to
# 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( min_y, max_x, "2" )
stdscr.addstr( max_y, min_x, "3" )
stdscr.addstr( max_y, max_x, "4" )
dir_x = 1
dir_y = 1
# Then we can pick a random point on the
# screen in both the y (up-down) and x
# (left-right) directions
y = random.randint( 0, max_y )
x = random.randint( 0, max_x )
# Ok, this basically says, while trying to
# get a key from the user keeps coming back
# with an error, meaning there aren't any keys
# to return, do the code inside the loop
while( stdscr.getch() == curses.ERR ):
# Ok, at the location we got, put a "0"
stdscr.addstr( y, x, "O" )
# stdscr.addstr( str( (y, x) ) )
# this just moves the cursor to a new location so we can see the "O" better
stdscr.move( 0, 0 )
# have the screen update
stdscr.refresh()
# now choose a new location
x = x + dir_x
y = y + dir_y
# have we gone too far
if x > max_x or x < min_x:
# change direction
dir_x = -dir_x
x = x + dir_x
if y > max_y or y < min_y:
# change direction
dir_y = -dir_y
y = y + dir_y
# wait a bit so we can see the screen we've drawn.
time.sleep( 0.05 )
# Ok, we're finished. Tidy up
curses.nocbreak()
stdscr.keypad( False )
curses.echo()
curses.endwin()
我运行程序并且非常高兴。然而,当球击中边缘时,它似乎“滑动”而不是干净地反弹。
晚上已经很晚了,所以我无法理解它,我想我会把它扔出去,看看有没有人可以解释原因。 我不是在修复代码之后,我很确定使用&lt; =或&gt; =测试可以工作。我只是无法理解它做了什么......
THX 标记
答案 0 :(得分:10)
您需要在“更改方向”代码中添加两次dir_ *。
答案 1 :(得分:4)
在测试之前,您正在计算新的x和y坐标。因此,假设球在20,1处被绘制,并且假设dir是东北,坡度为1.下一个位置将在21,0处计算。这里你看到y现在超出了范围。因此,当你真正想要的是2时,你将它设置回1,所以你可以在边缘滑动。通常,我所做的是先测试下一个条件,然后添加新的偏移量。所以......
if x + dir_x > max_x or x - dir_x < min_x:
答案 2 :(得分:0)
让这种模拟在边缘正常工作是很棘手的。我在这里跑了它看起来很好(OS X)。当它碰到边缘时,你会在边缘连续得到两个O,然后它会回来。猜测一下,这与你的虚拟x,y点在反转发生前必须穿透的程度有关,这取决于它的移动速度。
答案 3 :(得分:0)
花了一些时间来弄清楚你的意思。你的意思是改变方向时墙上的双“OO”?在我看来,条件允许球越过(不到达)线然后再将其拉回。我改变了这一点:
# have we gone too far
if x >= max_x or x <= min_x:
# change direction
dir_x = -dir_x
#x = x + dir_x
if y >= max_y or y <= min_y:
# change direction
dir_y = -dir_y
#y = y + dir_y
(注意我将&gt;更改为&gt; =并且与&lt;相同,并且我还注释了第二个增量 - 如果您在线上捕获它,则不需要它。)
顺便说一句很好的例子。
答案 4 :(得分:-2)
对python不太熟悉,但是
dir_x = -dir_x
可接受?也许试试
dir_x *= -1
或
dir_x = dir_x * -1