gpt4 book ai didi

python - 如何在 Pygame 中滚动我的背景图像?

转载 作者:行者123 更新时间:2023-12-04 15:24:05 27 4
gpt4 key购买 nike

<分区>

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

我有一个移动的物体 我希望它在该物体移动时滚动这是背景图片的视频video现在我只是 blit 背景图片


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()

我希望背景一直向右滚动,因为我的播放器总是向右移动谢谢!

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com