PageRenderTime 28ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/samples/sfmlPong/main.go

https://bitbucket.org/krepa098/gosfml2
Go | 224 lines | 160 code | 36 blank | 28 comment | 33 complexity | 219952fd422b9674afb86c82aff23cb8 MD5 | raw file
  1. /*
  2. #############################################
  3. # GOSFML2
  4. # SFML Examples: Pong
  5. # Ported from C++ to Go
  6. #############################################
  7. */
  8. package main
  9. import (
  10. sf "bitbucket.org/krepa098/gosfml2"
  11. "math"
  12. "math/rand"
  13. "runtime"
  14. "time"
  15. )
  16. func init() {
  17. runtime.LockOSThread()
  18. }
  19. func main() {
  20. const (
  21. paddleSpeed = float32(400)
  22. ballSpeed = float32(400)
  23. )
  24. var (
  25. gameWidth uint = 800
  26. gameHeight uint = 600
  27. paddleSize sf.Vector2f = sf.Vector2f{25, 100}
  28. ballRadius float32 = 10
  29. )
  30. ticker := time.NewTicker(time.Second / 60)
  31. AITicker := time.NewTicker(time.Second / 10)
  32. rand.Seed(time.Now().UnixNano())
  33. renderWindow := sf.NewRenderWindow(sf.VideoMode{gameWidth, gameHeight, 32}, "Pong (GoSFML2)", sf.StyleDefault, sf.DefaultContextSettings())
  34. // Load the sounds used in the game
  35. buffer, _ := sf.NewSoundBufferFromFile("resources/ball.wav")
  36. ballSound := sf.NewSound(buffer)
  37. // Create the left paddle
  38. leftPaddle, _ := sf.NewRectangleShape()
  39. leftPaddle.SetSize(sf.Vector2f{paddleSize.X - 3, paddleSize.Y - 3})
  40. leftPaddle.SetOutlineThickness(3)
  41. leftPaddle.SetOutlineColor(sf.ColorBlack())
  42. leftPaddle.SetFillColor(sf.Color{100, 100, 200, 255})
  43. leftPaddle.SetOrigin(sf.Vector2f{paddleSize.X / 2, paddleSize.Y / 2})
  44. // Create the right paddle
  45. rightPaddle, _ := sf.NewRectangleShape()
  46. rightPaddle.SetSize(sf.Vector2f{paddleSize.X - 3, paddleSize.Y - 3})
  47. rightPaddle.SetOutlineThickness(3)
  48. rightPaddle.SetOutlineColor(sf.ColorBlack())
  49. rightPaddle.SetFillColor(sf.Color{200, 100, 100, 255})
  50. rightPaddle.SetOrigin(sf.Vector2f{paddleSize.X / 2, paddleSize.Y / 2})
  51. // Create the ball
  52. ball, _ := sf.NewCircleShape()
  53. ball.SetRadius(ballRadius - 3)
  54. ball.SetOutlineThickness(3)
  55. ball.SetOutlineColor(sf.ColorBlack())
  56. ball.SetFillColor(sf.ColorWhite())
  57. ball.SetOrigin(sf.Vector2f{ballRadius / 2, ballRadius / 2})
  58. // Load the text font
  59. font, _ := sf.NewFontFromFile("resources/sansation.ttf")
  60. // Initialize the pause message
  61. pauseMessage, _ := sf.NewText(font)
  62. pauseMessage.SetCharacterSize(40)
  63. pauseMessage.SetPosition(sf.Vector2f{170, 150})
  64. pauseMessage.SetColor(sf.ColorWhite())
  65. pauseMessage.SetString("Welcome to SFML pong!\nPress space to start the game")
  66. var (
  67. rightPaddleSpeed float32 = 0
  68. ballAngle float32 = 0
  69. isPlaying bool = false
  70. )
  71. for renderWindow.IsOpen() {
  72. select {
  73. case <-ticker.C:
  74. //poll events
  75. for event := renderWindow.PollEvent(); event != nil; event = renderWindow.PollEvent() {
  76. switch ev := event.(type) {
  77. case sf.EventKeyReleased:
  78. switch ev.Code {
  79. case sf.KeyEscape:
  80. renderWindow.Close()
  81. case sf.KeySpace:
  82. if !isPlaying {
  83. // (re)start the game
  84. isPlaying = true
  85. // reset position of the paddles and ball
  86. leftPaddle.SetPosition(sf.Vector2f{10 + paddleSize.X/2, float32(gameHeight) / 2})
  87. rightPaddle.SetPosition(sf.Vector2f{float32(gameWidth) - 10 - paddleSize.X/2, float32(gameHeight) / 2})
  88. ball.SetPosition(sf.Vector2f{float32(gameWidth) / 2, float32(gameHeight) / 2})
  89. // reset the ball angle
  90. for {
  91. // Make sure the ball initial angle is not too much vertical
  92. ballAngle = rand.Float32() * math.Pi * 2
  93. if math.Abs(math.Cos(float64(ballAngle))) > 0.7 {
  94. break
  95. }
  96. }
  97. }
  98. }
  99. case sf.EventClosed:
  100. renderWindow.Close()
  101. }
  102. }
  103. //playing
  104. if isPlaying {
  105. deltaTime := time.Second / 60
  106. // Move the player's paddle
  107. if sf.KeyboardIsKeyPressed(sf.KeyUp) && leftPaddle.GetPosition().Y-paddleSize.Y/2 > 5 {
  108. leftPaddle.Move(sf.Vector2f{0, -paddleSpeed * float32(deltaTime.Seconds())})
  109. }
  110. if sf.KeyboardIsKeyPressed(sf.KeyDown) && leftPaddle.GetPosition().Y+paddleSize.Y/2 < float32(gameHeight)-5 {
  111. leftPaddle.Move(sf.Vector2f{0, paddleSpeed * float32(deltaTime.Seconds())})
  112. }
  113. // Move the computer's paddle
  114. if (rightPaddleSpeed < 0 && rightPaddle.GetPosition().Y-paddleSize.Y/2 > 5) || (rightPaddleSpeed > 0 && rightPaddle.GetPosition().Y+paddleSize.Y/2 < float32(gameHeight)-5) {
  115. rightPaddle.Move(sf.Vector2f{0, rightPaddleSpeed * float32(deltaTime.Seconds())})
  116. }
  117. // Move the ball
  118. factor := ballSpeed * float32(deltaTime.Seconds())
  119. ball.Move(sf.Vector2f{float32(math.Cos(float64(ballAngle))) * factor, float32(math.Sin(float64(ballAngle))) * factor})
  120. // Check collisions between the ball and the screen
  121. if ball.GetPosition().X-ballRadius < 0 {
  122. isPlaying = false
  123. pauseMessage.SetString("You lost !\nPress space to restart or\nescape to exit")
  124. }
  125. if ball.GetPosition().X+ballRadius > float32(gameWidth) {
  126. isPlaying = false
  127. pauseMessage.SetString("You won !\nPress space to restart or\nescape to exit")
  128. }
  129. if ball.GetPosition().Y-ballRadius < 0 {
  130. ballAngle = -ballAngle
  131. ball.SetPosition(sf.Vector2f{ball.GetPosition().X, ballRadius + 0.1})
  132. ballSound.Play()
  133. }
  134. if ball.GetPosition().Y+ballRadius > float32(gameHeight) {
  135. ballAngle = -ballAngle
  136. ball.SetPosition(sf.Vector2f{ball.GetPosition().X, float32(gameHeight) - ballRadius - 0.1})
  137. ballSound.Play()
  138. }
  139. // Check the collisions between the ball and the paddles
  140. // Left Paddle
  141. if ball.GetPosition().X-ballRadius < leftPaddle.GetPosition().X+paddleSize.X/2 &&
  142. ball.GetPosition().X-ballRadius > leftPaddle.GetPosition().X &&
  143. ball.GetPosition().Y+ballRadius >= leftPaddle.GetPosition().Y-paddleSize.Y/2 &&
  144. ball.GetPosition().Y-ballRadius <= leftPaddle.GetPosition().Y+paddleSize.Y/2 {
  145. if ball.GetPosition().Y > leftPaddle.GetPosition().Y {
  146. ballAngle = math.Pi - ballAngle + rand.Float32()*math.Pi*0.2
  147. } else {
  148. ballAngle = math.Pi - ballAngle - rand.Float32()*math.Pi*0.2
  149. }
  150. ball.SetPosition(sf.Vector2f{leftPaddle.GetPosition().X + ballRadius + paddleSize.X/2 + 0.1, ball.GetPosition().Y})
  151. ballSound.Play()
  152. }
  153. // Right Paddle
  154. if ball.GetPosition().X+ballRadius > rightPaddle.GetPosition().X-paddleSize.X/2 &&
  155. ball.GetPosition().X+ballRadius < rightPaddle.GetPosition().X &&
  156. ball.GetPosition().Y+ballRadius >= rightPaddle.GetPosition().Y-paddleSize.Y/2 &&
  157. ball.GetPosition().Y-ballRadius <= rightPaddle.GetPosition().Y+paddleSize.Y/2 {
  158. if ball.GetPosition().Y > rightPaddle.GetPosition().Y {
  159. ballAngle = math.Pi - ballAngle + rand.Float32()*math.Pi*0.2
  160. } else {
  161. ballAngle = math.Pi - ballAngle - rand.Float32()*math.Pi*0.2
  162. }
  163. ball.SetPosition(sf.Vector2f{rightPaddle.GetPosition().X - ballRadius - paddleSize.X/2 - 0.1, ball.GetPosition().Y})
  164. ballSound.Play()
  165. }
  166. }
  167. // Clear the window
  168. renderWindow.Clear(sf.Color{50, 200, 50, 0})
  169. if isPlaying {
  170. renderWindow.Draw(leftPaddle, sf.DefaultRenderStates())
  171. renderWindow.Draw(rightPaddle, sf.DefaultRenderStates())
  172. renderWindow.Draw(ball, sf.DefaultRenderStates())
  173. } else {
  174. renderWindow.Draw(pauseMessage, sf.DefaultRenderStates())
  175. }
  176. // Display things on screen
  177. renderWindow.Display()
  178. case <-AITicker.C:
  179. if ball.GetPosition().Y+ballRadius > rightPaddle.GetPosition().Y+paddleSize.Y/2 {
  180. rightPaddleSpeed = paddleSpeed
  181. } else if ball.GetPosition().Y-ballRadius < rightPaddle.GetPosition().Y-paddleSize.Y/2 {
  182. rightPaddleSpeed = -paddleSpeed
  183. } else {
  184. rightPaddleSpeed = 0
  185. }
  186. }
  187. }
  188. }