Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.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_Pygame - Fatal编程技术网

Python 使用字符串显示平台(Pygame)

Python 使用字符串显示平台(Pygame),python,pygame,Python,Pygame,我很难在有“p”字符串的区域中找到一个平台来显示。我试图在pygame中使用字符串创建一个关卡,而不是为每个平台设置坐标。这是一个样本 class Level_01(Level): def __init__(self, player): self.background = forest_bg Level.__init__(self, player) platforms = [] x = y = 0

我很难在有“p”字符串的区域中找到一个平台来显示。我试图在pygame中使用字符串创建一个关卡,而不是为每个平台设置坐标。这是一个样本

class Level_01(Level):

    def __init__(self, player):

        self.background = forest_bg

        Level.__init__(self, player)

        platforms = []

        x = y = 0
        level = ['            ',
                 'P           ',
                 '            ',
                 '  P         ',
                 '            ',
                 'PPPPPPPPPPPP',]

        # build the level
        for row in level:
            for col in row:
                if col == 'P':
                    P = Platform(x, y)
                    platforms.append(P)
                x += 90
            y += 90
            x = 0
这是整个项目

import pygame

pygame.init()

#Screen Size
screen_size = 1024, 576
screen_width = 1024
screen_height = 576

#Display Window
screen = pygame.display.set_mode(screen_size)
pygame.display.set_caption('The Adventures of Fresco the Explorer')

#Clock
clock = pygame.time.Clock()

#Colors
black = (0,0,0)
white = (255,255,255)

#Game Start
gameStart = False

#Forest Background
forest_bg = pygame.image.load('forest.png')

#Player Assets and Variables
fresco = pygame.image.load('fresco v2.png').convert()
fresco = pygame.transform.scale(fresco,(32,136))

velocity = 6

move_left = False
move_right = False

#Grass
grass = pygame.image.load('grass.png')
grass = pygame.transform.scale(grass, (90, 90))


#Player Class
class Player(pygame.sprite.Sprite):

    def __init__(self, x, y):

        pygame.sprite.Sprite.__init__(self)

        self.x = 0
        self.y = 0
        self.image = fresco
        self.rect = self.image.get_rect()

    def handle_keys(self):

        key = pygame.key.get_pressed()
        velocity = 8

        #Move Right
        if key[pygame.K_d]:
            self.rect.x += velocity

        #Move Left
        elif key[pygame.K_a]:
            self.rect.x -= velocity
            pygame.transform.flip(self.image, True, False)

    def draw (self, surface):
        surface.blit(self.image, self.rect)

player = Player(0,0)


class Platform(pygame.sprite.Sprite):

    def __init__(self, x, y):
        super().__init__()

        self.image = grass
        self.rect = self.image.get_rect()


class Level(object):
    """ This is a generic super-class used to define a level.
        Create a child class for each level with level-specific
        info. """

    def __init__(self, player):
        """ Constructor. Pass in a handle to player. Needed for when moving platforms
            collide with the player. """
        self.platform_list = pygame.sprite.Group()
        self.player = player

        # Background image
        self.background = None

    # Update everything on this level
    def update(self):
        """ Update everything in this level."""
        self.platform_list.update()

        #self.enemy_list.update() <--- NOTE: Use this late :3

    def draw(self, screen):
        """ Draw everything on this level. """

        # Draw all the sprite lists that we have
        self.platform_list.draw(screen)
        #self.enemy_list.draw(screen) <--- Use it later :3


class Level_01(Level):

    def __init__(self, player):

        self.background = forest_bg

        Level.__init__(self, player)

        platforms = []

        x = y = 0
        level = ['            ',
                 'P           ',
                 '            ',
                 '  P         ',
                 '            ',
                 'PPPPPPPPPPPP',]

        # build the level
        for row in level:
            for col in row:
                if col == 'P':
                    P = Platform(x, y)
                    platforms.append(P)
                x += 90
            y += 90
            x = 0

level_list = []
level_list.append(Level_01(player))

# Set the current level
current_level_no = 0
current_level = level_list[current_level_no]

active_sprite_list = pygame.sprite.Group()
player.level = current_level

#Game Loop
while not gameStart:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            gameStart = True


    #Background
    screen.blit(forest_bg,(0,0))

    #Player Updates
    player.handle_keys()
    player.draw(screen)

    #Level Updates
    current_level.update()
    current_level.draw(screen)
    active_sprite_list.draw(screen)
    # Updates Screen
    pygame.display.update()

    #FPS
    clock.tick(60)

pygame.quit()
导入pygame
pygame.init()
#屏幕大小
屏幕大小=1024576
屏幕宽度=1024
屏幕高度=576
#显示窗口
screen=pygame.display.set_模式(屏幕大小)
pygame.display.set_标题(“探险家弗雷斯科历险记”)
#钟
clock=pygame.time.clock()
#颜色
黑色=(0,0,0)
白色=(255255)
#比赛开始
gameStart=False
#森林背景
forest\u bg=pygame.image.load('forest.png')
#玩家资产和变量
fresco=pygame.image.load('frescov2.png').convert()
湿壁画=pygame.变换.比例(湿壁画,(32136))
速度=6
向左移动=错误
向右移动=错误
#草
grass=pygame.image.load('grass.png')
grass=pygame.transform.scale(grass,(90,90))
#球员级别
职业玩家(pygame.sprite.sprite):
定义初始化(self,x,y):
pygame.sprite.sprite.\uuuuu init\uuuuuuu(自我)
self.x=0
self.y=0
自我形象=壁画
self.rect=self.image.get_rect()
def手柄_键(自):
key=pygame.key.get_pressed()
速度=8
#右移
如果键[pygame.K_d]:
自校正x+=速度
#向左移动
elif key[pygame.K_a]:
自校正x-=速度
pygame.transform.flip(self.image,True,False)
def绘图(自、表面):
blit(self.image,self.rect)
玩家=玩家(0,0)
类平台(pygame.sprite.sprite):
定义初始化(self,x,y):
super()。\uuuu init\uuuuu()
自我形象=草
self.rect=self.image.get_rect()
类级别(对象):
“”“这是用于定义级别的泛型超类。
为每个具有特定级别的级别创建子类
信息。”“”
定义初始(自我,玩家):
“”“构造函数。将句柄传递给播放器。移动平台时需要
与播放器碰撞。”“”
self.platform_list=pygame.sprite.Group()
self.player=玩家
#背景图像
self.background=None
#更新此级别上的所有内容
def更新(自我):
“”“更新此级别中的所有内容。”“”
self.platform_list.update()

#self.敌军列表.update()您的问题是因为您使用argumenst
x,y

 P = Platform(x, y)
但是你对这些信息什么都不做

您必须记住
\uuuu init\uuuu

self.rect.x = x
self.rect.y = y
或更短

self.rect = self.image.get_rect(x=x, y=y)
在类
Player()


顺便说一句:最好在所有类中使用
self.rect.x
self.rect.y
而不是
self.x
self.y
,因为你可以用它来检查碰撞-
player.rect.collide-rect(某些元素.rect)
player.rect.collidepoint(鼠标位置)


甚至
pygame.sprite.Group()
也希望所有元素在
self.rect
中都有位置和大小,因为它使用
self.image
self.rect
来绘制它们。

BTW:将所有类和函数放在
pygame.init()
之前,以使其更具可读性。在类之间创建
player=player(0,0)
,这样就没有人能找到它。看见