Start of my first python project

+6 Mason Farina · August 31, 2014

import random
stats = [
["Name:","placeholder","Level:",1,"Experience:",0,"Strength:",0],
]
enemyStats = [
["Level:",1,"Strength:",0],
]
stats[0][7] = random.randrange(10,20)
enemyStats[0][3] = random.randrange(10,20)
name = input("Please enter a name for your character: ")
stats[0][1] = name
if name == 'debug':
enemyStats[0][3] = 0
print(stats[0][0],stats[0][1],stats[0][2],stats[0][3],stats[0][4],stats[0][5],stats[0][6],stats[0][7])
_stats = input("These are your stats, type 'reroll' to re-roll your stats or 'done' to finish: ")
if _stats == "reroll":
stats[0][7] = random.randrange(10,20)
else:
print("Continuing..")
pAttack = stats[0][7] / 2 * stats[0][3]
eAttack = enemyStats[0][3] / 2 * enemyStats[0][1]
print(pAttack,eAttack)
def combat(enemyAttack,playerAttack):
if playerAttack > enemyAttack:
print(name,"wins")
elif enemyAttack > playerAttack:
print("Enemy wins")
else:
print("It's a tie")
combat(eAttack,pAttack)

So, Brand new to python, just started when Bucky started posting his videos, I went ahead and started making a game, because I like to challenge myself. I have no previous knowledge of Python, but some LUA knowledge which really sped up my process. Heres my code so far.


It's not even close to being done, but it can be fun to mess with for a few minutes..I still need to comment out a few things, and clean up some of those table arrays and wrap it into a shell of some sort and I still need to figure out how to use external files for saving statistics and such, all in due time! Let me know what you guys think c:

~Inumel

edit:sorry for the repost, I deleted the original c:

Post a Reply

Replies

- page 1
Oldest  Newest  Rating
0 Mason Farina · September 2, 2014
Thanks! Going to be another long day of messing with this again, i think :P
+2 Jake Scaife · September 2, 2014
This looks fantastic! Much easier to read
+1 Mason Farina · September 2, 2014
Another update!(sorry about that!)

Just giving a small update before I stop coding for the night :) heres what I have so far


import random
__stats = open('stats.txt', 'r')
__enemy = open('enemy.txt', 'r+')
### unpacking stats
_stats = [__stats.readlines()]

stats = {
    'name': _stats[0][1],
    'level': _stats[0][3],
    'experience': int(_stats[0][5]),
    'experiencetolevel': int(_stats[0][7]),
    'strength': _stats[0][9],
    'wins':  int(_stats[0][11]),
    'losses': int(_stats[0][13]),
    'ties': int(_stats[0][15]),
}
__stats.close()
__stats = open('stats.txt','w')

if int(stats['strength']) is 0:
    stats['strength'] = random.randrange(10, 20)

playerAttackDamage = int(stats['strength']) / 2 * int(stats['level'])
### unpacking stats

### creating enemy
_enemy = [__enemy.readlines()]
enemy = {
    'enemylevel': _enemy[0][1],
    'enemystrength': _enemy[0][3],
    'xpworth': int(_enemy[0][5])
}

if int(enemy['enemystrength']) is 0:
    enemy['enemystrength'] = random.randrange(10, 20)

enemyAttackDamage = int(enemy['enemystrength']) / 2 * int(enemy['enemylevel'])

print(playerAttackDamage)
print(enemyAttackDamage)
### creating enemy

### combat

if playerAttackDamage > enemyAttackDamage:
    stats['wins'] += 1
    stats['experiencetolevel'] = stats['experiencetolevel'] - enemy['xpworth']
    stats['experience'] = stats['experience'] + enemy['xpworth']
    print("The player wins")
elif enemyAttackDamage > playerAttackDamage:
    stats['losses'] += 1
    print("The player has been defeated")
else:
    print("It's a tie")
    stats['ties'] += 1
    
### Saving the players statistics
__stats.write("Name:\n")
__stats.write(stats['name'])
__stats.write("Level:\n")
__stats.write(stats['level'])
__stats.write("Experience:\n")
__stats.write(str(stats['experience'])+'\n')
__stats.write("ExperienceToLevel:\n")
__stats.write(str(stats['experiencetolevel'])+'\n')
__stats.write("Strength:\n")
__stats.write(str(stats['strength']))
__stats.write("Wins:\n")
__stats.write(str(stats['wins'])+'\n')
__stats.write("Losses:\n")
__stats.write(str(stats['losses'])+'\n')
__stats.write("Ties:\n")
__stats.write(str(stats['ties'])+'\n')
__stats.close()
### Saving the players statistics

As you can see, I have started the(long) process of reading and saving statistics, going good so far but still have a way to go. I also started implementing the leveling mechanic, but again. Still quite a bit to do there.
Here is what the player statistics looks like after a bit of extensive button spamming


Name:
Inumel
Level:
1
Experience:
60
ExperienceToLevel:
20
Strength:
11
Wins:
3
Losses:
24
Ties:
7

I need to tweak my attack formula a bit, given the very few amount of wins to loses. but it's getting there! thank you to everybody who's given me feedback so far :)
+1 Mason Farina · September 2, 2014
I have started a complete re-write of everything I had. With new knowledge of Python I figured it would be better than trying to salvage what I had already written. So, with my knew found Python knowledge thanks to Bucky and taking in a suggestion from Jake Scaife, here is the start of the re-written project




