Collision Detection is the major part of physics engine which is used in games and simulations.Python offers a well enhanced built in code that makes us to perform collision and detection of objects present in the game.
First we will deal with some mathematical concepts,
Lets see the python code for this,
-------------program starts------------------------------
import simplegui
width=640
height=480
pos=[50,50]
v=[5,5]
radius=20
def draw(canvas):
pos[0]+=v[0]
pos[1]+=v[1]
if pos[0]<=radius:
v[0]=-v[0]
v[1]=v[1]
elif pos[0]>=width-radius:
v[0]=-v[0]
v[1]=v[1]
elif pos[1]<=radius:
v[0]=v[0]
v[1]=-v[1]
elif pos[1]>=height-radius:
v[0]=v[0]
v[1]=-v[1]
canvas.draw_circle(pos,radius,5,"Red","Red")
frame=simplegui.create_frame("Collision and Detection",width,height)
frame.set_draw_handler(draw)
frame.start()
---------------------program ends------------------------------------
No comments:
Post a Comment