Python 如何在Pygame中滚动背景图像?

Python 如何在Pygame中滚动背景图像?,python,python-3.x,pygame,Python,Python 3.x,Pygame,我想知道如何在pygame中滚动我的背景图像 我有一个正在移动的对象,我希望它在移动时滚动 这里是一个视频的背景图像现在 现在我只是在快速播放背景图像 def redrawwindow(): window.blit(bg,(0,0)) 这是我的全部代码 import pygame import random pygame.init() #this is screem height window = pygame.display.set_mode((500,500)) #know

我想知道如何在pygame中滚动我的背景图像

我有一个正在移动的对象,我希望它在移动时滚动 这里是一个视频的背景图像现在 现在我只是在快速播放背景图像


def redrawwindow():
    window.blit(bg,(0,0))

这是我的全部代码

import pygame
import random
pygame.init()

#this is screem height
window = pygame.display.set_mode((500,500))

#know we put screem name
pygame.display.set_caption("Noobs Flappy Bird Game")

#player class
class bird:
    def __init__(self,x,y,height,width,color):
        self.x = x
        self.y =y
        self.bright = [
            pygame.image.load("killers50.png"),
            pygame.image.load("killers51.png"),
            pygame.image.load("killers52.png"),
            pygame.image.load("killers53.png"),
 
                           
                              ]
        self.bleft = [
            pygame.image.load("ms1.png"),
            pygame.image.load("ms2.png"),
            pygame.image.load("ms3.png"),
            pygame.image.load("ms4.png"),
 





                          
                              ]
        self.bright = [pygame.transform.scale(image,(image.get_width()//15,image.get_height()//15)) for image in self.bright]
        self.bleft = [pygame.transform.scale(image,(image.get_width()//15,image.get_height()//15)) for image in self.bleft]
        self.height = height
        self.width = width
        self.isJump = False
        self.JumpCount = 10
        self.fall = 0
        self.speed = 5
        self.Walking = 0
        self.vel = 5
        self.color  = color
        self.rect = pygame.Rect(x,y,height,width)
        self.direction = "down"

                # this makes the enemy move right and left
    def draw(self):
        self.rect.topleft = (self.x,self.y)
        if self.Walking + 1 >= 33:
            self.Walking = 0

        if self.vel > 0:        # left
            window.blit(self.bright[self.Walking % 3], (self.x,self.y))
            self.Walking += 1
        else:                   # right
            window.blit(self.bleft[self.Walking % 3], (self.x,self.y))
            self.Walking += 1

class platform:
    def __init__(self,x,y,height,width,color):
        self.x = x
        self.y = y
        self.height = height
        self.width = width
        self.color = color
        self.pipis = pygame.image.load("pip.png")
        self.pipis = pygame.transform.scale(self.pipis,(self.pipis.get_width()//3,self.pipis.get_height()//3))
        self.rect = pygame.Rect(x,y,height,width)
    def draw(self):
        self.rect.topleft=(self.x,self.y)
        window.blit(self.pipis,self.rect)

class pip:
    def __init__(self,x,y,height,width,color):
        self.x = x
        self.y = y
        self.height = height
        self.width = width
        self.color = color
        self.pipis = pygame.image.load("pipo.png")
        self.pipis = pygame.transform.scale(self.pipis,(self.pipis.get_width()//3,self.pipis.get_height()//3))
        self.rect = pygame.Rect(x,y,height,width)
    def draw(self):
        self.rect.topleft=(self.x,self.y)
        window.blit(self.pipis,self.rect)


#player and enemy
white = (255,255,255)
bird1 = bird(0,400,50,50,white)

red = (255,48,48)
platform1 = platform(600,300,150,50,white)
platform2 = platform(800,200,150,50,white)
platform3 = platform(1100,300,150,50,white)
platform4 = platform(1300,400,150,50,white)
platform5 = platform(1500,300,150,50,white)
platform6 = platform(1800,200,150,50,white)

# ROUND 2
platform7 = platform(2200,300,150,50,white)
platform8 = platform(2400,200,150,50,white)
platform9 = platform(2600,300,150,50,white)
platform10 = platform(2700,400,150,50,white)
platform11 = platform(2900,300,150,50,white)
platform12 = platform(3200,200,150,50,white)

# rOUND 3
platform13 = platform(3400,300,150,50,white)
platform14 = platform(3600,200,150,50,white)
platform15 = platform(3800,300,150,50,white)
platform16 = platform(4000,400,150,50,white)
platform17 = platform(4200,300,150,50,white)
platform18 = platform(4400,200,150,50,white)

# ROUND 4
platform19 = platform(600,300,150,50,white)
platform20 = platform(800,200,150,50,white)
platform21 = platform(1100,300,150,50,white)
platform22 = platform(1300,400,150,50,white)
platform23 = platform(1500,300,150,50,white)
platform24 = platform(1800,200,150,50,white)

platforms = [platform1,platform2,platform3,platform4,platform5,platform6,platform7,platform8,platform9,platform10,platform11,platform12,platform13,platform14,platform15,platform16,platform17,platform18,platform19,platform20,platform21,platform22,platform23,platform24]

# sceond pip

pip1 = pip(600,-160,150,50,white)
pip2 = pip(800,-270,150,50,white)
pip3 = pip(1100,-170,150,50,white)
pip4 = pip(1300,-170,150,50,white)
pip5 = pip(1500,-170,150,50,white)
pip6 = pip(1800,-270,150,50,white)

# ROUND 2
pip7 = pip(2200,-160,150,50,white)
pip8 = pip(2400,-270,150,50,white)
pip9 = pip(2600,-170,150,50,white)
pip10 = pip(2700,-170,150,50,white)
pip11 = pip(2900,-170,150,50,white)
pip12 = pip(3200,-270,150,50,white)

# ROUND 3

# rOUND 3
pip13 = pip(3400,-160,150,50,white)
pip14 = pip(3600,-270,150,50,white)
pip15 = pip(3800,-170,150,50,white)
pip16 = pip(4000,-170,150,50,white)
pip17 = pip(4200,-170,150,50,white)
pip18 = pip(4400,-270,150,50,white)


# ROUND 4

pip19 = pip(600,-160,150,50,white)
pip20 = pip(800,-270,150,50,white)
pip21 = pip(1100,-170,150,50,white)
pip22 = pip(1300,-170,150,50,white)
pip23 = pip(1500,-170,150,50,white)
pip24 = pip(1800,-270,150,50,white)

pips = [pip1,pip2,pip3,pip4,pip5,pip6,pip7,pip8,pip9,pip10,pip11,pip12,pip13,pip14,pip15,pip16,pip17,pip18,pip19,pip20,pip21,pip22,pip23,pip24]#window




class orb: 
    def __init__(self,x,y,height,width,color):
        self.x = x
        self.y = y
        self.height = height
        self.width = width
        self.color = color
        self.rect = pygame.Rect(x,y,height,width)
    def draw(self):
        self.rect.topleft = (self.x,self.y)

        




class particleAndPoint:  
    def __init__(self,x,y,height,width,color):
        self.x = x
        self.y =y
        self.height = height
        self.width = width
        self.color = color
        self.rect = pygame.Rect(x,y,height,width)
    def draw(self):
        self.rect.topleft = (self.x,self.y)
        
particleAndPoints = []
# this is the orbs
orb1 = orb(1500,100,50,550,white)
orb2 = orb(2600,100,50,550,white)

orbes = [orb1,orb2]
platformGroup = pygame.sprite.Group
platformList = []
level = [" ",
             "                                       ",
             "                                                      ",
             "                                                                                                                     ",
             "                                                                p                 p                              p                     p                p                               p                                      p                      p                  p         p                 p                            p                    p                   p                    p                 p                    p                    p",
             "                                                                                                                                                             ",
             "                                                                                                                                                            ",
             "                                                                                                                                                    ",
             "                                                                                                                                                               ",
             "                                                                                                                                                                  ",
             "                                                                                                  ",]
for iy, row in enumerate(level):
    for ix, col in enumerate(row):
        if col == "p":
            new_platforms = particleAndPoint(ix*10, iy*0, 10,1010,(255,255,255))
            particleAndPoints.append(new_platforms)





# the score text
font = pygame.font.Font('Candarai.ttf',60)
score = 0
loltext = font.render("" + str(score), True, (255,255,255))
lolrect = loltext.get_rect()
lolrect.center = ((130,60))

# wow sound anime
wowsound = pygame.mixer.Sound("wows.wav")
explodesound = pygame.mixer.Sound("partexplode.wav")

class particle:
    def __init__(self,x,y):
        self.x = x
        self.y = y
        self.x_vel = random.randrange(-10,13)*1
        self.y_vel = random.randrange(-10,-1)*1
        self.lifetime = 0
    def draw(self,window):
        self.lifetime += 1
        if self.lifetime <30:
            self.x -= self.x_vel
            self.y -= self.y_vel
            pygame.draw.rect(window,(232,255,24),(self.x,self.y, 16,16))
# draw the screen things
def redrawwindow():
    bg = pygame.image.load("bgs.png")
    window.blit(bg,(0,0))
 
#player draw
    bird1.draw()
    for platform in platforms:
        platform.draw()
    for pip in pips:
        pip.draw()
    for particleAndPoint in particleAndPoints:
        particleAndPoint.draw()
    window.blit(loltext,lolrect)
    for orb in orbes:
        orb.draw()
    for particle in particles:
        particle.draw(window)


fps = (30)
clock = pygame.time.Clock()
particles = []
run = True
while run:
    clock.tick(fps)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False



    # if player collides with the obsticles add 1 to the player and delete the obstacle
     
                

    for one in range(len(particleAndPoints)-1,-1,-1):
        if bird1.rect.colliderect(particleAndPoints[one].rect):
            score += 1
            bird1.speed += 0.2
            del particleAndPoints[one]
            explodesound.play()
            loltext = font.render("" + str(score), True, (255,255,255))
            lolrect.center = ((130,60))
            for x in range(60):
                x, y = bird1.rect.center
                particles.append( particle( x, y ) )

 



        # if ball collides with player1 show the particles
        # if ball collides with player1 show the particles
    if bird1.rect.colliderect( orb1.rect ):
        for x in range(60):
            wowsound.play()
            explodesound.play()
            x, y = bird1.rect.center
            particles.append( particle( x, y ) )

        
    if bird1.rect.colliderect( orb2.rect ):
        for x in range(60):
            wowsound.play()
            explodesound.play()
            x, y = bird1.rect.center
            particles.append( particle( x, y ) ) 
    
    keys = pygame.key.get_pressed()

    # bird moving
    bird1.x += bird1.speed
    if not bird1.isJump:
        bird1.y += bird1.speed
        bird1.isJump = False

        if keys[pygame.K_SPACE]:
            bird1.isJump = True


        
        # this part lets you jump on platform
        collide = False
        for platform in platforms:
            if bird1.rect.colliderect(platform.rect):
                collide = False

            
            # this makes the player fall down up to 
            if bird1.rect.bottom >= 500:
                collide = True
                bird1.isJump = False
                bird1.JumpCount = 10
                bird1.y = 500 - bird1.height

        if collide:
            if keys[pygame.K_SPACE]:
                bird1.isJump = True
            bird1.fall = 0


    else:
        if bird1.JumpCount > 0:
            bird1.y -= (bird1.JumpCount*abs(bird1.JumpCount)) * 0.2
            bird1.JumpCount -= 1
        else:
            bird1.JumpCount = 10
            bird1.isJump = False


    # this scrolls my screen right
    if bird1.x > 300:
        
        bird1.x -= bird1.speed
        for platform in platforms:
            platform.x -= bird1.speed
            
        for pip in pips:
            pip.x -=bird1.speed
        for particleAndPoint in particleAndPoints:
            particleAndPoint.x -= bird1.speed
        for orb in orbes:
            orb.x -= bird1.speed



            
    redrawwindow()
    pygame.display.update()
pygame.quit()

导入pygame
随机输入
pygame.init()
#这是尖叫高度
window=pygame.display.set_模式((500500))
#知道我们把尖叫的名字
pygame.display.set_标题(“Noobs Flappy Bird游戏”)
#球员级别
类鸟:
定义初始值(自、x、y、高度、宽度、颜色):
self.x=x
self.y=y
self.bright=[
pygame.image.load(“killers50.png”),
pygame.image.load(“killers51.png”),
pygame.image.load(“killers52.png”),
pygame.image.load(“killers53.png”),
]
self.bleft=[
pygame.image.load(“ms1.png”),
pygame.image.load(“ms2.png”),
pygame.image.load(“ms3.png”),
pygame.image.load(“ms4.png”),
]
self.bright=[pygame.transform.scale(image,(image.get_-width()//15,image.get_-height()//15))用于self.bright中的图像]
self.bleft=[pygame.transform.scale(image,(image.get_-width()//15,image.get_-height()//15))用于self.bleft中的图像]
自我高度=高度
self.width=宽度
self.isJump=False
self.JumpCount=10
self.fall=0
自身速度=5
自行行走=0
self.vel=5
self.color=颜色
self.rect=pygame.rect(x,y,高度,宽度)
self.direction=“向下”
#这使敌人左右移动
def牵引(自):
self.rect.topleft=(self.x,self.y)
如果自行行走+1>=33:
自行行走=0
如果self.vel>0:#左
blit(self.bright[self.Walking%3],(self.x,self.y))
自行行走+=1
其他:#对
blit(self.bleft[self.Walking%3],(self.x,self.y))
自行行走+=1
课程平台:
定义初始值(自、x、y、高度、宽度、颜色):
self.x=x
self.y=y
自我高度=高度
self.width=宽度
self.color=颜色
self.pipis=pygame.image.load(“pip.png”)
self.pipis=pygame.transform.scale(self.pipis,(self.pipis.get_width()//3,self.pipipis.get_height()//3))
self.rect=pygame.rect(x,y,高度,宽度)
def牵引(自):
self.rect.topleft=(self.x,self.y)
blit(self.pipis,self.rect)
pip类:
定义初始值(自、x、y、高度、宽度、颜色):
self.x=x
self.y=y
自我高度=高度
self.width=宽度
self.color=颜色
self.pipis=pygame.image.load(“pipo.png”)
self.pipis=pygame.transform.scale(self.pipis,(self.pipis.get_width()//3,self.pipipis.get_height()//3))
self.rect=pygame.rect(x,y,高度,宽度)
def牵引(自):
self.rect.topleft=(self.x,self.y)
blit(self.pipis,self.rect)
#玩家与敌人
白色=(255255)
bird1=鸟(0400,50,50,白色)
红色=(255,48,48)
平台1=平台(600300150,50,白色)
平台2=平台(800200150,50,白色)
平台3=平台(1100300150,50,白色)
平台4=平台(1300400150,50,白色)
平台5=平台(1500300150,50,白色)
平台6=平台(1800200150,50,白色)
#第二轮
平台7=平台(2200300150,50,白色)
平台8=平台(2400200150,50,白色)
平台9=平台(2600300150,50,白色)
平台10=平台(2700400150,50,白色)
平台11=平台(2900300150,50,白色)
平台12=平台(3200200150,50,白色)
#第三轮
平台13=平台(340300150,50,白色)
平台14=平台(360020150,50,白色)
平台15=平台(3800300150,50,白色)
平台16=平台(4000400150,50,白色)
平台17=平台(4200300150,50,白色)
平台18=平台(440020150,50,白色)
#第四轮
平台19=平台(600300150,50,白色)
平台20=平台(800200150,50,白色)
平台21=平台(1100300150,50,白色)
平台22=平台(1300400150,50,白色)
平台23=平台(1500300150,50,白色)
平台24=平台(1800200150,50,白色)
平台=[平台1、平台2、平台3、平台4、平台5、平台6、平台7、平台8、平台9、平台10、平台11、平台12、平台13、平台14、平台15、平台16、平台17、平台18、平台19、平台20、平台21、平台22、平台23、平台24]
#雪松皮
pip1=pip(600,-160150,50,白色)
pip2=pip(800,-270150,50,白色)
pip3=pip(1100,-170150,50,白色)
pip4=pip(1300,-170150,50,白色)
pip5=pip(1500,-170150,50,白色)
pip6=pip(1800,-270150,50,白色)
#第二轮
pip7=pip(2200,-160150,50,白色)
pip8=pip(2400,-270150,50,白色)
pip9=pip(2600,-170150,50,白色)
pip10=pip(2700,-170150,50,白色)
pip11=pip(2900,-170150,50,白色)
pip12=pip(3200,-270150,50,白色)
#第三轮
#第三轮
pip13=pip(3400,-160150,50,白色)
pip14=pip(3600,-270150,50,白色)
pip15=pip(3800,-170150,50,白色)
pip16=pip(4000,-170150,50,白色)
pip17=pip(4200,-170150,50,白色)
pip18=pip(4400,-270150,50,白色)
#第四轮
pip19=pip(600,-160150,50,白色)
pip20=pip(800,-270150,50,白色)
pip21=pip(1100,-170150,50,白色)
pip22=pip(1300,-170150,50,白色)
pip23=pip(1500,-170150,50,白色)
pip24=pip(1800,-270150,50,白色)
pips=[pip1、pip2、pip3、pip4、pip5、pip6、pip7、pip8、pip9、pip10、pip11、pip12、pip14、pip15、pip16、pip17、pip18、pip19、pip23、pip24]#
类orb:
定义初始值(自、x、y、高度、宽度、颜色):
self.x=x
self.y=y
自我高度=高度
self.width=宽度
self.color=颜色
self.rect=pygame.rect(x,y,高度,宽度)
def牵引(自):
self.rect.topleft=(self.x,self.y)
班