Merge branch 'master' into 'master'
Refactor; battle; added weapons See merge request mollusk/trinket!1
This commit is contained in:
commit
3630326f44
95
battle.go
95
battle.go
@ -1,45 +1,86 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Character struct {
|
||||
Name string
|
||||
Health int
|
||||
Damage int
|
||||
Spell Spell
|
||||
Name string
|
||||
Health int
|
||||
Damage int
|
||||
Weapons [5]Weapon
|
||||
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) {
|
||||
speed *= time.Second
|
||||
heroInitiative := rand.Intn(7) + hero.Initiative
|
||||
enemyInitiative := rand.Intn(7) + enemy.Initiative
|
||||
|
||||
rand.Seed(time.Now().Unix())
|
||||
fmt.Println("Random Int: ",rand.Intn(11))
|
||||
//time.Sleep(speed)
|
||||
print("Hero: ",hero.Name, "\n")
|
||||
print("Enemy: ",enemy.Name, "\n")
|
||||
damage := 0
|
||||
|
||||
if spellOrAttack() == "spell"{
|
||||
print("Spell Damage: ",hero.Spell.Damage, "\n")
|
||||
}else{
|
||||
print("Attack Damage: ", hero.Damage, "\n")
|
||||
}
|
||||
for {
|
||||
printCombatantStats(&hero, &enemy, heroInitiative, enemyInitiative)
|
||||
|
||||
if enemy.Name == "Goblin" {
|
||||
print("This is a goblin character\n")
|
||||
}
|
||||
}
|
||||
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 {
|
||||
damage = rand.Intn(enemyDamage)
|
||||
hero.Health -= damage
|
||||
print(hero.Name, " was hit for ", damage, " damage!\n")
|
||||
|
||||
func spellOrAttack() string {
|
||||
if rand.Intn(2) == 1{
|
||||
return "spell"
|
||||
}else{
|
||||
return "attack"
|
||||
if hero.Health > 0 {
|
||||
damage = rand.Intn(heroDamage)
|
||||
enemy.Health -= damage
|
||||
print(enemy.Name, " was hit for ", damage, " damage!\n")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
time.Sleep(speed)
|
||||
|
||||
if enemy.Health <= 0 || hero.Health <= 0 {
|
||||
ClearScreen()
|
||||
printCombatantStats(&hero, &enemy, heroInitiative, enemyInitiative)
|
||||
|
||||
print("It's over\n")
|
||||
if enemy.Health <= 0 {
|
||||
print("The ", enemy.Name, " is dead!\n")
|
||||
} else {
|
||||
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}
|
||||
)
|
16
magic.go
16
magic.go
@ -2,10 +2,18 @@ package main
|
||||
|
||||
//Spell contains all the magic spells
|
||||
type Spell struct {
|
||||
Name string
|
||||
Name string
|
||||
Damage int
|
||||
Cost int
|
||||
Level int
|
||||
Cost int
|
||||
Level int
|
||||
Active bool
|
||||
|
||||
}
|
||||
|
||||
var (
|
||||
firearrow = Spell{
|
||||
Name: "Fire Arrow",
|
||||
Damage: 50,
|
||||
Cost: 10,
|
||||
Level: 1,
|
||||
Active: true}
|
||||
)
|
||||
|
86
main.go
86
main.go
@ -1,29 +1,63 @@
|
||||
package main
|
||||
|
||||
|
||||
|
||||
//Characters
|
||||
var (
|
||||
player = Character{
|
||||
|
||||
Name: "Daniel",
|
||||
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}
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"os"
|
||||
"os/exec"
|
||||
"time"
|
||||
)
|
||||
func main(){
|
||||
Battle(player, goblin, 1)
|
||||
}
|
||||
|
||||
func main() {
|
||||
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