side scrolling shooter
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.go 1.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package main
  2. import (
  3. "fmt"
  4. "git.192k.pw/bake/bullet/controlls"
  5. "github.com/veandco/go-sdl2/sdl"
  6. "log"
  7. "time"
  8. )
  9. func main() {
  10. if err := run(); err != nil {
  11. log.Fatal(err)
  12. }
  13. }
  14. func run() error {
  15. if err := sdl.Init(sdl.INIT_EVERYTHING); err != nil {
  16. return fmt.Errorf("could not init sdl: %v", err)
  17. }
  18. defer sdl.Quit()
  19. w, r, err := sdl.CreateWindowAndRenderer(800, 600, sdl.WINDOW_SHOWN)
  20. if err != nil {
  21. return fmt.Errorf("could not create window: %v", err)
  22. }
  23. defer w.Destroy()
  24. c, err := controlls.NewController(0)
  25. if err != nil {
  26. log.Fatal(err)
  27. }
  28. defer c.Close()
  29. s, err := newScene(r, c)
  30. if err != nil {
  31. log.Println(err)
  32. }
  33. defer s.destroy()
  34. go game(s, r, c)
  35. input(s, r, c)
  36. return nil
  37. }
  38. func game(s *scene, r *sdl.Renderer, c controlls.RelativeControl) error {
  39. select {
  40. case err := <-s.run(r, c):
  41. if err != nil {
  42. return err
  43. }
  44. case <-time.After(5 * time.Second):
  45. return nil
  46. }
  47. return nil
  48. }
  49. func input(s *scene, r *sdl.Renderer, c controlls.RelativeControl) {
  50. for {
  51. switch e := sdl.WaitEvent().(type) {
  52. case *sdl.QuitEvent:
  53. break
  54. default:
  55. c.Update(e)
  56. s.event(e, c)
  57. }
  58. }
  59. }