Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我如何把我的pygame变成一个侧滚?_Python_Background_Pygame_Platform_Side Scroller - Fatal编程技术网

Python 我如何把我的pygame变成一个侧滚?

Python 我如何把我的pygame变成一个侧滚?,python,background,pygame,platform,side-scroller,Python,Background,Pygame,Platform,Side Scroller,我正在用pygame构建一个游戏,这个游戏有一个问题,我不知道如何在我的玩家精灵向右移动时使背景和平台慢慢向右滚动 我希望滚动发生在def shift\u world()中 也许有人可以教我如何在背景中添加图像。如果你能利用我正在使用的图书馆那就太好了。我有三个文件:第一个用于游戏,第二个用于精灵,第三个用于设置 main.py: # Sarada's Blaze! - side scrolling platform shooting game import pygame as pg

我正在用pygame构建一个游戏,这个游戏有一个问题,我不知道如何在我的玩家精灵向右移动时使背景和平台慢慢向右滚动

我希望滚动发生在
def shift\u world()

也许有人可以教我如何在背景中添加图像。如果你能利用我正在使用的图书馆那就太好了。我有三个文件:第一个用于游戏,第二个用于精灵,第三个用于设置

 main.py:
 # Sarada's Blaze! - side scrolling platform shooting game

 import pygame as pg 
 import random 
 import os
 from settings import *
 from sprites import *

 class Game:
     def __init__(self):
        # initialize game window, etc
        pg.init()
        pg.mixer.init()
        self.screen = pg.display.set_mode((WIDTH, HEIGHT))
        pg.display.set_caption(TITLE)
        self.clock = pg.time.Clock()
        self.running = True
        self.font_name = pg.font.match_font(FONT_NAME)

     def new(self):
        # start a new game
        self.score = 0
        self.all_sprites = pg.sprite.Group()
        self.platforms = pg.sprite.Group()
        self.player = Player(self)
        self.all_sprites.add(self.player)
        for plat in PLATFORM_LIST:
            p = Platform(*plat)
            self.all_sprites.add(p)
            self.platforms.add(p)
        self.run()


     def run(self):
        # Game loop
        self.playing = True
        while self.playing:
            self.clock.tick(FPS)
            self.events()
            self.update()
            self.draw()

     def update(self):
        # Game Loop - Update
        self.all_sprites.update()
        self.platforms.update()
        # check if player hits a platform - only if falling
        if self.player.vel.y  0:
            hits = pg.sprite.spritecollide(self.player, self.platforms, False)
            if hits:
                self.player.pos.y = hits[0].rect.top
                self.player.vel.y = 0


     def events(self):
        # Game Loop - events
        for event in pg.event.get():
        # check for closing window
            if event.type == pg.QUIT:
                if self.playing:
                    self.playing = False
                self.running = False
            if event.type == pg.KEYDOWN:
                if event.key == pg.K_UP:
                    self.player.jump()

     def draw(self):
        # Game Loop - draw
        self.screen.fill(bgcolor) # I want to change the background color to an image
        self.all_sprites.draw(self.screen)
        self.draw_text(str(self.score), 22, white, WIDTH / 2, 15)
        # after drawing everything, flip the display
        pg.display.flip()

     # How far the world has been scrolled right
     # This is where i want the side scroller stuff to happen
     def shift_world(self):
        # when the user moves left/right, i want to scroll everything
        self.world_shift += shift_x
        # i want all my platforms and the background to scroll when
        # my player sprite moves closer to the right
        for plat in PLATFORM_LIST:
            self.platform.rect.x += shift_x
        if self.pos.x = 500:
            diff = self.pos.x - 500
            self.pos.x = 500
            self.shift_world(- diff)
        if self.pos.x <= 120:
            diff = 120 - self.pos.x
            self.pos.x -= 120
            self.shift_world(diff)

     def show_start_screen(self):
        # game splash/start screen
        self.screen.fill(greenblue)
        self.draw_text(TITLE, 48, red, WIDTH / 2, HEIGHT / 4)
        self.draw_text("left arrow to move left, right arrow to move right, up arrow to jump",   22, blue, WIDTH / 2, HEIGHT / 2)
        self.draw_text("Press any key to begin", 22, green, WIDTH / 2, HEIGHT * 3 /4)
        pg.display.flip()
        self.wait_for_key()

     def show_go_screen(self):
        # game over/continue
        if not self.running:
            return
        self.screen.fill(greenblue)
        self.draw_text("Game Over!", 48, red, WIDTH / 2, HEIGHT / 4)
        self.draw_text("Score: " + str(self.score), 22, blue, WIDTH / 2, HEIGHT / 2)
        self.draw_text("Press any key to continue", 22, green, WIDTH / 2, HEIGHT * 3 /4)
        pg.display.flip()
        self.wait_for_key()

     def wait_for_key(self):
        waiting = True
        while waiting:
            self.clock.tick(FPS)
            for event in pg.event.get():
                if event.type == pg.QUIT:
                    waiting = False
                    self.running = False
                if event.type == pg.KEYUP:
                    waiting = False

     def draw_text(self, text, size, color, x, y):
        font = pg.font.Font(self.font_name, size)
        text_surface = font.render(text, True, color)
        text_rect = text_surface.get_rect()
        text_rect.midtop = (x, y)
        self.screen.blit(text_surface, text_rect)

 g = Game()
 g.show_start_screen()
 while g.running:
      g.new()
      g.show_go_screen()

 pg.quit()

 sprites.py:
 # Sprite classes for platform shooting game
 import pygame as pg
 import random
 from settings import *
 import os
 vec = pg.math.Vector2

 game_folder = os.path.dirname(__file__)
 img_folder = os.path.join(game_folder, "img")

 class Player(pg.sprite.Sprite):
     def __init__(self, game):
         pg.sprite.Sprite.__init__(self)
         self.game = game
         self.image = pg.image.load(os.path.join(img_folder, "sarada_shooting.png")).convert()
         self.image.set_colorkey(black)
         self.rect = self.image.get_rect()
         self.rect.center = (WIDTH / 2, HEIGHT / 2)
         self.pos = vec(WIDTH / 2, HEIGHT / 2)
         self.vel =vec(0, 0)
         self.acc = vec(0, 0)

     def jump(self):
         # jump only if standing on a platform
         self.rect.x += 1
         hits = pg.sprite.spritecollide(self, self.game.platforms, False)
         self.rect.x -= 1
         if hits:
              self.vel.y = -PLAYER_JUMP


     def update(self):
         self.acc = vec(0, PLAYER_GRAV)
         keys = pg.key.get_pressed()
         if keys[pg.K_LEFT]:
             self.acc.x = -PLAYER_ACC
         if keys[pg.K_RIGHT]:
             self.acc.x = PLAYER_ACC


         # apply friction
         self.acc.x += self.vel.x * PLAYER_FRICTION
         # equations of motion
         self.vel += self.acc
         self.pos += self.vel + 0.5 * self.acc
         # wrap around the sides of the screen
         if self.pos.x  WIDTH:
             self.pos.x = 0
         if self.pos.x < 0:
             self.pos.x = WIDTH

         self.rect.midbottom = self.pos

     # i want the platform graphic changed
 class Platform(pg.sprite.Sprite):
     def __init__(self, x, y, w, h):
         pg.sprite.Sprite.__init__(self)
         self.image = pg.Surface((WIDTH, h))
         self.image.fill(greenblue)
         self.rect = self.image.get_rect()
         self.rect.x = x
         self.rect.y = y

 settings.py:
 # game options/settings
 TITLE = "Sarada's Blaze"
 WIDTH = 800
 HEIGHT = 600
 FPS = 60
 FONT_NAME = 'times new roman'

 # Player properties
 PLAYER_ACC = 0.5
 PLAYER_FRICTION = -0.20
 PLAYER_GRAV = 0.8
 PLAYER_JUMP = 20

 # starting platforms
 PLATFORM_LIST = [(0, HEIGHT - 40, WIDTH, 40)]

 # define colors
 white = (255, 255, 255)
 black = (0, 0, 0)
 red = (255, 0, 0) 
 green = (0, 255, 0)
 blue = (0, 0, 255)
 purple = (255, 0, 255) # ENEMY COLOR
 greenblue = (0, 155, 155)
 bgcolor = red
