Design a classic game and a Python classic game with python

Source: Internet
Author: User

Design a classic game and a Python classic game with python

This article describes how to use Python to design a classic game: Guess the size.

In this game, we will use all the content I mentioned earlier: usage of variables, parameter transfer, function design, conditional control, and loop. Let's make a summary and review.

Game rules:

The initial principal amount is 1000 RMB. The default odds are doubled. If you win, you will get a factor of the amount, lose, and deduct a factor of the amount.

  1. The player chooses to place a bet. The bet is large or small;
  2. Enter the bet amount;
  3. Shake 3 dice, 11 ≤ the total number of dice ≤ 18 is large, 3 ≤ the total number of dice ≤ 10 is small;
  4. If you win the game, you will get a factor of 1. If you lose the game, you will be deducted a factor of 1. If the principal is 0, the game will end.

The program running result is as follows:

Now let's sort out our ideas.

  1. Let's let the program know how to shake the dice first;
  2. Let the program know what is big and what is small;
  3. The user starts to play the game. If he is right, he will win the money. If he is wrong, he will lose the money. After the game is completed, the game will end.

After sorting out the ideas, we will start coding.

Shake the dice:

Define the roll_dice function. For three dice, the number of rounds numbers is 3, and the initial points value of the dice points is null. Here, the parameter transfer uses the previous keyword parameter transfer.

The random number is generated using import random. The most convenient thing in Python is that there are a lot of powerful library support. Now we can directly import a random built-in library and use it to generate random numbers. For example:

1 import random2 point = random. randrange () 3 # random. randrange () generate a random number from 1 to 6 4 print (point)

After print (point), you can see the printed random number. Each running result is random.

Next, let's take a look at the complete code for rolling the dice:

Import randomdef roll_dice (numbers = 3, points = None): print ('----- shake dice -----') if points is None: points = [] # points is an empty list, you can insert a new value to this list later while numbers> 0: point = random. randrange (1, 7) points. append (point) # Use the append () method to insert the point value to numbers = numbers-1 in the points list # After completing this operation, numbers minus 1, if the value is less than or equal to 0, the return points loop is no longer executed.

Fixed size:

The Code is as follows:

Def roll_result (total): isBig = 11 <= total <= 18 isSmall = 3 <= total <= 10 if isBig: return 'Big 'elif isSmall: return 'small'

Play games:

The initial principal is 1000 yuan, with a default odds of one time. If you win the game, you will get a factor of the amount. If you lose, you will be deducted a factor of one. If the principal is 0, the game ends.

Def start_game (): your_money = 1000 while your_money> 0: print ('----- game start -----') choices = ['day', 'small'] # choices values are big and small, the user needs to enter either of them as correct your_choice = input ('place a bet, large or small: ') your_bet = input ('bet amount:') if your_choice in choices: points = roll_dice () # Call the roll_dice function total = sum (points) # sum as the sum, and add the results of the three dice youWin = your_choice = roll_result (total) if youWin: print ('dice points: ', points) print ('Congratulations, you have won {} RMB. You now have {} RMB '. format (your_bet, your_money + int (your_bet) # your_bet is a string format, which needs to be converted to the int type for calculation your_money = your_money + int (your_bet) # newest principal else: print ('dice points: ', points) print (' Sorry, you lost {} RMB. You now have {} RMB '. format (your_bet, your_money-int (your_bet) your_money = your_money-int (your_bet) else: print ('incorrect format, please input it again ') # If the input is not large or small in the choices list, the format is incorrect. else: print ('game termination') start_game ()

Here, we have completed the design of the three major parts of the game. You must think carefully, sort out the design ideas, and work out the code.

Finally, attach[Guess size] complete game code:

Import randomdef roll_dice (numbers = 3, points = None): print ('----- shake dice -----') if points is None: points = [] while numbers> 0: point = random. randrange (1, 7) points. append (point) numbers = numbers-1 return pointsdef roll_result (total): isBig = 11 <= total <= 18 isSmall = 3 <= total <= 10 if isBig: return 'Big 'elif isSmall: return '小' def start_game (): your_money = 1000 while your_money> 0: print (' ----- game start ----- ') choices = ['Big ', '小'] your_choice = input ('place a bet, large or small: ') your_bet = input ('bet amount:') if your_choice in choices: points = roll_dice () total = sum (points) youWin = your_choice = roll_result (total) if youWin: print ('dice points: ', points) print ('Congratulations, you win {} RMB, you now have {} RMB '. format (your_bet, your_money + int (your_bet) your_money = your_money + int (your_bet) else: print ('dice points: ', points) print (' sorry, you lost {} RMB. You now have {} RMB '. format (your_bet, your_money-int (your_bet) your_money = your_money-int (your_bet) else: print ('incorrect format, please enter 'Again) else: print ('game termination') start_game ()

The above is all the content of this article. I hope this article will help you in your study or work. I also hope to provide more support to the customer's home!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.