影子奔跑猫 pygame向前移动背景演示

"""影子奔跑猫,向前移动背景演示,猫的坐标不变只是切换造型,程序其实很简单.
"""

import time
import pygame
from pygame.locals import *

>class Background:
    """不断向前移动的滚动背景"""
    def __init__(self,image,screen):
        """初始化函数"""
        self.screen = screen
        self.w = screen.get_width()     # 记录屏幕宽度
        self.h = screen.get_height()    # 记录屏幕高度
        self.image = image              # image是一个surface
        self.image2 = image
        self.rect = self.image.get_rect()
        self.rect2 = self.image2.get_rect()
        self.rect2.right = 0           # 两个surface相隔一个屏幕宽度
        self.dx = 50
        
    def update(self):
        """更新两个矩形的坐标"""
        self.rect.move_ip(self.dx,0)
        self.rect2.move_ip(self.dx,0)
        if self.rect.left >= self.w:self.rect.right = 0   # 到了最右边,瞬移到最左边
        if self.rect2.left >= self.w:self.rect2.right = 0    # 到了最左边,瞬移到最右边

You must log in to read the rest of this article. Please log in or register as a user.