From f3b7713a87983f49781e773dcbb32ac82cb6d8db Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Fri, 8 Jan 2016 15:44:35 -0600 Subject: [PATCH] THE GAME WORKS --- screen_main.go | 115 ++++++++++++++++++++++++++++++------------------ screen_title.go | 21 ++++++--- 2 files changed, 87 insertions(+), 49 deletions(-) diff --git a/screen_main.go b/screen_main.go index 2a1c9d4..95fa62b 100644 --- a/screen_main.go +++ b/screen_main.go @@ -49,8 +49,8 @@ func (screen *mainScreen) handleKeyPress(event termbox.Event) int { } else if selOpt.GetText() == "Resume" { screen.GameMode = modeRun } else if selOpt.GetText() == "Restart" { - //screen.GameMode = modeInit - //return titleScreenIndex + screen.GameMode = modeInit + return titleScreenIndex } } } @@ -64,13 +64,9 @@ func (screen *mainScreen) handleKeyPress(event termbox.Event) int { screen.animating = true screen.GameMode = modeRun } else if event.Key == termbox.KeyArrowLeft || event.Ch == 'a' { - if p.projAngle < 180 { - p.projAngle++ - } + p.projAngle-- } else if event.Key == termbox.KeyArrowRight || event.Ch == 'd' { - if p.projAngle > 0 { - p.projAngle-- - } + p.projAngle++ } else if event.Key == termbox.KeyArrowUp || event.Ch == 'w' { p.projSpeed++ } else if event.Key == termbox.KeyArrowDown || event.Ch == 's' { @@ -88,11 +84,11 @@ func (screen *mainScreen) performLayout(style style) { b := projectile{gravity: screen.gravity} screen.Bullet = &b // TODO: Reset Buildings - //screen.Buildings = []building{} + screen.Buildings = []building{} // Create a random seed (from time) seed := fmt.Sprintf("%d", time.Now().UnixNano()) 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 { w := screen.r.Intn(8) + 6 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) p1x, p1y := screen.getRndPosForBuilding(bld) screen.Player1 = createPlayer(p1x, p1y) + screen.Player1.gravity = screen.gravity // 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) @@ -124,7 +121,8 @@ func (screen *mainScreen) performLayout(style style) { p2x, p2y = screen.getRndPosForBuilding(bld - 1) } screen.Player2 = createPlayer(p2x, p2y) - screen.Player2.projAngle = 135 + screen.Player2.projAngle = 225 + screen.Player2.gravity = screen.gravity screen.PauseMenu = termboxUtil.CreateMenu("** GOPHER BATTLE **", []string{"Resume", "Restart", "Exit"}, @@ -136,7 +134,13 @@ func (screen *mainScreen) performLayout(style style) { } 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 } @@ -146,8 +150,12 @@ func (screen *mainScreen) update() { if !screen.animating { if screen.PlayerTurn == 1 { screen.PlayerTurn = 2 + screen.Player1.turn = false + screen.Player2.turn = true } else { screen.PlayerTurn = 1 + screen.Player1.turn = true + screen.Player2.turn = false } screen.GameMode = modeRunInput } else { @@ -155,6 +163,7 @@ func (screen *mainScreen) update() { screen.Bullet.update() // Check for collisions if screen.Player1.didCollide(screen.Bullet) { + screen.Player1.dead = true screen.PauseMenu.SetOptionDisabled(0) if screen.PlayerTurn == 1 { // Self kill @@ -166,6 +175,7 @@ func (screen *mainScreen) update() { screen.GameMode = modePause } if screen.Player2.didCollide(screen.Bullet) { + screen.Player2.dead = true screen.PauseMenu.SetOptionDisabled(0) if screen.PlayerTurn == 2 { // Self kill @@ -276,7 +286,7 @@ func (p *projectile) update() { tm := time.Since(p.launchTime).Seconds() useSpeed := float64(p.speed) 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.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)) @@ -290,7 +300,10 @@ type player struct { x, y int projSpeed int projAngle int + gravity float64 baseColor termbox.Attribute + turn bool + dead bool } 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, 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+3, p.y-3, '@', termbox.ColorBlack, termbox.ColorWhite) 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+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) // Now draw the arms - bulletHandX, bulletHandY := p.GetBulletHandPos() - termbox.SetCell(bulletHandX, bulletHandY, 'O', termbox.ColorBlack, p.baseColor) + if p.turn { + bulletHandX, bulletHandY := p.GetBulletHandPos() + termbox.SetCell(bulletHandX, bulletHandY, '+', termbox.ColorBlack, p.baseColor) + } } func (p *player) didCollide(pr *projectile) bool { @@ -339,32 +359,41 @@ func (p *player) didCollide(pr *projectile) bool { } func (p *player) GetBulletHandPos() (int, int) { - if p.projAngle <= 11 { - return p.x + 5, p.y - 2 - } else if p.projAngle <= 22 { - return p.x + 5, p.y - 3 - } else if p.projAngle <= 41 { - return p.x + 5, p.y - 4 - } else if p.projAngle <= 50 { - return p.x + 5, p.y - 5 - } else if p.projAngle <= 77 { - return p.x + 4, p.y - 5 - } else if p.projAngle <= 86 { - return p.x + 3, p.y - 5 - } else if p.projAngle <= 95 { - return p.x + 2, p.y - 5 - } else if p.projAngle <= 119 { - return p.x + 1, p.y - 5 - } else if p.projAngle <= 130 { - return p.x, p.y - 5 - } else if p.projAngle <= 141 { - return p.x - 1, p.y - 5 - } else if p.projAngle <= 152 { - return p.x - 1, p.y - 4 - } else if p.projAngle <= 165 { - return p.x - 1, p.y - 3 - } - return p.x - 1, p.y - 2 + 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 { + return p.x + 5, p.y - 2 + } else if p.projAngle <= 22 { + return p.x + 5, p.y - 3 + } else if p.projAngle <= 41 { + return p.x + 5, p.y - 4 + } else if p.projAngle <= 50 { + return p.x + 5, p.y - 5 + } else if p.projAngle <= 77 { + return p.x + 4, p.y - 5 + } else if p.projAngle <= 86 { + return p.x + 3, p.y - 5 + } else if p.projAngle <= 95 { + return p.x + 2, p.y - 5 + } else if p.projAngle <= 119 { + return p.x + 1, p.y - 5 + } else if p.projAngle <= 130 { + return p.x, p.y - 5 + } else if p.projAngle <= 141 { + return p.x - 1, p.y - 5 + } else if p.projAngle <= 152 { + return p.x - 1, p.y - 4 + } else if p.projAngle <= 165 { + return p.x - 1, p.y - 3 + } + return p.x - 1, p.y - 2 + */ } type building struct { diff --git a/screen_title.go b/screen_title.go index 6d0baf1..5c650c7 100644 --- a/screen_title.go +++ b/screen_title.go @@ -34,12 +34,21 @@ func (screen *titleScreen) handleKeyPress(event termbox.Event) int { func (screen *titleScreen) performLayout(style style) { if !screen.initialized { var tmplt []string - tmplt = append(tmplt, " ") - tmplt = append(tmplt, " ") - tmplt = append(tmplt, " gopher.bas ") - tmplt = append(tmplt, " ") - tmplt = append(tmplt, " ") - tmplt = append(tmplt, " ") + 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, " Gopher Battle ") + tmplt = append(tmplt, " ") + tmplt = append(tmplt, " ") + tmplt = append(tmplt, " ") + } defaultFg := style.defaultFg defaultBg := style.defaultBg