Advertisement
xerpi

pong

Jun 1st, 2011
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.32 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=180,vel=3,dir=1,p=0}
  11. ball.img=image.create(ball.w,ball.h,color.new(255,0,0))
  12.  
  13. --ball.ang = math.random(1,360)
  14. vx=(math.sin(math.rad(ball.ang-90))-math.sin(math.rad(ball.ang-90))*5)
  15. vy=(math.cos(math.rad(ball.ang-90))-math.cos(math.rad(ball.ang-90))*5)
  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. while true do
  30. controls.read()
  31.  
  32.  
  33. ball.img:blit(ball.x,ball.y)
  34.  
  35. if controls.r() then
  36.     ball.ang = ball.ang +1
  37. end
  38. if controls.l() then
  39.     ball.ang = ball.ang -1
  40. end
  41.  
  42. if not controls.start() then
  43. ball.x = ball.x + math.cos(math.rad(ball.ang-90))*ball.vel
  44. ball.y = ball.y + math.sin(math.rad(ball.ang-90))*ball.vel
  45. end
  46.  
  47. if ball.x-ball.w/2 <= 0 then ball.ang = ball.ang*(2*ball.dir)+180 end
  48. if ball.y-ball.h/2 <= 0 then ball.ang = ball.ang*(2*ball.dir)+180 end
  49. if ball.x+ball.w/2 >= 480 then ball.ang = ball.ang*(2*ball.dir)+180 end
  50. if ball.y+ball.h/2 >= 272 then ball.ang = ball.ang*(2*ball.dir)+180 end
  51.  
  52. for i=1, #player do
  53.     if colision(player[i],ball) then ball.ang = ball.ang*(2*ball.dir)+180 end
  54.     player[i].img:blit(player[i].x,player[i].y)
  55. end
  56.  
  57. draw.line(240,136,240+math.cos(math.rad(ball.ang-90))*50,136+math.sin(math.rad(ball.ang-90))*50,color.new(255,0,0))
  58. draw.line(240,0,240,136,color.new(0,255,0))
  59.  
  60. if controls.down() then player[1].y = math.min(player[1].y + player[1].vel,272-player[1].h)  end
  61. if controls.up() then player[1].y = math.max(player[1].y - player[1].vel ,0) end
  62. if controls.cross() then player[2].y = math.min(player[2].y + player[2].vel,272-player[2].h) end
  63. if controls.triangle() then player[2].y = math.max(player[2].y - player[2].vel ,0) end
  64.  
  65.  
  66. vx=(math.sin(math.rad(ball.ang-90))-math.sin(math.rad(ball.ang-90))*5)
  67. vy=(math.cos(math.rad(ball.ang-90))-math.cos(math.rad(ball.ang-90))*5)
  68.  
  69. screen.print(50,5,ball.ang.."  P: "..vy/vx)
  70.  
  71. if controls.select() then a() end
  72. screen.flip()
  73. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement