Advertisement
Guest User

Untitled

a guest
Jan 18th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.50 KB | None | 0 0
  1. requires sdl
  2.  
  3. sdl_window window
  4. sdl_renderer renderer
  5. sdl_event e
  6.  
  7. 640 constant win-w
  8. 480 constant win-h
  9.  
  10. 20 constant ball-size
  11. 20 constant paddle-w
  12. 100 constant paddle-h
  13. 30 constant p1-x-offset
  14. win-w paddle-w - 30 - constant p2-x-offset
  15.  
  16. variable p1-y
  17. variable p2-y
  18. variable ball-x
  19. variable ball-y
  20. variable ball-x-vel
  21. variable ball-y-vel
  22. variable quit-flag
  23. variable start-dir
  24.  
  25. variable p1-going-up
  26. variable p2-going-up
  27. variable p1-going-down
  28. variable p2-going-down
  29.  
  30. variable p1-score
  31. variable p2-score
  32.  
  33. : init
  34. sdl_init_video sdl_init
  35. if ." Failed to initalize SDL video" cr then ;
  36.  
  37. : set_hints
  38. sdl_hint_render_scale_quality z" 1" sdl_sethint
  39. 0= if ." Linear filters not enabled" cr then ;
  40.  
  41. : create_window
  42. z" Pong" 100 100 win-w win-h sdl_window_shown
  43. sdl_createwindow to window
  44. window 0= if ." Failed to create window" cr then ;
  45.  
  46. : create_renderer
  47. window -1 SDL_RENDERER_ACCELERATED sdl_createrenderer
  48. to renderer
  49. renderer 0= if ." Failed to create renderer" cr then ;
  50.  
  51. : start_ball
  52. win-w 2/ ball-size 2/ - ball-x !
  53. win-h 2/ ball-size 2/ - ball-y !
  54. start-dir @ -1 = if
  55. -6 ball-x-vel !
  56. else 6 ball-x-vel ! then
  57. 0 ball-y-vel !
  58. start-dir @ invert start-dir ! ;
  59.  
  60. : put_score cr p1-score ? ." - " p2-score ? ;
  61.  
  62. : collide_p1
  63. ball-x @ p1-x-offset paddle-w + >
  64. ball-x @ ball-size + p1-x-offset <
  65. ball-y @ p1-y @ paddle-h + >
  66. ball-y @ ball-size + p1-y @ <
  67. or or or invert
  68. if
  69. 6 ball-x-vel !
  70. p1-y @ paddle-h 2/ + ball-y @ ball-size 2/ + - -20 /
  71. ball-y-vel @ + ball-y-vel !
  72. then ;
  73. : collide_p2
  74. ball-x @ p2-x-offset paddle-w + >
  75. ball-x @ ball-size + p2-x-offset <
  76. ball-y @ p2-y @ paddle-h + >
  77. ball-y @ ball-size + p2-y @ <
  78. or or or invert
  79. if -6 ball-x-vel !
  80. p2-y @ paddle-h 2/ + ball-y @ ball-size 2/ + - -20 /
  81. ball-y-vel @ + ball-y-vel ! then ;
  82. : collide_walls
  83. ball-y @ 0 < if
  84. 0 ball-y ! ball-y-vel @ -1 * ball-y-vel ! then
  85. ball-y @ ball-size + win-h > if
  86. win-h ball-size - ball-y ! ball-y-vel @ -1 * ball-y-vel ! then
  87. ball-x @ 0 < if
  88. p2-score @ +1 p2-score !
  89. put_score
  90. start_ball then
  91. ball-x @ ball-size + win-w > if
  92. p1-score @ +1 p1-score !
  93. put_score
  94. start_ball then ;
  95. : ball_collide collide_p1 collide_p2 collide_walls ;
  96.  
  97. : pong_setup
  98. win-h 2/ paddle-h 2/ - p1-y !
  99. p1-y @ p2-y !
  100. -1 start-dir !
  101. start_ball
  102. 0 0 0 0 p1-going-up ! p2-going-up ! p1-going-down ! p2-going-down !
  103. 0 0 p1-score ! p2-score ! ;
  104.  
  105. : p1_up 0 p1-y @ -5 + max p1-y ! ;
  106. : p2_up 0 p2-y @ -5 + max p2-y ! ;
  107. : p1_down win-h paddle-h - p1-y @ 5 + min p1-y ! ;
  108. : p2_down win-h paddle-h - p2-y @ 5 + min p2-y ! ;
  109. : ball_move ball-x-vel @ ball-x +! ball-y-vel @ ball-y +! ;
  110.  
  111. sdl_rect rect
  112. : render-rect ( x y w h -- )
  113. rect sdl_rect:h !
  114. rect sdl_rect:w !
  115. rect sdl_rect:y !
  116. rect sdl_rect:x !
  117. renderer rect sdl_renderfillrect drop ;
  118.  
  119. : pong_render_p1
  120. p1-x-offset p1-y @ paddle-w paddle-h render-rect ;
  121. : pong_render_p2
  122. p2-x-offset p2-y @ paddle-w paddle-h render-rect ;
  123. : pong_render_ball
  124. ball-x @ ball-y @ ball-size ball-size render-rect ;
  125.  
  126. : pong_render
  127. pong_render_p1 pong_render_p2 pong_render_ball ;
  128.  
  129. : go
  130. init set_hints create_window create_renderer
  131. pong_setup
  132. 0 quit-flag !
  133. begin
  134. begin
  135. e sdl_pollevent
  136. while
  137. e SDL_CommonEvent:type sdl_quitevent = if
  138. -1 quit-flag !
  139. else e SDL_CommonEvent:type sdl_keydown = if
  140. e SDL_KeyboardEvent:keysym @ sdl_scancode_w = if
  141. -1 p1-going-up !
  142. else e SDL_KeyboardEvent:keysym @ sdl_scancode_s = if
  143. -1 p1-going-down !
  144. else e SDL_KeyboardEvent:keysym @ sdl_scancode_up = if
  145. -1 p2-going-up !
  146. else e SDL_KeyboardEvent:keysym @ sdl_scancode_down = if
  147. -1 p2-going-down !
  148. then then then then
  149. else e SDL_CommonEvent:type sdl_keyup if
  150. e SDL_KeyboardEvent:keysym @ sdl_scancode_w = if
  151. 0 p1-going-up !
  152. else e SDL_KeyboardEvent:keysym @ sdl_scancode_s = if
  153. 0 p1-going-down !
  154. else e SDL_KeyboardEvent:keysym @ sdl_scancode_up = if
  155. 0 p2-going-up !
  156. else e SDL_KeyboardEvent:keysym @ sdl_scancode_down = if
  157. 0 p2-going-down !
  158. then then then then
  159. then then then
  160. repeat
  161.  
  162. p1-going-up @ if p1_up then
  163. p2-going-up @ if p2_up then
  164. p1-going-down @ if p1_down then
  165. p2-going-down @ if p2_down then
  166.  
  167. ball_move
  168. ball_collide
  169. renderer $00 $00 $00 $ff sdl_setrenderdrawcolor
  170. renderer sdl_renderclear
  171. renderer $ff $ff $ff $ff sdl_setrenderdrawcolor
  172. pong_render
  173. renderer sdl_renderpresent
  174. 15 sdl_delay
  175. quit-flag @ until
  176. sdl_quit cr ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement