A small game written in Python: the fun of exploring the game world

introduction:

Python is an easy-to-learn programming language whose flexibility and powerful features make it the first choice of many developers. In this article, we will use Python to write three small games to show the charm of Python and the fun of game development. These small games will lead you into a wonderful game world, allowing you to experience the fun and creativity of programming.
insert image description here

Part 1: Guess the Number Game

In this game, the computer randomly generates a number between 1 and 100, and the player needs to guess to find this number. The game will give hints according to the player's guess until the player guesses correctly.
Before Latiao, I posted a full 30 mini-games: 30 Python mini-games, I can play fish at work for a day [enclosed source code] welcome to watch

Code case 1:

import random

def guess_number():
    number = random.randint(1, 100)
    guess = 0
    tries = 0

    while guess != number:
        guess = int(input("请输入一个1到100之间的数字:"))
        tries += 1

        if guess < number:
            print("猜小了!")
        elif guess > number:
            print("猜大了!")
        else:
            print("恭喜你,猜对了!")
            print("你一共猜了", tries, "次。")

guess_number()

Part II: The Rock, Paper, Scissors Game

This is a classic guessing game, the player and the computer will punch at the same time, and the winner will be judged according to the rules.

Code case 2:

import random

def rock_paper_scissors():
    choices = ["石头", "剪刀", "布"]
    player_choice = input("请输入你的选择(石头、剪刀、布):")
    computer_choice = random.choice(choices)

    print("你选择了:", player_choice)
    print("计算机选择了:", computer_choice)

    if player_choice == computer_choice:
        print("平局!")
    elif (player_choice == "石头" and computer_choice == "剪刀") or \
         (player_choice == "剪刀" and computer_choice == "布") or \
         (player_choice == "布" and computer_choice == "石头"):
        print("你赢了!")
    else:
        print("你输了!")

rock_paper_scissors()

Part Three: The Maze Game

In this game, the player needs to control a character to move in the maze through the keyboard, and the goal is to find the exit. The player can use the arrow keys to control the movement of the character.

Code case 3:

import pygame

def maze_game():
    pygame.init()
    screen = pygame.display.set_mode((400, 400))
    pygame.display.set_caption("迷宫游戏")

    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False

        keys = pygame.key.get_pressed()
        if keys[pygame.K_UP]:
            # 处理向上移动的逻辑
            pass
        elif keys[pygame.K_DOWN]:
            # 处理向下移动的逻辑
            pass
        elif keys[pygame.K_LEFT]:
            # 处理向左移动的逻辑
            pass
        elif keys[pygame.K_RIGHT]:
            # 处理向右移动的逻辑
            pass

        screen.fill((255, 255, 255))
        # 绘制迷宫和角色
        pygame.display.flip()

    pygame.quit()

maze_game()

Summarize:

Through the code cases of the above three small games, we have demonstrated the application of Python in game development. Whether it is a number guessing game, a rock-paper-scissors game or a maze game, Python has demonstrated its simplicity, flexibility and ease of learning. I hope these small games can stimulate your interest in programming and let you experience the fun and creativity brought by programming. Start your game programming journey!

Guess you like

Origin blog.csdn.net/AI19970205/article/details/132066264