Python Forum
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
1st Game
#2
(Jun-26-2018, 08:21 AM)taflad Wrote: As I have no experience with functions yet, I created the game using a for loop (code below)

Note that you used a while loop, not a for loop. Two different animals.

The first thing you could do with functions is checking for replay. You keep repeating that code over and over again. If you want to change it, you have to change it in each place. If the code is in a function, you just have to change it in one place.

Functions can also help organized your code. I don't like having a block of code bigger than about 50 lines. If it is under 50 lines, I can see the whole block on my screen at once. You have a whole bunch of if statements. Each of those could be a function, and then the main block of code would be much smaller. If you use descriptive function names, like your variable names (nice to see from a new programmer), it's still clear what is going on in your code.

What I would do is put all the variables defined at the top of the code into a dictionary. Call it game_state. Then pass that dictionary to the functions as a parameter. While not strictly necessary, I would return it from the function and reassign it each time (for clarity). So you function calls would look something like this:

game_state = full_speed(game_state)
And your function calls would look something like this:

def full_speed(game_state):
    ... # code here
    return game_state
A few comments about the game: You don't need the done variable. Everywhere you would set done to True, you break instead, except the user choosing 'Q'. You could just break on 'Q', make the loop while True:, and simplify your if statements:

if replay_char != 'Y':
    break
Or, if you do the replay function like I suggest, and have it return True for replaying:

if not replay_check():
    break
You don't reset the game tracking variables when the player wants to replay. So if they kill their camel, they start the next game with a dead camel. This could be another function, to return a fresh game_state dictionary. The way you track the natives has no memory. If I go full speed twice and normal speed once, going normal speed wipes out any "gains" I got from going full speed. I would keep track of where the natives are, not how close they are, and have them move the same each turn (maybe a range between normal speed and full speed). Then check if their distance is greater than the player's to see if they've caught up.

There is a functions tutorial on this site. There is a link to it in my signature (below).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Messages In This Thread
1st Game - by taflad - Jun-26-2018, 08:21 AM
RE: 1st Game - by ichabod801 - Jun-26-2018, 12:52 PM
RE: 1st Game - by taflad - Jun-27-2018, 10:29 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020