func (i *Iceblock) Draw(s *tl.Screen) { // if the iceblock has a direction if i.direction != NONE { // then move it in the direction specified i.time += s.TimeDelta() if i.time > i.update { switch i.direction { case RIGHT: i.r.SetPosition(i.endPos.x+1, i.endPos.y) break case LEFT: i.r.SetPosition(i.endPos.x-1, i.endPos.y) break case UP: i.r.SetPosition(i.endPos.x, i.endPos.y-1) break case DOWN: i.r.SetPosition(i.endPos.x, i.endPos.y+1) } // updated its previous position i.endPos.x, i.endPos.y = i.r.Position() i.time -= i.update } } i.r.Draw(s) }
func (r *RemainingTime) Draw(s *tl.Screen) { if Status == 1 { r.t = math.Max(r.t-s.TimeDelta(), 0) if r.t == 0 { Status = 0 r.e.SetText("Time up!") } else { if Response == 1 { s, _ := strconv.Atoi(r.s.Text()) s = s + 1 if s%5 == 0 { r.t = r.t + 1 } r.s.SetText(strconv.Itoa(s)) switch s { case 10: r.m.SetText("You've got it!") case 20: r.m.SetText("Keep going!") case 30: r.m.SetText("You're doing great!") case 40: r.m.SetText("You rock!") case 50: r.m.SetText("Don't stop!") case 60: r.m.SetText("I like your style!") case 70: r.m.SetText("Awesome!") case 80: r.m.SetText("How do you do that?") case 90: r.m.SetText("Don't ever stop!!") case 100: r.m.SetText("I'm really impressed.") case 150: r.m.SetText("You're really still here?") case 200: r.m.SetText("That's incredible!") } } else if Response == 2 { Status = 0 r.e.SetText("Game Over") } } Response = 0 r.r.SetText(fmt.Sprintf("%.3f", r.t)) } r.r.Draw(s) r.s.Draw(s) r.m.Draw(s) r.e.Draw(s) }
// Handles player movement and food spawn rate. func (player *Player) Update(screen *tl.Screen) { player.snakeTime += screen.TimeDelta() if player.snakeTime > snakeRate { player.snakeTime -= snakeRate player.prevX, player.prevY = player.Position() switch player.direction { case "right": player.SetPosition(player.prevX+1, player.prevY) case "left": player.SetPosition(player.prevX-1, player.prevY) case "up": player.SetPosition(player.prevX, player.prevY-1) case "down": player.SetPosition(player.prevX, player.prevY+1) } player.SnakeMovement() } player.spawnTime += screen.TimeDelta() if player.spawnTime > spawnRate { player.spawnTime -= spawnRate screenWidth, screenHeight := screen.Size() rando := rand.New(rand.NewSource(time.Now().UnixNano())) spawnX, spawnY := rando.Intn(screenWidth), rando.Intn(screenHeight) screen.Level().AddEntity(tl.NewRectangle(spawnX, spawnY, 1, 1, tl.ColorGreen)) game.Log("Spawn at (%d,%d)", spawnX, spawnY) } //Check box boundaries playerX, playerY := player.Position() screenWidth, screenHeight := game.Screen().Size() //<= is used on the upper-boundaries to prevent the player from disappearing offscreen //by one square //(Funnily enough, when player.snake is more than one unit long, just stopping the player at //the boundaries also causes a game over state because the tail slides into the head) if playerX < 0 || playerX >= screenWidth { GameOver() } if playerY < 0 || playerY >= screenHeight { GameOver() } }
//Handles auto events func (player *Player) Update(screen *tl.Screen) { //tl.Screen.size() parameters are evidently zero until the game.Start(), //So this is a crude solution intended to center the player after the game has begun if firstPass { screenWidth, screenHeight := screen.Size() player.SetPosition(screenWidth/2, screenHeight/2) firstPass = false } snakeTime += screen.TimeDelta() if snakeTime > 0.1 { snakeTime -= 0.1 player.prevX, player.prevY = player.Position() switch player.direction { case "right": player.SetPosition(player.prevX+1, player.prevY) //player.SnakeMovement(player.prevX+1, player.prevY) case "left": player.SetPosition(player.prevX-1, player.prevY) //player.SnakeMovement(player.prevX-1, player.prevY) case "up": player.SetPosition(player.prevX, player.prevY-1) //player.SnakeMovement(player.prevX, player.prevY-1) case "down": player.SetPosition(player.prevX, player.prevY+1) //player.SnakeMovement(player.prevX, player.prevY+1) } player.SnakeMovement() } spawnTime += screen.TimeDelta() if spawnTime > 1 { spawnTime -= 1 screenWidth, screenHeight := screen.Size() rando := rand.New(rand.NewSource(time.Now().UnixNano())) spawnX, spawnY := rando.Intn(screenWidth), rando.Intn(screenHeight) screen.Level().AddEntity(tl.NewRectangle(spawnX, spawnY, 1, 1, tl.ColorGreen)) game.Log("Spawn at (%d,%d)", spawnX, spawnY) } //Check box boundaries playerX, playerY := player.Position() screenWidth, screenHeight := game.Screen().Size() //<= is used on the upper-boundaries to prevent the player from disappearing offscreen //by one square //(Funnily enough, when player.snake is more than one unit long, just stopping the player at //the boundaries also causes a game over state because the tail slides into the head) if playerX < 0 || playerX >= screenWidth { GameOver() //player.SetPosition(player.prevX, player.prevY) } if playerY < 0 || playerY >= screenHeight { GameOver() //player.SetPosition(player.prevX, player.prevY) } }