在pygame中构建键盘控制的游戏。我收到了缩进错误,但我不知道为什么

时间:2013-11-27 04:33:39

标签: python pygame indentation

到目前为止,这是我的游戏。我能够显示播放器并使其与键盘一起移动。在一个单独的程序中,我在范围内随机显示了Enemy类。当我合并这两个程序时,我开始收到一堆缩进错误。如果我修一个,另一个弹出。请帮忙!

import pygame
import os
import random

black = (0,0,0)
white = (255,255,255)
red = (255, 0, 0)
green = (0, 100, 0)


# This class represents the bar at the bottom that the player controls
class Player(object):
    def __init__(self):
        self.image = pygame.image.load("player_one.png").convert()
        self.image.set_colorkey(white)
        self.width = 15
        self.height = 15
        self.x = 940
        self.y = 240

    def handle_keys(self):
        key = pygame.key.get_pressed()
        if key[pygame.K_DOWN]:
            if self.y < 470:
                self.y += self.height
        elif key[pygame.K_UP]:
            if self.y > 0:
                self.y -= self.height
        if key[pygame.K_RIGHT]:
            if self.x < 940:
                self.x += self.width
        elif key[pygame.K_LEFT]:
            if self.x > 0:
                self.x -= self.width

    def draw(self, surface):
        surface.blit(self.image, (self.x, self.y))

class Enemy(object):

    def __init__(self):
        self.image = pygame.image.load(image).convert()
        self.image.set_colorkey(white)
        image_rect = image.get_rect()

        self.rect = self.image.get_rect()

        self.rect.centerx = x
        self.rect.centery = y

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

    def update(self):
        self.rect.topleft = random.randint(60, 220+1), random.randint( 0, 475+1)

class Game():
    def __init__(self):
        pygame.init()
        pygame.display.set_caption('Best Football Game Ever!')

        self.screen = pygame.display.set_mode((1000, 500))

        self.multi_enemies = []

        for i in range(1, 4):
            enemy = Enemy("enemy_"+str(i)+".png")
            enemy.update()
            self.multi_enemies.append(enemy)

    def run(self):

        clock = pygame.time.Clock()
        running = True

        while running:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    running = False

                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_ESCAPE:
                        running = False

                for enemy in self.multi_enemies:
                    enemy.update()

            #---updates----

            # place for updates

            # --- draws ---

            for enemy in self.multi_enemies:
                enemy.draw(self.screen)

            for x in range(60,940,35):
                pygame.draw.line(screen, white, [x, 0], [x, 500], 1)

            player.handle_keys()

            self.screen.fill(green)

            pygame.display.flip()

            clock.tick(20)  

    pygame.quit()

Game().run()

2 个答案:

答案 0 :(得分:3)

您正在将标签与空格混合以进行缩进。这个“有效”的唯一方法就是将tabstop设置为8

由于几乎每个人都使用4个空格进行缩进,因此标签看起来像两个级别的缩进,但Python只将其视为一个


此处标签以黄色突出显示:

enter image description here

答案 1 :(得分:0)

编辑 - 全选

然后转到

格式化 - Untabify

那应该解决你的问题