Advertisement
xerpi

pong

Jun 1st, 2011
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. player={}
  2.  
  3. player[1]={x=10,y=136-20,w=5,h=40,vel=3,score=0}
  4. player[1].img = image.create(player[1].w,player[1].h,color.new(0,255,0))
  5.  
  6. player[2]={x=465,y=136-20,w=5,h=40,vel=3,score=0}
  7. player[2].img = image.create(player[2].w,player[2].h,color.new(0,0,255))
  8.  
  9. ball={x=240,y=136,w=10,h=10,ang=(math.random()-0.5)*math.pi,v=5,dx=0,dy=0}
  10. ball.img=image.create(ball.w,ball.h,color.new(255,0,0))
  11.  
  12. goal={}
  13. goal[1]={x=0,y=116,w=5,h=40}
  14. goal[2]={x=475,y=116,w=5,h=40}
  15.  
  16.  
  17.  
  18. function colision(Obj1,Obj2)
  19. if Obj1.x + Obj1.w >= Obj2.x and
  20.     Obj1.x <= Obj2.x + Obj2.w and
  21.     Obj1.y + Obj1.h >= Obj2.y and
  22.     Obj1.y <= Obj2.y + Obj2.h then
  23. return true else return false
  24. end
  25. end
  26.  
  27. ball.img:center(ball.w/2,ball.h/2)
  28.  
  29.  
  30. while true do
  31. controls.read()
  32.  
  33.  
  34.  
  35. ball.dx, ball.dy = math.vector.cartesian(ball.v,ball.ang);
  36. ball.x = ball.x + ball.dx;
  37. ball.y = ball.y + ball.dy;
  38. if (ball.y-ball.h/2<0 and ball.dy<0) or (ball.y+ball.h/2>272 and ball.dy>0) then ball.dy=-ball.dy;end
  39. if (ball.x-ball.w/2<0 and ball.dx<0) or (ball.x+ball.w/2>480 and ball.dx>0) then ball.dx=-ball.dx;end
  40. for i=1, #player do
  41.     if colision(player[i],ball) then ball.dx = -ball.dx end
  42.     player[i].img:blit(player[i].x,player[i].y)
  43.     draw.rect(goal[i].x,goal[i].y,goal[i].w,goal[i].h,color.new(255,255,255))
  44.     if colision(ball,goal[1]) then player[2].score=player[2].score+1 ball.ang,ball.x,ball.y = (math.random()-0.5)*math.pi,240,136  ball.v = 5 end
  45.     if colision(ball,goal[2]) then player[1].score=player[1].score+1 ball.ang,ball.x,ball.y = (math.random()-0.5)*math.pi,240,136 ball.v = 5 end
  46. end
  47.  
  48. ball.v, ball.ang = math.vector.polar(ball.dx,ball.dy);
  49.  
  50. ball.img:blit(ball.x,ball.y)
  51. if controls.down() then player[1].y = math.min(player[1].y + player[1].vel,272-player[1].h)  end
  52. if controls.up() then player[1].y = math.max(player[1].y - player[1].vel ,0) end
  53. if controls.cross() then player[2].y = math.min(player[2].y + player[2].vel,272-player[2].h) end
  54. if controls.triangle() then player[2].y = math.max(player[2].y - player[2].vel ,0) end
  55.  
  56.  
  57. screen.print(5,5,"        Score: "..player[1].score.."                                                     Score: "..player[2].score)
  58.  
  59. ball.v=ball.v+0.01
  60.  
  61. if controls.select() then a() end
  62. screen.flip()
  63. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement