Advertisement
xerpi

Untitled

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