import random
__stats = open('stats.txt','r+')
__enemy = open('enemy.txt','r+')
### unpacking stats
_stats = [__stats.readlines()]

stats = {
'level': _stats[0][3],
'experience':_stats[0][5],
'experiencetolevel':_stats[0][7],
'strength': _stats[0][9],
'wins':  _stats[0][11],
'losses': _stats[0][13],
'ties': _stats[0][15],
}

if stats['strength'] is 0:
    stats['strength'] = random.randrange(10,20)

playerAttackDamage = int(stats['strength']) / 2 * int(stats['level'])
### unpacking stats

### creating enemy*
_enemy = [__enemy.readlines()]
enemy = {
'enemylevel': _enemy[0][1],
'enemystrength': _enemy[0][3],
'xpworth': _enemy[0][5]
}

if enemy['enemystrength'] is 0:
    enemy['enemystrength'] = random.randrange(10,20)

enemyAttackDamage = int(enemy['enemystrength']) / 2 * int(enemy['enemylevel'])
### creating enemy


Here is the file for player statistics



Name:
Inumel
Level:
1
Experience:
0
ExperienceToLevel:
80
Strength:
0
wins:
0
losses:
0
ties:
0


And for enemies.
level:
1
strength:
0
xpWorth:
20

Let me know what you guys think of the re-write, and please. Any suggestions and criticism are always appreciated :) 
+1 Jake Scaife · September 1, 2014
The spacing between lines makes things easier to read.

For example isolating if statements and other unique sections of code just makes it that little bit better to read.

Sort of like paragraphing, instead of having one massive block of text :)

Like this:



def combat(enemyAttack,playerAttack):

   if playerAttack > enemyAttack:
       print(name,"wins")
       return wins
   elif enemyAttack > playerAttack:
       print("Enemy wins")
       return losses
   else:
       print("It's a tie")
       return ties

_combat = combat(eAttack,pAttack)

if pAttack > eAttack:
   wins  += 1
elif eAttack < pAttack:
   losses += 1
else:
   ties +=1


Extra lines of white space here and there just makes it more presentable which can be really beneficial for people reading your code. (even yourself from time to time)
0 Mason Farina · September 1, 2014
I always appreciate constructive criticism! More white spaces where? In between the lines? Or in between the commas and such, seperating different objects?

like

this

Or
x=5*15, into x = 5 * 12

I'll be happy to change it :)
+1 Jake Scaife · September 1, 2014
Sweet, keep us updated! Would love to see the finished thing.


I hate to be 'that guy' but maybe try to use a little more white space between sections of code. Just makes it that bit easier for us to read ;) The content is great so far though!
0 Mason Farina · September 1, 2014
Heres an update! I have started integrating my saved statistics file, and i'm starting to attempt to re-save to to said file c:


import random
import test
import math
import tkinter
_readstats = open('savedStats.txt','r+')
_savedStats = [_readstats.read().splitlines()]
stats = [
    ["Name:","placeholder","Level:",1,"Experience:",0,"Strength:",0],
]
enemyStats = [
    ["Level:",1,"Strength:",0],
]
_expchart = [

]
print(_savedStats[0][3])
#print(_teststat)
stats[0][7] = random.randrange(10,20)
enemyStats[0][3] = random.randrange(10,20)
if _savedStats[0][2] == "placeholder":
    name = input("Please enter a name for your character: ")
    stats[0][1] = name
    if name == 'debug':
        enemyStats[0][3] = 0
name = _savedStats[0][1]
print(stats[0][0],stats[0][1],stats[0][2],stats[0][3],stats[0][4],stats[0][5],stats[0][6],stats[0][7])
_stats = input("These are your stats, type 'reroll' to re-roll your stats or 'done' to finish: ")
if _stats == "reroll":
    stats[0][7] = random.randrange(10,20)
else:
    print("Continuing..")
pAttack = stats[0][7] / 2 * int(_savedStats[0][3])
eAttack = enemyStats[0][3] / 2 * enemyStats[0][1]
print(pAttack,eAttack)
wins = int(_savedStats[0][9])
losses = int(_savedStats[0][11])
ties = int(_savedStats[0][13])
def combat(enemyAttack,playerAttack):
    if playerAttack > enemyAttack:
        print(name,"wins")
        return wins
    elif enemyAttack > playerAttack:
        print("Enemy wins")
        return losses
    else:
        print("It's a tie")
        return ties
_combat = combat(eAttack,pAttack)
if pAttack > eAttack:
    wins  += 1
elif eAttack < pAttack:
    losses += 1
else:
    ties +=1

I'm sticking with the "r+" file mode, Ill unpack all of the contents of the file, assign them to variables in the main program, and then re-save them once the program has ended.
0 Mason Farina · September 1, 2014
That should help quite a bit! Thank you!
+2 Jake Scaife · September 1, 2014
Using r+ as far as I understand is fine. The issue is that when you try to write to a file using python, it will overwrite the entire file. Which is why, as you mentioned, when you run the program a second time, it simply overwrites the text rather than appending to it.

There is a few ways you can get around this, I would probably just append to the file.

If you use:
file = open('savedStats.txt','a')


This will open the file in append mode, so it will basically put anything you add to the file at the end of what is already in it.


You could also go the route of reading what is in the file and saving it as a string variable. Then simply adding whatever you need to, to the string.


hope this helps
  • 1
  • 2

Python

107,085 followers
About

This section is all about snakes! Just kidding.

Links
Moderators
Bucky Roberts Administrator