Home Backend Development Python Tutorial pygame学习笔记(5):游戏精灵

pygame学习笔记(5):游戏精灵

Jun 10, 2016 pm 03:15 PM
pygame

据说在任天堂FC时代,精灵的作用相当巨大,可是那时候只知道怎么玩超级玛丽、魂斗罗,却对精灵一点也不知。pygame.sprite.Sprite就是Pygame里面用来实现精灵的一个类,使用时,并不需要对它实例化,只需要继承他,然后按需写出自己的类就好了,因此非常简单实用。

一、什么是精灵

精灵可以认为成是一个个小图片,一种可以在屏幕上移动的图形对象,并且可以与其他图形对象交互。精灵图像可以是使用pygame绘制函数绘制的图像,也可以是原来就有的图像文件。

二、sprite中主要且常用的变量有以下几个:更多详细的见http://www.pygame.org/docs/ref/sprite.html#pygame.sprite.Sprite
self.image这个负责显示什么。如self.image=pygame.Surface([x,y])说明该精灵是一个x,y大小的距形,self.image=pygame.image.load(filename)说明该精灵调用显示filename这个图片文件。

复制代码 代码如下:

self.image.fill([color]),负责对self.image着色,如self.image=pygame.Surface([x,y])
self.image.fill([255,0,0])

对x,y距形填充红色。
self.rect负责在哪里显示。一般来说,先用self.rect=self.image.get_rect()获得image距形大小,然后给self.rect设定显示的位置,一般用self.rect.topleft(topright、bottomleft、bottomright)来设定某一个角的显示位置。另外,self.rect.top、self.rect.bottom、self.rect.right、self.rect.left分别表示上下左右。
self.update 负责使精灵行为生效。
Sprite.add  添加精灵到group中去。
Sprite.remove 从group中删除
Sprite.kill 从groups中全部删除精灵
Sprite.alive 判断精灵是否属于groups

三、建立一个简单的精灵

所有精灵在建立时都是从pygame.sprite.Sprite中继承的。

(1)做一个精灵,绘制一个宽30、高30的距形,具体代码如下:

复制代码 代码如下:

class Temp(pygame.sprite.Sprite):
    def __init__(self,color,initial_position):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface([30,30])
        self.image.fill(color)
        self.rect=self.image.get_rect()
        self.rect.topleft=initial_position

这里逐句进行一下分析,pygame.sprite.Sprite.__init__(self)完成初始化。self.image = pygame.Surface([30,30])定义显示30*30的一个距形surface。self.image.fill(color)用color来填充颜色。self.rect=self.image.get_rect()获取self.image大小。self.rect.topleft=initial_position确定左上角显示位置,当然也可以用topright、bottomrigh、bottomleft来分别确定其他几个角的位置。精灵的显示,在一个640*480大小的白色窗体[50,100]的位置绘制一个30*30大小的红色距形,完整代码如下:

复制代码 代码如下:

import pygame,sys
pygame.init()
class Temp(pygame.sprite.Sprite):
    def __init__(self,color,initial_position):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface([30,30])
        self.image.fill(color)
        self.rect=self.image.get_rect()
        self.rect.topleft=initial_position
screen=pygame.display.set_mode([640,480])
screen.fill([255,255,255])
b=Temp([255,0,0],[50,100])
screen.blit(b.image,b.rect)
pygame.display.update()
while True:
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            sys.exit()

(2)做一个精灵,显示内容为某一图片,这里以前面用过的小车图片为例,代码如下:
复制代码 代码如下:

import pygame,sys
pygame.init()
class Car(pygame.sprite.Sprite):
    def __init__(self,filename,initial_position):
        pygame.sprite.Sprite.__init__(self)
        self.image=pygame.image.load(filename)
        self.rect=self.image.get_rect()
        #self.rect.topleft=initial_position
        self.rect.bottomright=initial_position
        print self.rect.right
        
screen=pygame.display.set_mode([640,480])
screen.fill([255,255,255])
fi='ok1.jpg'
b=Car(fi,[150,100])
screen.blit(b.image,b.rect)
pygame.display.update()
while True:
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            sys.exit()

这段代码与(1)的不同之处在于self.image定义为pygame.image.load(filename),用来显示filename文件,本代码使用了ok1.jpg文件,并定义了小车右底角的显示位置是[150,100]。

三、学习精灵组

(1)使用精灵在屏幕上放多个图像,这种方法没用利用精灵组的概念,而是利用了list来生成每一个精灵。Cargroup用来存储不同位置的Car,screen.blit(carlist.image,carlist.rect)逐个显示每一个精灵。具体见代码:

复制代码 代码如下:

import pygame,sys
pygame.init()
class Car(pygame.sprite.Sprite):
    def __init__(self,filename,initial_position):
        pygame.sprite.Sprite.__init__(self)
        self.image=pygame.image.load(filename)
        self.rect=self.image.get_rect()
        self.rect.bottomright=initial_position

        
