Home >Backend Development >Python Tutorial >How to make games with python

How to make games with python

(*-*)浩
(*-*)浩Original
2019-06-25 15:06:086301browse

Have you ever wondered how computer games are made? It's actually not as complicated as you think!

How to make games with python

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 <=0 or self.rect.bottom <=0:
        self.kill()

3. Create Hero Elf

class Bullet(GameSprite):

        def __init__(self):
            super().__init__("/home/zhangyuan/images/bullet1.png",-5)
        def update(self):
            super().update()

      #判断是否超出屏幕 如果是 从精灵组删除
      if self.rect.bottom < 0:
          self.kill()

5. Collision Detection

#第一个参数和第二个参数是要参与碰撞检测的精灵
#第三个参数为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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn