THE GAME WORKS

This commit is contained in:
Brian Buller 2016-01-08 15:44:35 -06:00
parent 146573f56c
commit f3b7713a87
2 changed files with 87 additions and 49 deletions

View File

@ -49,8 +49,8 @@ func (screen *mainScreen) handleKeyPress(event termbox.Event) int {
} else if selOpt.GetText() == "Resume" { } else if selOpt.GetText() == "Resume" {
screen.GameMode = modeRun screen.GameMode = modeRun
} else if selOpt.GetText() == "Restart" { } else if selOpt.GetText() == "Restart" {
//screen.GameMode = modeInit screen.GameMode = modeInit
//return titleScreenIndex return titleScreenIndex
} }
} }
} }
@ -64,13 +64,9 @@ func (screen *mainScreen) handleKeyPress(event termbox.Event) int {
screen.animating = true screen.animating = true
screen.GameMode = modeRun screen.GameMode = modeRun
} else if event.Key == termbox.KeyArrowLeft || event.Ch == 'a' { } else if event.Key == termbox.KeyArrowLeft || event.Ch == 'a' {
if p.projAngle < 180 {
p.projAngle++
}
} else if event.Key == termbox.KeyArrowRight || event.Ch == 'd' {
if p.projAngle > 0 {
p.projAngle-- p.projAngle--
} } else if event.Key == termbox.KeyArrowRight || event.Ch == 'd' {
p.projAngle++
} else if event.Key == termbox.KeyArrowUp || event.Ch == 'w' { } else if event.Key == termbox.KeyArrowUp || event.Ch == 'w' {
p.projSpeed++ p.projSpeed++
} else if event.Key == termbox.KeyArrowDown || event.Ch == 's' { } else if event.Key == termbox.KeyArrowDown || event.Ch == 's' {
@ -88,11 +84,11 @@ func (screen *mainScreen) performLayout(style style) {
b := projectile{gravity: screen.gravity} b := projectile{gravity: screen.gravity}
screen.Bullet = &b screen.Bullet = &b
// TODO: Reset Buildings // TODO: Reset Buildings
//screen.Buildings = []building{} screen.Buildings = []building{}
// Create a random seed (from time) // Create a random seed (from time)
seed := fmt.Sprintf("%d", time.Now().UnixNano()) seed := fmt.Sprintf("%d", time.Now().UnixNano())
screen.r = rand.New(rand.NewSource(getSeedFromString(seed + "-world"))) screen.r = rand.New(rand.NewSource(getSeedFromString(seed + "-world")))
screen.PlayerTurn = screen.r.Intn(1) + 1 screen.PlayerTurn = (screen.r.Int() % 2) + 1
for bldCity < ScreenWidth { for bldCity < ScreenWidth {
w := screen.r.Intn(8) + 6 w := screen.r.Intn(8) + 6
h := screen.r.Intn(ScreenHeight-10) + 2 h := screen.r.Intn(ScreenHeight-10) + 2
@ -116,6 +112,7 @@ func (screen *mainScreen) performLayout(style style) {
bld := screen.r.Intn(len(screen.Buildings) / 3) bld := screen.r.Intn(len(screen.Buildings) / 3)
p1x, p1y := screen.getRndPosForBuilding(bld) p1x, p1y := screen.getRndPosForBuilding(bld)
screen.Player1 = createPlayer(p1x, p1y) screen.Player1 = createPlayer(p1x, p1y)
screen.Player1.gravity = screen.gravity
// Player 2 should be on the third 1/3 of the buildings // Player 2 should be on the third 1/3 of the buildings
bld = screen.r.Intn(len(screen.Buildings)/3) + ((len(screen.Buildings) * 2) / 3) bld = screen.r.Intn(len(screen.Buildings)/3) + ((len(screen.Buildings) * 2) / 3)
@ -124,7 +121,8 @@ func (screen *mainScreen) performLayout(style style) {
p2x, p2y = screen.getRndPosForBuilding(bld - 1) p2x, p2y = screen.getRndPosForBuilding(bld - 1)
} }
screen.Player2 = createPlayer(p2x, p2y) screen.Player2 = createPlayer(p2x, p2y)
screen.Player2.projAngle = 135 screen.Player2.projAngle = 225
screen.Player2.gravity = screen.gravity
screen.PauseMenu = termboxUtil.CreateMenu("** GOPHER BATTLE **", screen.PauseMenu = termboxUtil.CreateMenu("** GOPHER BATTLE **",
[]string{"Resume", "Restart", "Exit"}, []string{"Resume", "Restart", "Exit"},
@ -136,7 +134,13 @@ func (screen *mainScreen) performLayout(style style) {
} }
func createPlayer(px, py int) *player { func createPlayer(px, py int) *player {
p := player{x: px, y: py, projSpeed: 10, projAngle: 45, baseColor: termbox.ColorCyan} p := player{
x: px,
y: py,
projSpeed: 10,
projAngle: 315,
baseColor: termbox.ColorCyan,
}
return &p return &p
} }
@ -146,8 +150,12 @@ func (screen *mainScreen) update() {
if !screen.animating { if !screen.animating {
if screen.PlayerTurn == 1 { if screen.PlayerTurn == 1 {
screen.PlayerTurn = 2 screen.PlayerTurn = 2
screen.Player1.turn = false
screen.Player2.turn = true
} else { } else {
screen.PlayerTurn = 1 screen.PlayerTurn = 1
screen.Player1.turn = true
screen.Player2.turn = false
} }
screen.GameMode = modeRunInput screen.GameMode = modeRunInput
} else { } else {
@ -155,6 +163,7 @@ func (screen *mainScreen) update() {
screen.Bullet.update() screen.Bullet.update()
// Check for collisions // Check for collisions
if screen.Player1.didCollide(screen.Bullet) { if screen.Player1.didCollide(screen.Bullet) {
screen.Player1.dead = true
screen.PauseMenu.SetOptionDisabled(0) screen.PauseMenu.SetOptionDisabled(0)
if screen.PlayerTurn == 1 { if screen.PlayerTurn == 1 {
// Self kill // Self kill
@ -166,6 +175,7 @@ func (screen *mainScreen) update() {
screen.GameMode = modePause screen.GameMode = modePause
} }
if screen.Player2.didCollide(screen.Bullet) { if screen.Player2.didCollide(screen.Bullet) {
screen.Player2.dead = true
screen.PauseMenu.SetOptionDisabled(0) screen.PauseMenu.SetOptionDisabled(0)
if screen.PlayerTurn == 2 { if screen.PlayerTurn == 2 {
// Self kill // Self kill
@ -276,7 +286,7 @@ func (p *projectile) update() {
tm := time.Since(p.launchTime).Seconds() tm := time.Since(p.launchTime).Seconds()
useSpeed := float64(p.speed) useSpeed := float64(p.speed)
useX, useY := float64(p.startX), float64(p.startY) useX, useY := float64(p.startX), float64(p.startY)
useA := float64(p.angle) useA := degToRad(p.angle)
p.x = int(useX + (useSpeed * math.Cos(useA) * tm)) p.x = int(useX + (useSpeed * math.Cos(useA) * tm))
p.y = int(useY + (useSpeed*math.Sin(useA)*tm - p.gravity*tm*tm/2)) p.y = int(useY + (useSpeed*math.Sin(useA)*tm - p.gravity*tm*tm/2))
WriteToLog(fmt.Sprintf("%f - S:%d; (%d,%d)=>(%d,%d)\n", useA, p.speed, p.startX, p.startY, p.x, p.y)) WriteToLog(fmt.Sprintf("%f - S:%d; (%d,%d)=>(%d,%d)\n", useA, p.speed, p.startX, p.startY, p.x, p.y))
@ -290,7 +300,10 @@ type player struct {
x, y int x, y int
projSpeed int projSpeed int
projAngle int projAngle int
gravity float64
baseColor termbox.Attribute baseColor termbox.Attribute
turn bool
dead bool
} }
func (p *player) draw() { func (p *player) draw() {
@ -301,10 +314,15 @@ func (p *player) draw() {
termbox.SetCell(p.x+4, p.y-4, ' ', p.baseColor, p.baseColor) termbox.SetCell(p.x+4, p.y-4, ' ', p.baseColor, p.baseColor)
termbox.SetCell(p.x, p.y-3, ' ', p.baseColor, p.baseColor) termbox.SetCell(p.x, p.y-3, ' ', p.baseColor, p.baseColor)
termbox.SetCell(p.x+1, p.y-3, '@', termbox.ColorBlack, termbox.ColorWhite)
termbox.SetCell(p.x+2, p.y-3, ' ', p.baseColor, p.baseColor) termbox.SetCell(p.x+2, p.y-3, ' ', p.baseColor, p.baseColor)
termbox.SetCell(p.x+3, p.y-3, '@', termbox.ColorBlack, termbox.ColorWhite)
termbox.SetCell(p.x+4, p.y-3, ' ', p.baseColor, p.baseColor) termbox.SetCell(p.x+4, p.y-3, ' ', p.baseColor, p.baseColor)
if p.dead {
termbox.SetCell(p.x+1, p.y-3, 'X', termbox.ColorRed, termbox.ColorBlack)
termbox.SetCell(p.x+3, p.y-3, 'X', termbox.ColorRed, termbox.ColorBlack)
} else {
termbox.SetCell(p.x+1, p.y-3, '@', termbox.ColorBlack, termbox.ColorWhite)
termbox.SetCell(p.x+3, p.y-3, '@', termbox.ColorBlack, termbox.ColorWhite)
}
termbox.SetCell(p.x, p.y-2, ' ', p.baseColor, p.baseColor) termbox.SetCell(p.x, p.y-2, ' ', p.baseColor, p.baseColor)
termbox.SetCell(p.x+1, p.y-2, ' ', p.baseColor, p.baseColor) termbox.SetCell(p.x+1, p.y-2, ' ', p.baseColor, p.baseColor)
@ -325,8 +343,10 @@ func (p *player) draw() {
termbox.SetCell(p.x+4, p.y, ' ', p.baseColor, p.baseColor) termbox.SetCell(p.x+4, p.y, ' ', p.baseColor, p.baseColor)
// Now draw the arms // Now draw the arms
if p.turn {
bulletHandX, bulletHandY := p.GetBulletHandPos() bulletHandX, bulletHandY := p.GetBulletHandPos()
termbox.SetCell(bulletHandX, bulletHandY, 'O', termbox.ColorBlack, p.baseColor) termbox.SetCell(bulletHandX, bulletHandY, '+', termbox.ColorBlack, p.baseColor)
}
} }
func (p *player) didCollide(pr *projectile) bool { func (p *player) didCollide(pr *projectile) bool {
@ -339,6 +359,14 @@ func (p *player) didCollide(pr *projectile) bool {
} }
func (p *player) GetBulletHandPos() (int, int) { func (p *player) GetBulletHandPos() (int, int) {
tm := 0.5
useSpeed := float64(p.projSpeed)
useX, useY := float64(p.x+2), float64(p.y-2)
useA := degToRad(p.projAngle)
retX := int(useX + (useSpeed * math.Cos(useA) * tm))
retY := int(useY + (useSpeed*math.Sin(useA)*tm - p.gravity*tm*tm/2))
return retX, retY
/*
if p.projAngle <= 11 { if p.projAngle <= 11 {
return p.x + 5, p.y - 2 return p.x + 5, p.y - 2
} else if p.projAngle <= 22 { } else if p.projAngle <= 22 {
@ -365,6 +393,7 @@ func (p *player) GetBulletHandPos() (int, int) {
return p.x - 1, p.y - 3 return p.x - 1, p.y - 3
} }
return p.x - 1, p.y - 2 return p.x - 1, p.y - 2
*/
} }
type building struct { type building struct {

View File

@ -34,12 +34,21 @@ func (screen *titleScreen) handleKeyPress(event termbox.Event) int {
func (screen *titleScreen) performLayout(style style) { func (screen *titleScreen) performLayout(style style) {
if !screen.initialized { if !screen.initialized {
var tmplt []string var tmplt []string
if ScreenWidth >= 87 {
tmplt = append(tmplt, " ________ .__ __________ __ __ .__ ")
tmplt = append(tmplt, " / _____/ ____ ______ | |__ ___________ \\______ _____ _/ |__/ |_| | ____ ")
tmplt = append(tmplt, "/ \\ ___ / _ \\\\____ \\| | \\_/ __ \\_ __ \\ | | _\\__ \\\\ __\\ __| | _/ __ \\ ")
tmplt = append(tmplt, "\\ \\_\\ ( <_> | |_> | Y \\ ___/| | \\/ | | \\/ __ \\| | | | | |_\\ ___/ ")
tmplt = append(tmplt, " \\______ /\\____/| __/|___| /\\___ |__| |______ (____ |__| |__| |____/\\___ >")
tmplt = append(tmplt, " \\/ |__| \\/ \\/ \\/ \\/ \\/ ")
} else {
tmplt = append(tmplt, " ") tmplt = append(tmplt, " ")
tmplt = append(tmplt, " ") tmplt = append(tmplt, " ")
tmplt = append(tmplt, " gopher.bas ") tmplt = append(tmplt, " Gopher Battle ")
tmplt = append(tmplt, " ") tmplt = append(tmplt, " ")
tmplt = append(tmplt, " ") tmplt = append(tmplt, " ")
tmplt = append(tmplt, " ") tmplt = append(tmplt, " ")
}
defaultFg := style.defaultFg defaultFg := style.defaultFg
defaultBg := style.defaultBg defaultBg := style.defaultBg