Advertisement
xerpi

piring pingpong

Jun 1st, 2011
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.87 KB | None | 0 0
  1. math.randomseed(os.clock())
  2. player={}
  3.  
  4. player[1]={x=10,y=10,w=5,h=40,vel=3,score=0}
  5. player[1].img = image.create(player[1].w,player[1].h,color.new(0,255,0))
  6.  
  7. player[2]={x=465,y=10,w=5,h=40,vel=3,score=0}
  8. player[2].img = image.create(player[2].w,player[2].h,color.new(0,0,255))
  9.  
  10. ball={x=240,y=136,w=10,h=10,ang=math.random(1,360),vel=3,dir=1,p=0}
  11. ball.img=image.create(ball.w,ball.h,color.new(255,0,0))
  12.  
  13.  
  14.  
  15. function colision(Obj1,Obj2)
  16. if Obj1.x + Obj1.w >= Obj2.x and
  17.     Obj1.x <= Obj2.x + Obj2.w and
  18.     Obj1.y + Obj1.h >= Obj2.y and
  19.     Obj1.y <= Obj2.y + Obj2.h then
  20. return true else return false
  21. end
  22. end
  23.  
  24. ball.img:center(ball.w/2,ball.h/2)
  25.  
  26. while true do
  27. controls.read()
  28.  
  29. ejex = math.cos(math.rad(ball.ang-90))*ball.vel
  30. ejey = math.sin(math.rad(ball.ang-90))*ball.vel
  31. if ball.x-ball.w/2 <= 0 then ejex = -ejex end
  32. if ball.y-ball.h/2 <= 0 then ejey = -ejey end
  33. if ball.x+ball.w/2 >= 480 then ejex = -ejex end
  34. if ball.y+ball.h/2 >= 272 then ejey = -ejey end
  35. ball.ang = math.atan2(ejey,ejex);
  36.  
  37. ball.img:blit(ball.x,ball.y)
  38.  
  39. if controls.r() then
  40.     ball.ang = ball.ang +1
  41. end
  42. if controls.l() then
  43.     ball.ang = ball.ang -1
  44. end
  45.  
  46. if not controls.start() then
  47. ball.x = ball.x + math.cos(math.rad(ball.ang-90))*ball.vel
  48. ball.y = ball.y + math.sin(math.rad(ball.ang-90))*ball.vel
  49. end
  50.  
  51.  
  52.  
  53. for i=1, #player do
  54.     if colision(player[i],ball) then ball.ang =  ball.ang+90 end
  55.     player[i].img:blit(player[i].x,player[i].y)
  56. end
  57.  
  58.  
  59. if controls.down() then player[1].y = math.min(player[1].y + player[1].vel,272-player[1].h)  end
  60. if controls.up() then player[1].y = math.max(player[1].y - player[1].vel ,0) end
  61. if controls.cross() then player[2].y = math.min(player[2].y + player[2].vel,272-player[2].h) end
  62. if controls.triangle() then player[2].y = math.max(player[2].y - player[2].vel ,0) end
  63.  
  64.  
  65.  
  66. screen.print(50,5,ball.ang)
  67.  
  68. if controls.select() then a() end
  69. screen.flip()
  70. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement