Search code examples
pythoncollisioncurses

Collision Detection Python curses


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


Solution

  • 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