package main

import (
	"math/rand"
	"time"
)

const (
	gameColorsCellSize = 16
	gameColorsSpeed    = 9
	gameColorsWidth    = int16(displayWidth / gameColorsCellSize)
	gameColorsHeight   = int16(displayHeight / gameColorsCellSize)
)

var (
	colorgame [][]bool

	p1x, p1y, p2x, p2y, tx int16
	v1x, v1y, v2x, v2y, ty int16
)

func ColorGame() {
	display.FillScreen(colorOrange)

	colorgame = make([][]bool, gameColorsWidth)

	p1x = int16(rand.Int31n(int32(displayWidth/3))) + displayWidth/12
	p1y = int16(rand.Int31n(int32(4*gameColorsHeight/5))) + gameColorsHeight/10
	p2x = int16(rand.Int31n(int32(displayWidth/3))) + 7*displayWidth/12
	p2y = int16(rand.Int31n(int32(4*gameColorsHeight/5))) + gameColorsHeight/10

	v1x = 2
	v1y = 3
	v2x = -3
	v2y = 2

	for i := int16(0); i < gameColorsWidth; i++ {
		colorgame[i] = make([]bool, gameColorsHeight)
		if i >= gameColorsWidth/2 {
			for j := int16(0); j < gameColorsHeight; j++ {
				colorgame[i][j] = true
				display.FillRectangle(i*gameColorsCellSize, j*gameColorsCellSize, gameColorsCellSize, gameColorsCellSize, colorPurple)
			}
		}
	}

	display.FillRectangle(p1x-gameColorsSpeed, p1y-gameColorsSpeed, gameColorsCellSize, gameColorsCellSize, colorPurple)
	display.FillRectangle(p2x-gameColorsSpeed, p2y-gameColorsSpeed, gameColorsCellSize, gameColorsCellSize, colorOrange)

	gw := (gameColorsWidth * gameColorsCellSize) - gameColorsSpeed
	gh := (gameColorsHeight * gameColorsCellSize) - gameColorsSpeed

	for {
		display.FillRectangle(p1x-gameColorsSpeed, p1y-gameColorsSpeed, gameColorsCellSize, gameColorsCellSize, colorOrange)
		display.FillRectangle(p2x-gameColorsSpeed, p2y-gameColorsSpeed, gameColorsCellSize, gameColorsCellSize, colorPurple)

		p1x += v1x
		p1y += v1y
		p2x += v2x
		p2y += v2y

		if (p1x < gameColorsSpeed && v1x < 0) || (p1x > gw && v1x > 0) {
			v1x = -v1x
		}
		if (p2x < gameColorsSpeed && v2x < 0) || (p2x > gw && v2x > 0) {
			v2x = -v2x
		}
		if (p1y < gameColorsSpeed && v1y < 0) || (p1y > gh && v1y > 0) {
			v1y = -v1y
		}
		if (p2y < gameColorsSpeed && v2y < 0) || (p2y > gh && v2y > 0) {
			v2y = -v2y
		}

		tx = (p1x - gameColorsSpeed) / gameColorsCellSize
		ty = p1y / gameColorsCellSize
		if tx < 0 {
			tx = 0
		}
		if colorgame[tx][ty] {
			colorgame[tx][ty] = false
			if v1x < 0 {
				v1x = -v1x
			}
			display.FillRectangle(tx*gameColorsCellSize, ty*gameColorsCellSize, gameColorsCellSize, gameColorsCellSize, colorOrange)
		}
		tx = (p1x + gameColorsSpeed) / gameColorsCellSize
		if tx >= gameColorsWidth {
			tx = gameColorsWidth - 1
		}
		if colorgame[tx][ty] {
			colorgame[tx][ty] = false
			if v1x > 0 {
				v1x = -v1x
			}
			display.FillRectangle(tx*gameColorsCellSize, ty*gameColorsCellSize, gameColorsCellSize, gameColorsCellSize, colorOrange)
		}
		tx = p1x / gameColorsCellSize
		ty = (p1y - gameColorsSpeed) / gameColorsCellSize
		if ty < 0 {
			ty = 0
		}
		if colorgame[tx][ty] {
			colorgame[tx][ty] = false
			if v1y < 0 {
				v1y = -v1y
			}
			display.FillRectangle(tx*gameColorsCellSize, ty*gameColorsCellSize, gameColorsCellSize, gameColorsCellSize, colorOrange)
		}
		ty = (p1y + gameColorsSpeed) / gameColorsCellSize
		if ty >= gameColorsHeight {
			ty = gameColorsHeight - 1
		}
		if colorgame[tx][ty] {
			colorgame[tx][ty] = false
			if v1y > 0 {
				v1y = -v1y
			}
			display.FillRectangle(tx*gameColorsCellSize, ty*gameColorsCellSize, gameColorsCellSize, gameColorsCellSize, colorOrange)
		}

		// P2
		tx = (p2x - gameColorsSpeed) / gameColorsCellSize
		ty = p2y / gameColorsCellSize
		if tx < 0 {
			tx = 0
		}
		if !colorgame[tx][ty] {
			colorgame[tx][ty] = true
			if v2x < 0 {
				v2x = -v2x
			}
			display.FillRectangle(tx*gameColorsCellSize, ty*gameColorsCellSize, gameColorsCellSize, gameColorsCellSize, colorPurple)
		}
		tx = (p2x + gameColorsSpeed) / gameColorsCellSize
		if tx >= gameColorsWidth {
			tx = gameColorsWidth - 2
		}
		if !colorgame[tx][ty] {
			colorgame[tx][ty] = true
			if v2x > 0 {
				v2x = -v2x
			}
			display.FillRectangle(tx*gameColorsCellSize, ty*gameColorsCellSize, gameColorsCellSize, gameColorsCellSize, colorPurple)
		}
		tx = p2x / gameColorsCellSize
		ty = (p2y - gameColorsSpeed) / gameColorsCellSize
		if ty < 0 {
			ty = 0
		}
		if !colorgame[tx][ty] {
			colorgame[tx][ty] = true
			if v2y < 0 {
				v2y = -v2y
			}
			display.FillRectangle(tx*gameColorsCellSize, ty*gameColorsCellSize, gameColorsCellSize, gameColorsCellSize, colorPurple)
		}
		ty = (p2y + gameColorsSpeed) / gameColorsCellSize
		if ty >= gameColorsHeight {
			ty = gameColorsHeight - 2
		}
		if !colorgame[tx][ty] {
			colorgame[tx][ty] = true
			if v2y > 0 {
				v2y = -v2y
			}
			display.FillRectangle(tx*gameColorsCellSize, ty*gameColorsCellSize, gameColorsCellSize, gameColorsCellSize, colorPurple)
		}

		display.FillRectangle(p1x-gameColorsSpeed, p1y-gameColorsSpeed, gameColorsCellSize, gameColorsCellSize, colorPurple)
		display.FillRectangle(p2x-gameColorsSpeed, p2y-gameColorsSpeed, gameColorsCellSize, gameColorsCellSize, colorOrange)

		getInput()
		if goBack() {
			break
		}

		time.Sleep(10 * time.Millisecond)

	}

}