main.py:
#萨拉达的火焰侧滚平台射击游戏
导入pygame作为pg
随机输入
导入操作系统
从设置导入*
从精灵导入*
班级游戏:
定义初始化(自):
#初始化游戏窗口等
第init页()
pg.mixer.init()
self.screen=pg.display.set_模式((宽度、高度))
pg.display.set_标题(标题)
self.clock=pg.time.clock()
self.running=True
self.font\u name=pg.font.match\u font(字体名称)
def新(自我):
#开始新游戏
self.score=0
self.all_sprite=pg.sprite.Group()
self.platforms=pg.sprite.Group()
self.player=player(self)
self.all_精灵添加(self.player)
对于平台列表中的平台:
p=平台(*平台)
self.all_sprite.add(p)
self.platforms.add(p)
self.run()
def运行(自):
#游戏循环
自弹=真
自娱自乐时:
自我时钟滴答声(FPS)
self.events()
self.update()
self.draw()
def更新(自我):
#游戏循环-更新
self.all_sprites.update()
self.platforms.update()
#检查玩家是否撞到平台-仅在摔倒时
如果self.player.y级别为0:
hits=pg.sprite.spritecollide(self.player,self.platforms,False)
如果点击:
self.player.pos.y=点击[0].rect.top
self.player.vel.y=0
def事件(自):
#游戏循环-事件
对于pg.event.get()中的事件:
#检查车窗是否关闭
如果event.type==pg.QUIT:
如果自己玩:
自我表现=错误
self.running=False
如果event.type==pg.KEYDOWN:
如果event.key==pg.K_UP:
self.player.jump()
def牵引(自):
#游戏循环-平局
self.screen.fill(bgcolor)#我想将背景颜色更改为图像
self.all_sprite.draw(self.screen)
自绘文本(str(self.score),22,白色,宽度/2,15)
#绘制完所有内容后,翻转显示器
pg.display.flip()
#世界被向右滚动了多远
#这就是我希望侧卷轴的事情发生的地方
def shift_世界(自我):
#当用户向左/向右移动时,我希望滚动所有内容
self.world\u shift+=shift\u x
#我希望我的所有平台和背景在
#我的玩家精灵向右边移动
对于平台列表中的平台:
self.platform.rect.x+=shift\u x
如果自身位置x=500:
差异=自身位置x-500
自身位置x=500
自我变换世界(-diff)

