Created
November 13, 2017 19:32
-
-
Save jakelacey2012/f2457b940f518f60a4c113ece950ae3f to your computer and use it in GitHub Desktop.
Functional game loop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
type coords struct { | |
x int | |
y int | |
} | |
type apple struct { | |
position coords | |
} | |
type gameState struct { | |
apple apple | |
} | |
func logic(state *gameState) { | |
state.apple.position.x = state.apple.position.x + 1 | |
} | |
func gameLoop(running bool, state *gameState) bool { | |
if running == false { | |
return false | |
} | |
logic(state) | |
return gameLoop(running, state) | |
} | |
func main() { | |
// set the initial game state | |
state := gameState{ | |
apple: apple{ | |
position: coords{ | |
x: 2, | |
y: 2, | |
}, | |
}, | |
} | |
gameLoop(true, &state) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment