Exemple #1
0
func main() {
	rand.Seed(time.Now().UnixNano())

	vm := chip8.NewInterpreter()
	vm.SetRandomizer(randomizer{})

	speakers := &speakers{}
	vm.SetSpeakers(speakers)

	keyNames := []string{
		"0", "1", "2", "3",
		"4", "5", "6", "7",
		"8", "9", "a", "s",
		"d", "f", "g", "h",
	}
	keyboard := &keyboard{
		window:   nil,
		keyNames: keyNames,
	}
	vm.SetKeyboard(keyboard)

	keyEvent := &keyEvent{
		keyNames: keyNames,
		last:     -1,
	}
	vm.SetKeyEvent(keyEvent)

	vm.LoadProgram(games.Blitz.Program)

	draw.RunWindow("GO 8", 640, 320, func(window *draw.Window) {
		if window.WasKeyPressed("escape") {
			window.Close()
			return
		}

		speakers.window = window
		keyboard.window = window
		keyEvent.update(window)

		const instructionsPerFrame = 100
		for i := 0; i < instructionsPerFrame; i++ {
			vm.ExecuteNext()
		}
		vm.TimerTick()

		drawScreen(window, vm.Screen())
	})
}
Exemple #2
0
func main() {
	g := game.New([]game.Color{game.Red, game.Blue, game.White}, rand.Int)
	state := buildingSettlement
	g.Players[0].Roads[0].Position = game.TileEdge{9, 2}

	draw.RunWindow("Settlers", 1400, 1000, draw.Resizable, func(window draw.Window) {
		if window.WasKeyPressed("escape") {
			window.Close()
		}

		if window.WasKeyPressed("s") {
			state = buildingSettlement
		}
		if window.WasKeyPressed("r") {
			state = buildingRoad
		}
		if window.WasKeyPressed("c") {
			state = buildingCity
		}

		for i := 1; i <= 4; i++ {
			if window.WasKeyPressed(strconv.Itoa(i)) {
				fmt.Println(g.CurrentPlayer, "->", i)
				g.CurrentPlayer = i - 1
			}
		}

		if state == buildingSettlement || state == buildingCity || state == buildingRoad {
			canBuild := g.CanPlayerBuildSettlement
			build := g.BuildSettlement
			screenToTile := screenToCorner
			if state == buildingCity {
				canBuild = g.CanPlayerBuildCity
				build = g.BuildCity
			}
			if state == buildingRoad {
				canBuild = g.CanPlayerBuildRoad
				build = g.BuildRoad
				screenToTile = screenToEdge
			}
			if canBuild() {
				for _, click := range window.Clicks() {
					if click.Button == draw.LeftButton {
						corner, ok := screenToTile(click.X, click.Y, 40)
						if ok {
							build(corner)
						} else {
							fmt.Println("miss")
						}
					}
				}
			}
		}

		for i := 2; i <= 12; i++ {
			// TODO what about 7?
			if window.WasKeyPressed(strconv.Itoa(i)) {
				g.DealResources(i)
			}
		}

		drawGame(g, window)
		if state == buildingSettlement || state == buildingCity {
			mx, my := window.MousePosition()
			corner, hit := screenToCorner(mx, my, 40)
			if hit {
				x, y := cornerToScreen(corner)
				color := currentPlayerColor(g)
				color.A = 0.75
				window.FillEllipse(x-40, y-40, 80, 80, color)
				if state == buildingSettlement && !g.CanBuildSettlementAt(game.TileCorner(corner)) {
					color.A = 1
					window.DrawLine(x-50, y-50, x+50, y+50, color)
					window.DrawLine(x-50, y+50, x+50, y-50, color)
				}
			}
		}
		if state == buildingRoad {
			mx, my := window.MousePosition()
			edge, hit := screenToEdge(mx, my, 40)
			if hit {
				x, y := edgeToScreen(game.TileEdge(edge))
				color := currentPlayerColor(g)
				color.A = 0.75
				window.FillEllipse(x-40, y-40, 80, 80, color)
				if !g.CanBuildRoadAt(game.TileEdge(edge)) {
					color.A = 1
					window.DrawLine(x-50, y-50, x+50, y+50, color)
					window.DrawLine(x-50, y+50, x+50, y-50, color)
				}
			}
		}
	})
}