Python Fat Chicken War Development Record (1): Basic Framework

Game design:
Game name: Fatty Chicken War
Basic gameplay: The player uses the keyboard to control the Fatty Chicken to kill the enemy.
Needs to be realized: buy and select chicken breeds, control fat chicken battles, rewards and achievements, etc.

One, Pygame installation

pip install Pygame

2. The main program entry
New: \FatChickenWars.py:
1. Create a Pygame window

import sys
import pygame

def run_game():#游戏初始化
    pygame.init()
    screen=pygame.display.set_mode((1200,800))
    pygame.display.set_caption("肥鸡大战")

    while True:
        pygame.display.flip()

run_game()

First step effect
The window will appear and freeze.
2. Monitor user input events

def run_game():#游戏初始化
    pygame.init()
    screen=pygame.display.set_mode((1200,800))
    pygame.display.set_caption("肥鸡大战")
    
    #主循环
    while True:
        #监视键盘和鼠标事件
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                sys.exit()
                
        #刷新屏幕
        pygame.display.flip()

This way it won't get stuck.
3. Add some decorative codes (background color)

def run_game():#游戏初始化
    pygame.init()
    screen=pygame.display.set_mode((1200,800))
    pygame.display.set_caption("肥鸡大战")

    #背景色
    bg_color=(230,230,230)#小灰灰

    #主循环
    while True:
        #监视键盘和鼠标事件
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                sys.exit()

        #填充背景色
        screen.fill(bg_color)

        #刷新屏幕
        pygame.display.flip()

After modification
Isn't it beautiful?

2021.1.20

Guess you like

Origin blog.csdn.net/k1095118808/article/details/112875242