下面的代码被写为一个玩具代码,用于在MacBook Pro上测试curses模块(我使用终端应用程序中的默认python安装)。 测试会创建一个由L符号表示的“敌人”,该符号遵循一系列动作,玩家以“@”表示。 无论我如何处理边界问题(请参阅宽度和高度处理),我唯一关心的问题是碰撞检测不起作用。 我不明白,如果它是由错误的块或时间的位移造成的。
代码粘贴:
import curses import random from time import sleep screen = curses.initscr() screen.keypad(True) curses.noecho() screen.nodelay(True) xpos=1 ypos=1 i=0 h,w = screen.getmaxyx() w=w-22 h=h-5 e1startxpos=random.randint(5,80) e1startypos=random.randint(2,15) e1xpos=[0,0,0,1,1,1,0,0,0,-1,-1,-1] e1ypos=[1,1,1,0,0,0,-1,-1,-1,0,0,0] tempe1x = e1startypos+e1ypos[i] tempe1y = e1startxpos+e1xpos[i] while True: screen.clear() screen.border(0) screen.addstr(ypos,xpos,"@") screen.addstr(0,w,"xpos:{0}ypos:{1}h:{2}w:{3}".format(xpos,ypos,h,w)) if (xpos == tempe1x and ypos == tempe1y):#The detector, which should run before another cycle screen.addstr(1,1,"Collision Detected: Exiting") screen.refresh() sleep(1.5) #timing redundant to see the detection of collision break else: tempe1x = tempe1x+e1ypos[i] tempe1y = tempe1y+e1xpos[i] screen.addstr(tempe1x,tempe1y,"L") if(i == len(e1xpos)-1): i=0 else: i+=1 screen.refresh() c = screen.getch() if c == ord('a'): if xpos>0: xpos = xpos-1 elif c == ord('d'): if xpos<w: xpos = xpos+1 elif c == ord('w'): if ypos>0: ypos = ypos-1 elif c == ord('s'): if ypos<h: ypos = ypos+1 elif c == ord('q'): break sleep(0.1) screen.clear() screen.addstr(0,0,"Gioco Finito") screen.refresh() sleep(2) curses.echo() curses.endwin()`PS:我没有经验在此平台上编辑帖子,所以此代码副本可能导致无法正确缩进
The following code is written as a toy code for testing curses module on a MacBook Pro(I'm using the default python installation which comes in Terminal App). The test creates an "Enemy" represented by the L sign, which follows a cycle of moves, and the player represented by "@". Regardless of how I terribly handled the border problem(see width and height handling), the only issue I care about is the collision detection that doesn't work. I do not understand if it's caused by wrong displacement of the if block or timing.
code paste:
import curses import random from time import sleep screen = curses.initscr() screen.keypad(True) curses.noecho() screen.nodelay(True) xpos=1 ypos=1 i=0 h,w = screen.getmaxyx() w=w-22 h=h-5 e1startxpos=random.randint(5,80) e1startypos=random.randint(2,15) e1xpos=[0,0,0,1,1,1,0,0,0,-1,-1,-1] e1ypos=[1,1,1,0,0,0,-1,-1,-1,0,0,0] tempe1x = e1startypos+e1ypos[i] tempe1y = e1startxpos+e1xpos[i] while True: screen.clear() screen.border(0) screen.addstr(ypos,xpos,"@") screen.addstr(0,w,"xpos:{0}ypos:{1}h:{2}w:{3}".format(xpos,ypos,h,w)) if (xpos == tempe1x and ypos == tempe1y):#The detector, which should run before another cycle screen.addstr(1,1,"Collision Detected: Exiting") screen.refresh() sleep(1.5) #timing redundant to see the detection of collision break else: tempe1x = tempe1x+e1ypos[i] tempe1y = tempe1y+e1xpos[i] screen.addstr(tempe1x,tempe1y,"L") if(i == len(e1xpos)-1): i=0 else: i+=1 screen.refresh() c = screen.getch() if c == ord('a'): if xpos>0: xpos = xpos-1 elif c == ord('d'): if xpos<w: xpos = xpos+1 elif c == ord('w'): if ypos>0: ypos = ypos-1 elif c == ord('s'): if ypos<h: ypos = ypos+1 elif c == ord('q'): break sleep(0.1) screen.clear() screen.addstr(0,0,"Gioco Finito") screen.refresh() sleep(2) curses.echo() curses.endwin()`PS:I'm not experienced into editing post on this platform, so this copy of the code might result not correctly indented
最满意答案
首先,你在这里混合了tempe1x和tempe1y的初始化:
tempe1x = e1startypos+e1ypos[i] tempe1y = e1startxpos+e1xpos[i]您需要切换x和y:
tempe1x = e1startxpos+e1xpos[i] tempe1y = e1startypos+e1ypos[i]接下来, addstr方法首先获取y位置,然后获取x位置。 你在玩家身上得到了正确的结果: screen.addstr(ypos,xpos,"@")但是通过将x位置放在第一位: screen.addstr(tempe1x, tempe1y,"L") ,将其与敌人角色screen.addstr(tempe1x, tempe1y,"L") 。 它应该是:
screen.addstr(tempe1y,tempe1x,"L")这从我的经验。 我对curses库没有经验,所以如果你仍然遇到任何问题,请告诉我。
-Jason
First, you mixed up the initialization of tempe1x and tempe1y here:
tempe1x = e1startypos+e1ypos[i] tempe1y = e1startxpos+e1xpos[i]You need to switch the x's and y's:
tempe1x = e1startxpos+e1xpos[i] tempe1y = e1startypos+e1ypos[i]Next, the addstr method takes the y position first, then the x position. You got that right with the player: screen.addstr(ypos,xpos,"@") but you messed it up with the enemy character by putting the x position first: screen.addstr(tempe1x, tempe1y,"L"). It should be:
screen.addstr(tempe1y,tempe1x,"L")This worked from my experience. I have no experience with the curses library, so if you still run into any problems, let me know.
-Jason
更多推荐
发布评论