Example #1
0
func InitializeGame(width, height int) Game {
	var game Game
	game.Width = width
	game.Height = height

	game.Boundary = new(gt2d.Rectangle)
	game.Boundary.Min = gt2d.Vector2D{0, 0}
	game.Boundary.Max = gt2d.Vector2D{width + 1, height + 1} // correction factor.

	// Initializes the boards.
	// 50 is the height of the boards.
	game.Board1 = NewBoard(gt2d.Vector2D{80, height/2 - 50}, gt2d.Rect(0, 0, width/2+1, height+1))
	game.Board2 = NewBoard(gt2d.Vector2D{width - 90, height/2 - 50}, gt2d.Rect(width/2+21, 0, width+1, height+1))
	game.Bul = NewBullet(game.Boundary)

	game.keysDown = make(map[string]bool)
	game.keysDown[wde.KeyW] = false
	game.keysDown[wde.KeyS] = false
	game.keysDown[wde.KeyD] = false
	game.keysDown[wde.KeyA] = false
	game.keysDown[wde.KeyUpArrow] = false
	game.keysDown[wde.KeyDownArrow] = false
	game.keysDown[wde.KeyRightArrow] = false
	game.keysDown[wde.KeyLeftArrow] = false

	game.Running = true

	return game
}
Example #2
0
func NewBullet(boundary *gt2d.Rectangle) *Bullet {
	rand.Seed(time.Now().UTC().UnixNano())

	bullet := new(Bullet)
	bullet.Boundary = boundary

	// init with a random velocity and direction
	bullet.Velocity = new(gt2d.Vector2D)

	for {
		if abs(bullet.Velocity.X) > 1 && abs(bullet.Velocity.Y) > 1 {
			break
		}
		bullet.Velocity.X = rand.Intn(16) - 8
		bullet.Velocity.Y = rand.Intn(16) - 8
	}

	bullet.CurrentPosition = new(gt2d.Vector2D)
	bullet.LastPosition = new(gt2d.Vector2D)
	bullet.CurrentPosition.X = boundary.Width() / 2
	bullet.CurrentPosition.Y = boundary.Height() / 2
	bullet.LastPosition.X = bullet.CurrentPosition.X
	bullet.LastPosition.Y = bullet.CurrentPosition.Y

	bullet.Rect = gt2d.Rect(bullet.CurrentPosition.X-5, bullet.CurrentPosition.Y-5, bullet.CurrentPosition.X+5, bullet.CurrentPosition.Y+5)
	return bullet
}