如果self.pos.x你需要迭代self.platforms
列表,并从每个精灵的x位置减去玩家的
vel.x
,以便移动它们。此外,为平台精灵的位置
self.pos=vec(x,y)
指定一个向量属性,因为
pygame.Rect
s的坐标被截断并转换为整数,因此如果速度向量由浮点数组成,则移动将不准确

def update(self):
    # ...
    self.shift_world()

def shift_world(self):
    # Scroll when the player sprite moves closer to the right.
    if self.player.pos.x >= 500:
        self.player.pos.x = 500  # Stop at 500.
        self.shift_platforms()
    # Scroll when the player sprite moves closer to the left.
    if self.player.pos.x <= 120:
        self.player.pos.x = 120  # Stop at 120.
        self.shift_platforms()

def shift_platforms(self):
    for plat in self.platforms:  # Iterate over the platform sprites.
        plat.pos.x -= self.player.vel.x  # Update the platform's pos vector.
        plat.rect.x = plat.pos.x  # Update the rect.
    self.world_shift -= self.player.vel.x  # For the background.

请阅读并一次只问一个问题。如何用self.player.vel.x减去self.shift_world?我是否给它起了一个新名称和减法,然后将其召回draw方法?我已将其添加到
shift\u platforms
方法中。我不断收到一个错误,说上面的%示例不支持作为方法,而int表示self。shift\u world
world\u shift
不是
shift\u world
。我没有任何命名为它的属性在我的代码中。里面应该放些什么。
# Load it once in the global scope.
BACKGROUND = pg.image.load(os.path.join(img_folder, "background.png")).convert()

def draw(self):
    x = self.world_shift
    self.screen.blit(BACKGROUND, (x % WIDTH, 0))
    self.screen.blit(BACKGROUND, (x % WIDTH - WIDTH - 1, 0))