screen=pygame.display.set_mode([640,480])
screen.fill([255,255,255])
fi='ok1.jpg'
locationgroup=([150,200],[350,360],[250,280])
Cargroup=[]
for lo in locationgroup:
    Cargroup.append(Car(fi,lo))
for carlist in Cargroup:
    screen.blit(carlist.image,carlist.rect)
pygame.display.update()
while True:
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            sys.exit()


具体效果见图:

(2)使用精灵组来实现多个图像。上面精灵是存在一个列表中,很方便,就是有点不太好用。除了精灵,pygame还提供了精灵组,它很适合处理精灵列表,有添加,移除,绘制,更新等方法。具体如下:http://www.pygame.org/docs/ref/sprite.html#pygame.sprite.Sprite
Group.sprites 精灵组
Group.copy 复制
Group.add 添加
Group.remove 移除
Group.has 判断精灵组成员
Group.update 更新
Group.draw 位块显示
Group.clear - 绘制背景
Group.empty 清空
同样还是上面的这个例子,这里用精灵组来实现。

复制代码 代码如下:

import pygame,sys
pygame.init()
class Car(pygame.sprite.Sprite):
    def __init__(self,filename,initial_position):
        pygame.sprite.Sprite.__init__(self)
        self.image=pygame.image.load(filename)
        self.rect=self.image.get_rect()
        self.rect.bottomright=initial_position
screen=pygame.display.set_mode([640,480])
screen.fill([255,255,255])
fi='ok1.jpg'
locationgroup=([150,200],[350,360],[250,280])
Cargroup=pygame.sprite.Group()
for lo in locationgroup:
    Cargroup.add(Car(fi,lo))

for carlist in Cargroup.sprites():
    screen.blit(carlist.image,carlist.rect)
pygame.display.update()
while True:
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            sys.exit()


两个例子都是在[150,200],[350,360],[250,280]三个位置显示三辆小车,不同之处第一个用的是list,第二个用的是精灵组。差别就在几句话上,一是Cargroup=pygame.sprite.Group()定义Cargroup为精灵组,二是Cargroup.add(Car(fi,lo))用add代替了append,三是for carlist in Cargroup.sprites()这句中逐个显示精灵,这里试了一下,直接用for carlist in Cargroup也是可以的。精灵组的代码是高度优化过了,常常比列表还快。插入和删除都是常见的操作,代码还可以避免内存在循环中反复消耗。

四、动画

利用精灵组做动画会显得比较方便,这里我们首先让上面的三辆小车运动起来。
(1)三辆小车以不同的速度前行,利用random.choice随机生成[-10,-1]之间的值作为速度让小车从下向上运动,并且当到达顶部时,再从底部出现。代码如下:

复制代码 代码如下:

import pygame,sys
from random import *
pygame.init()
class Car(pygame.sprite.Sprite):
    def __init__(self,filename,initial_position,speed):
        pygame.sprite.Sprite.__init__(self)
        self.image=pygame.image.load(filename)
        self.rect=self.image.get_rect()
        self.rect.topleft=initial_position
        self.speed=speed
    def move(self):
        self.rect=self.rect.move(self.speed)
        if self.rect.bottom             self.rect.top=480
screen=pygame.display.set_mode([640,480])
screen.fill([255,255,255])
fi='ok1.jpg'
locationgroup=([150,200],[350,300],[250,200])
Cargroup=pygame.sprite.Group()
for lo in locationgroup:
    speed=[0,choice([-10,-1])]
    Cargroup.add(Car(fi,lo,speed))

while True:
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            sys.exit()
    pygame.time.delay(20)
    screen.fill([255,255,255])
    for carlist in Cargroup.sprites():
        carlist.move()
        screen.blit(carlist.image,carlist.rect)
    pygame.display.update()

(2)可以通过左右键控制三辆小车的左右移动,按左键向左移动,当到达最左边时,不再移动,按右键向右移动,当到达最右边时,不再移动。具体代码如下:

复制代码 代码如下:

import pygame,sys
from random import *
pygame.init()
class Car(pygame.sprite.Sprite):
    def __init__(self,filename,initial_position,speed):
        pygame.sprite.Sprite.__init__(self)
        self.image=pygame.image.load(filename)
        self.rect=self.image.get_rect()
        self.rect.topleft=initial_position
        self.speed=speed
    def move(self):
        self.rect=self.rect.move(self.speed)
        if self.rect.bottom             self.rect.top=480
    def moveleft(self):
        self.rect.left=self.rect.left-10
        if self.rect.left             self.rect.left=0
    def moveright(self):
        self.rect.right=self.rect.right+10
        if self.rect.right>640:
            self.rect.right=640
screen=pygame.display.set_mode([640,480])
screen.fill([255,255,255])
fi='ok1.jpg'
locationgroup=([150,200],[350,300],[250,200])
Cargroup=pygame.sprite.Group()
for lo in locationgroup:
    speed=[0,choice([-10,-1])]
    Cargroup.add(Car(fi,lo,speed))

