本文整理匯總了Golang中github.com/JoelOtter/termloop.Screen.Level方法的典型用法代碼示例。如果您正苦於以下問題:Golang Screen.Level方法的具體用法?Golang Screen.Level怎麽用?Golang Screen.Level使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/JoelOtter/termloop.Screen
的用法示例。
在下文中一共展示了Screen.Level方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Update
// 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()
}
}
示例2: Update
//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)
}
}