Merge branch 'master' into 'master'
Refactor; battle; added weapons See merge request mollusk/trinket!1
This commit is contained in:
commit
3630326f44
77
battle.go
77
battle.go
@ -1,7 +1,6 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@ -10,36 +9,78 @@ type Character struct {
|
|||||||
Name string
|
Name string
|
||||||
Health int
|
Health int
|
||||||
Damage int
|
Damage int
|
||||||
|
Weapons [5]Weapon
|
||||||
Spell Spell
|
Spell Spell
|
||||||
|
Initiative int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Battle(hero, enemy Character) {
|
||||||
|
speed := time.Second * 1
|
||||||
|
|
||||||
|
heroDamage := hero.Damage + hero.Weapons[0].Damage
|
||||||
|
enemyDamage := enemy.Damage + enemy.Weapons[0].Damage
|
||||||
|
|
||||||
func Battle(hero, enemy Character, speed time.Duration) {
|
heroInitiative := rand.Intn(7) + hero.Initiative
|
||||||
speed *= time.Second
|
enemyInitiative := rand.Intn(7) + enemy.Initiative
|
||||||
|
|
||||||
rand.Seed(time.Now().Unix())
|
damage := 0
|
||||||
fmt.Println("Random Int: ",rand.Intn(11))
|
|
||||||
//time.Sleep(speed)
|
|
||||||
print("Hero: ",hero.Name, "\n")
|
|
||||||
print("Enemy: ",enemy.Name, "\n")
|
|
||||||
|
|
||||||
if spellOrAttack() == "spell"{
|
for {
|
||||||
print("Spell Damage: ",hero.Spell.Damage, "\n")
|
printCombatantStats(&hero, &enemy, heroInitiative, enemyInitiative)
|
||||||
|
|
||||||
|
if heroInitiative >= enemyInitiative {
|
||||||
|
damage = rand.Intn(heroDamage)
|
||||||
|
enemy.Health -= damage
|
||||||
|
print(enemy.Name, " was hit for ", damage, " damage!\n")
|
||||||
|
|
||||||
|
if enemy.Health > 0 {
|
||||||
|
damage = rand.Intn(enemyDamage)
|
||||||
|
hero.Health -= damage
|
||||||
|
print(hero.Name, " was hit for ", damage, " damage!\n")
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
print("Attack Damage: ", hero.Damage, "\n")
|
damage = rand.Intn(enemyDamage)
|
||||||
}
|
hero.Health -= damage
|
||||||
|
print(hero.Name, " was hit for ", damage, " damage!\n")
|
||||||
|
|
||||||
if enemy.Name == "Goblin" {
|
if hero.Health > 0 {
|
||||||
print("This is a goblin character\n")
|
damage = rand.Intn(heroDamage)
|
||||||
|
enemy.Health -= damage
|
||||||
|
print(enemy.Name, " was hit for ", damage, " damage!\n")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
time.Sleep(speed)
|
||||||
|
|
||||||
func spellOrAttack() string {
|
if enemy.Health <= 0 || hero.Health <= 0 {
|
||||||
if rand.Intn(2) == 1{
|
ClearScreen()
|
||||||
return "spell"
|
printCombatantStats(&hero, &enemy, heroInitiative, enemyInitiative)
|
||||||
|
|
||||||
|
print("It's over\n")
|
||||||
|
if enemy.Health <= 0 {
|
||||||
|
print("The ", enemy.Name, " is dead!\n")
|
||||||
} else {
|
} else {
|
||||||
return "attack"
|
print("Rest in peace ", hero.Name, "\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
break
|
||||||
|
}
|
||||||
|
ClearScreen()
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func flipCoin() string {
|
||||||
|
if rand.Intn(10) < 5 {
|
||||||
|
return "heads"
|
||||||
|
} else {
|
||||||
|
return "tails"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func printCombatantStats(hero, enemy *Character, heroInitiative, enemyInitiative int) {
|
||||||
|
|
||||||
|
print("Hero: ", hero.Name, "\t", "Enemy: ", enemy.Name, "\n")
|
||||||
|
print("Health: ", hero.Health, "\t", "Health: ", enemy.Health, "\n")
|
||||||
|
print("Initiative: ", heroInitiative, "\t", "Initiative: ", enemyInitiative, "\n\n")
|
||||||
|
}
|
||||||
|
20
characters.go
Normal file
20
characters.go
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
var (
|
||||||
|
player = Character{
|
||||||
|
|
||||||
|
Name: "Daniel",
|
||||||
|
Health: 100,
|
||||||
|
Damage: 30,
|
||||||
|
Spell: firearrow,
|
||||||
|
Weapons: [5]Weapon{Broadsword},
|
||||||
|
Initiative: 4}
|
||||||
|
|
||||||
|
goblin = Character{
|
||||||
|
|
||||||
|
Name: "Goblin",
|
||||||
|
Health: 100,
|
||||||
|
Damage: 20,
|
||||||
|
Weapons: [5]Weapon{GoblinClaw},
|
||||||
|
Initiative: 3}
|
||||||
|
)
|
10
magic.go
10
magic.go
@ -7,5 +7,13 @@ type Spell struct {
|
|||||||
Cost int
|
Cost int
|
||||||
Level int
|
Level int
|
||||||
Active bool
|
Active bool
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
firearrow = Spell{
|
||||||
|
Name: "Fire Arrow",
|
||||||
|
Damage: 50,
|
||||||
|
Cost: 10,
|
||||||
|
Level: 1,
|
||||||
|
Active: true}
|
||||||
|
)
|
||||||
|
82
main.go
82
main.go
@ -1,29 +1,63 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
//Characters
|
"fmt"
|
||||||
var (
|
"math/rand"
|
||||||
player = Character{
|
"os"
|
||||||
|
"os/exec"
|
||||||
Name: "Daniel",
|
"time"
|
||||||
Health: 100,
|
|
||||||
Damage: 60,
|
|
||||||
Spell: firearrow}
|
|
||||||
|
|
||||||
goblin = Character{
|
|
||||||
|
|
||||||
Name: "Goblin",
|
|
||||||
Health: 100,
|
|
||||||
Damage: 20}
|
|
||||||
|
|
||||||
firearrow = Spell{
|
|
||||||
Name: "Fire Arrow",
|
|
||||||
Damage: 50,
|
|
||||||
Cost: 10,
|
|
||||||
Level: 1,
|
|
||||||
Active: true}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
Battle(player, goblin, 1)
|
initGame()
|
||||||
|
var answer string
|
||||||
|
|
||||||
|
for {
|
||||||
|
print("Type 'q' to quit\n")
|
||||||
|
answer, _ = input("Would you like to fight a goblin? y/n ")
|
||||||
|
|
||||||
|
if answer == "n" {
|
||||||
|
print("What a pussy! \n")
|
||||||
|
time.Sleep(time.Second)
|
||||||
|
ClearScreen()
|
||||||
|
} else if answer == "y" {
|
||||||
|
print("Sweeeeeet! \n")
|
||||||
|
time.Sleep(time.Second)
|
||||||
|
ClearScreen()
|
||||||
|
Battle(player, goblin)
|
||||||
|
} else if answer == "q" {
|
||||||
|
ClearScreen()
|
||||||
|
print("Ta!\n")
|
||||||
|
break
|
||||||
|
} else {
|
||||||
|
print("Try again, \n")
|
||||||
|
time.Sleep(time.Second)
|
||||||
|
ClearScreen()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func initGame() {
|
||||||
|
rand.Seed(time.Now().UnixNano())
|
||||||
|
ClearScreen()
|
||||||
|
}
|
||||||
|
|
||||||
|
func ClearScreen() {
|
||||||
|
cmd := exec.Command("clear")
|
||||||
|
cmd.Stdout = os.Stdout
|
||||||
|
cmd.Run()
|
||||||
|
}
|
||||||
|
|
||||||
|
func input(s string) (string, error) {
|
||||||
|
stdin_buf := bufio.NewReader(os.Stdin)
|
||||||
|
|
||||||
|
fmt.Print(s)
|
||||||
|
|
||||||
|
text, err := stdin_buf.ReadString('\n')
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return text[:len(text)-1], err
|
||||||
}
|
}
|
18
weapons.go
Normal file
18
weapons.go
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
type Weapon struct {
|
||||||
|
Name string
|
||||||
|
Damage int
|
||||||
|
Active bool
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
Broadsword = Weapon{
|
||||||
|
Name: "Broadsword",
|
||||||
|
Damage: 3,
|
||||||
|
Active: false}
|
||||||
|
|
||||||
|
GoblinClaw = Weapon{
|
||||||
|
Name: "Goblin Claw",
|
||||||
|
Damage: 5}
|
||||||
|
)
|
Loading…
x
Reference in New Issue
Block a user