微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

初学者需要帮助无法在窗口上显示红色方块并且窗口在几秒钟内关闭

如何解决初学者需要帮助无法在窗口上显示红色方块并且窗口在几秒钟内关闭

我可以显示游戏窗口,但没有出现红色方块,窗口在几秒钟内关闭。我正在关注本教程:https://www.youtube.com/watch?v=crUF36OkGDw&list=PLkkm3wcQHjT7gn81Wn-e78cAyhwBW3FIc 我已经观看了视频,我的代码中的所有内容都与视频中的一样。我不知道如何解决这个问题,我自己仍在学习 pygame 和 python。这是我的代码中的错误吗?还是我的设置有问题?

EDIT: is traceback something to be worried about? because  I get 
two of them this one: Main.py",line 58,in <module>
g.new()
then this one: Main.py",line 17,in new
self.all_sprites = pygame.sprite.LayedUpdates()
AttributeError: module 'pygame.sprite' has no attribute 
'LayedUpdates

main.py

import pygame
from sprites import *
from config import *
import sys

class Game:
    def __init__(self):
        pygame.init()
        self.screen = pygame.display.set_mode((WIN_WIDTH,WIN_HEIGHT))
        self.clock = pygame.time.clock()
        self.running = True
    
   def new(self):
        # a game starts
        self.playing = True

        self.all_sprites = pygame.sprite.LayedUpdates()
        self.blocks = pygame.sprite.LayedUpdates()
        self.enemies = pygame.sprite.LayedUpdates()
        self.attacks = pygame.sprite.LayedUpdates()

        self.player = Player(self,1,2)

    def main(self):
         # Game Loop
        while self.playing:
            self.events()
            self.update()
            self.draw()
            self.running = False
        
    def events(self):
        #Game Events Loop
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                self.playing = False
                self.running = False 

    def update(self):
        # Game Loop Updates
        self.all_sprites.update()
    
    def draw(self):
        # Game Draw Loop
        self.screen.fill(BLACK)
        self.all_sprites.draw(self.screen) 
        self.clock.tick(FPS)
        pygame.display.update()
    
g = Game()
g.intro_screen()
g.new()
while g.running:
    g.main()
    g.game_over()

pygame.quit()
sys.exit()

精灵.py

import pygame
from config import *
import math
import random

class Player(pygame.sprite.Sprite):
    def __init__(self,game,x,y):
    
        self.game = game
        self._layer = PLAYER_LAYER
        self.groups = self.game.all_sprites
        pygame.sprite.Sprite.__init__(self,self.groups)
    
        self.x = x * TITLESIZE
        self.y = y * TITLESIZE
        self.width = TITLESIZE
        self.height = TITLESIZE
    
    self.image = pygame.Surface([self.width,self.height])
    self.image.fill(RED)
    
    self.rect = self.image.get_rect()
    self.x = self.x
    self.y = self.y
    
    def update(self):
        pass

如果需要,这里是配置文件..

配置文件

WIN_WIDTH = 640
WIN_HEIGHT = 480
TITLESIZE = 32
FPS = 60

PLAYER_LAYER = 1

RED = (255,0)
BLACK = (0,0)

解决方法

您正在创建要在 Game 类中执行的代码,因此它根本没有被执行。尝试取消缩进代码,使其看起来像这样:

class Game:
    # ...
   
# Remove four spaces from the beginning of all theses lines below 
g = Game()
g.intro_screen()
g.new()
while g.running:
    g.main()
    g.game_over()

pygame.quit()
sys.exit()

另外,我不知道是不是复制粘贴错误,但是你的 Player 类的标识也是错误的。在从 self.game = gameself.y = self.y 的代码中添加四个空格,使其看起来像这样:

class Player(pygame.sprite.Sprite):
    def __init__(self,game,x,y):
        # Add four spaces to all those lines below
        self.game = game
        self._layer = PLAYER_LAYER
        self.groups = self.game.all_sprites
        pygame.sprite.Sprite.__init__(self,self.groups)
        
        # ...
    
        self.rect = self.image.get_rect()
        self.x = self.x
        self.y = self.y
    
    # Keep this line as it is,because it's a method of the Player class
    def update(self):
        pass

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。