while True:
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            if event.key==pygame.K_LEFT:
                for carlist in Cargroup.sprites():
                    carlist.moveleft()
                    screen.blit(carlist.image,carlist.rect)
            if event.key==pygame.K_RIGHT:
                for carlist in Cargroup.sprites():
                    carlist.moveright()
                    screen.blit(carlist.image,carlist.rect)
    pygame.time.delay(20)
    screen.fill([255,255,255])
    for carlist in Cargroup.sprites():
        carlist.move()
        screen.blit(carlist.image,carlist.rect)
    pygame.display.update()

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1253
24
Pygame installation details: teach you step by step to install and configure the development environment Pygame installation details: teach you step by step to install and configure the development environment Feb 20, 2024 pm 04:54 PM

Pygame installation details: Teach you step by step to install and configure the development environment, specific code examples are required Introduction: Pygame is a Python-based game development library. It provides a wealth of tools and functions to make game development simple and interesting. This article will introduce in detail how to install Pygame, configure the development environment, and provide specific code examples. Part 1: Install Pygame Install Python: Before you start installing Pygame, you first need to make sure that Pyt is installed on your computer.

How to install pygame How to install pygame Nov 27, 2023 pm 03:49 PM

pygame installation steps: 1. Use the "python --version" command to view the installed Python version; 2. Install pip; 3. Download pygame; 4. Enter cmd, enter the command pip install wheel, and install wheel; 5. Enter in cmd The directory of the .whl file; 6. Enter Python in cmd, and then enter import pygame to check whether the installation is successful; 7. Install pygame in the editor settings.

How to use Pygame's Draw drawing method in Python How to use Pygame's Draw drawing method in Python Apr 19, 2023 pm 04:46 PM

Pygame's Draw Pygame provides a draw module for drawing some simple graphic shapes, such as rectangles, polygons, circles, straight lines, arcs, etc. The commonly used methods of the pygame.draw module are shown in the following table: Name description pygame.draw.rect() draws a rectangle pygame.draw.polygon() draws a polygon pygame.draw.circle() draws a circle based on the center and radius pygame.draw. ellipse() draws an ellipse pygame.draw.arc() draws an arc (waving part of the ellipse) pygame.draw.line() draws a line

Getting Started with Pygame: Comprehensive Installation and Configuration Tutorial Getting Started with Pygame: Comprehensive Installation and Configuration Tutorial Feb 19, 2024 pm 10:10 PM

Learn Pygame from scratch: complete installation and configuration tutorial, specific code examples required Introduction: Pygame is an open source game development library developed using the Python programming language. It provides a wealth of functions and tools, allowing developers to easily create a variety of type of game. This article will help you learn Pygame from scratch, and provide a complete installation and configuration tutorial, as well as specific code examples to get you started quickly. Part One: Installing Python and Pygame First, make sure you have

Basic tutorial for learning Pygame: Quick introduction to game development Basic tutorial for learning Pygame: Quick introduction to game development Feb 19, 2024 am 08:51 AM

Pygame installation tutorial: Quickly master the basics of game development, specific code examples are required Introduction: In the field of game development, Pygame is a very popular Python library. It provides developers with rich features and easy-to-use interfaces, allowing them to quickly develop high-quality games. This article will introduce you in detail how to install Pygame and provide some specific code examples to help you quickly master the basics of game development. 1. Installation of Pygame Install Python and start installing Pyga

Python's Pygame Font module - how to use text and fonts? Python's Pygame Font module - how to use text and fonts? Apr 23, 2023 pm 11:19 PM

Pygame's Font text and font Pygame uses the pygame.font module to create a font object to achieve the purpose of drawing text. Commonly used methods of this module are as follows: Name Description pygame.font.init() Initialize the font module pygame.font.quit() Uninitialize the font module pygame.font.get_init() Check whether the font module has been initialized and return a Boolean value . pygame.font.get_default_font() gets the file name of the default font. Returns the file name of the font in the system pygame.font.get_fonts() gets all

How to create a simple word game using Python and Pygame How to create a simple word game using Python and Pygame May 09, 2023 pm 07:43 PM

1. Environment preparation 1) Operating environment environment installation: python3.8: interpreter, pycharm: code editor, pygame, numpy, and some of the built-in modules can be used directly by installing Python. 2) Module installation The installation method of third-party libraries is as follows: General installation: pipinstall+module name Mirror source installation: pipinstall-ipypi.douban.com/simple/+module name (there are many domestic mirror sources, here is Douban’s usage ) 3) Pictures and text materials, etc. 2. Code display main program - importpygameimportsysimp

Pygame Installation Guide: An easy-to-understand introductory tutorial Pygame Installation Guide: An easy-to-understand introductory tutorial Feb 20, 2024 pm 12:39 PM

Pygame Installation Tutorial: A simple and easy-to-understand getting started guide, requiring specific code examples Introduction: Pygame is a very popular Python library for developing 2D games. It provides rich functions and easy-to-use interfaces, making game development easier and more interesting. This article will introduce you to the installation process of Pygame and provide specific code examples to help beginners get started quickly. 1. Install Python and Pygame. Download Python and Pygame: First you need to install Python.

See all articles