Have you ever wondered how computer games are made? It's actually not as complicated as you think!
PyGame is a Python library that makes it easier for you to write a game. It provides functions such as image processing and sound playback, and they can be easily integrated into your game. Go to the official website and click here to download the PyGame installation package that suits you.
Let’s take masturbation as an example (recommended learning: Python video tutorial)
1. Create the game framework and game background
#这个模块放一些常用的工具和基础类和精灵类 #在其他模块调用 import pygame import random #设置游戏屏幕大小 这是一个常量 SCREEN_RECT = pygame.Rect(0,0,580,700) #敌机的定时器事件常量 CREATE_ENEMY_EVENT = pygame.USEREVENT #定制一个精灵类,需要继承pygame提供的精灵类 #需要定义的属性有: #image图片 #rect坐标 #speed速度 #接下来开始写敌机方面的内容 产生敌机 #先定义一个事件常量 CREATE_ENEMY_EVENT = pygame.USEREVENT #我们还可以定义一个事件常量(发射子弹) HERO_FIRE_EVENT = pygame.USEREVENT + 1 class GameSprite(pygame.sprite.Sprite): def __init__(self,new_image,new_speed=1): super().__init__() #图片 self.image = pygame.image.load(new_image) #速度 self.speed = new_speed #位置 获取图片的宽和高 get_rect()(0,0,宽,高) self.rect = self.image.get_rect() #精灵移动的速度 包括英雄精灵 背景精灵 敌机精灵 子弹精灵 self.speed = new_speed def update(self): #默认垂直方向移动 y轴控制垂直方向 self.rect.y += self.speed #self.rect.x += 1 #以上是游戏的基础类,接下来设置背景类 #明确背景类继承自游戏的精灵类 class Background(GameSprite): def __init__(self,is_alt = False): #is_alt判断是否为另一张图像 #False表示第一张图像 #Ture表示另外一张图像 #两张图像交替循环 #传图片 super().__init__("/home/zhangyuan/下载/beijing.png") if is_alt: #如果是第二张图片 初始位置为-self.rect.height self.rect.y = -self.rect.height #def __init__(self,new_image): # super().init__(new_image) def update(self): #调用父类方法 super().update() if self.rect.y >= SCREEN_RECT.height: self.rect.y = -self.rect.height
2. Create Enemy Elf
class Enemy(GameSprite): def __init__(self): super().__init__("/home/zhangyuan/images/enemy1.png") #随机速度 self.speed = random.randint(10, 15) #设置敌机的初始位置 self.rect.left = SCREEN_RECT.width max_ = SCREEN_RECT.height -self.rect.height self.rect.bottom = random.randint(0, max_) def update(self): panduan = random.randint(0, 1) if panduan == 0: self.rect.y -= self.speed self.rect.x -= self.speed else: self.rect.y += self.speed self.rect.x -= self.speed #判断敌机是否飞出屏幕 如果飞出屏幕将敌机从精灵组删除 if self.rect.y >= SCREEN_RECT.height or self.rect.right <p>3. Create Hero Elf</p><pre class="brush:php;toolbar:false">class Bullet(GameSprite): def __init__(self): super().__init__("/home/zhangyuan/images/bullet1.png",-5) def update(self): super().update() #判断是否超出屏幕 如果是 从精灵组删除 if self.rect.bottom <p>5. Collision Detection</p><pre class="brush:php;toolbar:false">#第一个参数和第二个参数是要参与碰撞检测的精灵 #第三个参数为Ture的时候 就是当碰撞的时候被碰撞的精灵从精灵组移除 pygame.sprite.groupcollide( self.enemy_group,self.hero.bullet, True, True)#子弹 #判断列表时候有内容 if len(enemies)>0: #让英雄牺牲 self.hero.kill() #结束游戏 PlaneGame.__game_over() @staticmethod def __game_over(): print("游戏结束") #这是pygame提供的卸载模块功能 pygame.quit() #这是pygame本身提供的退出脚本的功能 exit() #需要先卸载pygame模块 然后退出脚本 if __name__ == "__main__": game = PlaneGame() game.star_game()
The process is roughly like this. Pygame can also make many games. Only after special research Know the fun
For more Python-related technical articles, please visit the Python Tutorial column to learn!
The above is the detailed content of How to make games with python. For more information, please follow other related articles on the PHP Chinese website!

Python excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.

Python is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

Python is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.

You can learn the basics of Python within two hours. 1. Learn variables and data types, 2. Master control structures such as if statements and loops, 3. Understand the definition and use of functions. These will help you start writing simple Python programs.

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

Error loading Pickle file in Python 3.6 environment: ModuleNotFoundError:Nomodulenamed...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.