Article directory
write in front
Contents of this issue: Implementation of Pleasant Goat and Big Big Wolf version of Xiaoxiaole mini game based on pygame
Download address: https://download.csdn.net/download/m0_68111267/88700193
lab environment
- python3.11 and above
- pycharm
- pygame
Command to install pygame:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pygame
PyGame
pygame is a Python-based multimedia library mainly used for developing 2D games. It provides a series of functions and tools to make game development easier and faster. Below is a simple introductory tutorial to help you get started with pygame.
First, you need to install the pygame library. Open a command prompt and enter the following command:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pygame
After the installation is complete, you can start writing your first pygame program. First, create a new Python file named "game.py". Then, import the pygame library:
import pygame
Next, the pygame library needs to be initialized. Enter the following code:
pygame.init()
Then, create a window to display the game interface. Enter the following code:
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("My Game")
Here, we create a 800x600 pixel window and set the title to "My Game".
Next, we need to create a game loop that updates the game interface. Enter the following code:
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
In the game loop, we detect whether a QUIT event occurs (click the close button of the window). If so, set running to False and exit the game loop.
Finally, we need to add code to update the game interface. Enter the following code:
screen.fill((0, 0, 0))
pygame.display.flip()
Here, we first fill the entire screen with black, and then call the pygame.display.flip() function to update the game interface.
Now, you can run the program. Enter the following command:
python game.py
You will see a blank window. Click the Close button and the program will exit.
This is just a simple introductory tutorial to help you get started with pygame. In actual development, you can use other functions and tools provided by pygame to create more complex and interesting games. If you want to learn pygame in depth, you can check out the official documentation and online tutorials. I wish you write a fun game!
Xiaoxiaole
programming
import sys
import os
import time
import random
import pygame
# 参数
Width = 666
Height = 666
NumGrid = 8
GridSize = 64
X_Margin = (Width - GridSize * NumGrid) // 2
Y_Margin = (Height - GridSize * NumGrid) // 2
root = os.getcwd()
fps = 30
……请下载后查看
Run results
Precautions
If you encounter the problem " no module named pygame ", please enter "pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pygame" in the terminal and wait for the pygame library to be installed before running the program.
Series of articles
serial number | Table of contents | direct link |
---|---|---|
1 | Tetris mini game based on PyGame | https://want595.blog.csdn.net/article/details/135427809 |
2 | Backgammon game based on Tkinter | https://want595.blog.csdn.net/article/details/135427644 |
3 | Xiaoxiaole mini game based on PyGame | https://want595.blog.csdn.net/article/details/135390188 |
4 | Snake game based on PyGame | https://want595.blog.csdn.net/article/details/135373146 |
write on the back
I am a funny bunny, thank you for your like!