【问题标题】:PyGame: Nothing HappensPyGame:什么都没有发生
【发布时间】:2014-01-15 20:29:08
【问题描述】:

我正在 pygame 中制作游戏,但当我运行游戏时,什么也没有发生。出现一个黑框,但什么都不做,没有显示等。另外,让我烦恼的是 Python Shell 根本没有显示任何错误。这是主文件的代码:

import pygame
import sys
import random
import pygame.mixer
import math
from constants import *
from player import *

class Game():

    def __init__(self):

        #States (Not country states)
        self.game_state = STATE_INGAME

        #State variables
        #self.stateMenu = 

        #Screen
        size = SCREEN_WIDTH, SCREEN_HEIGHT
        self.screen = pygame.display.set_mode(size)
        pygame.display.set_caption('WIP')
        self.screen_rect = self.screen.get_rect()

        # Player
        self.player = Player(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2)

    def run(self):

        clock = pygame.time.Clock()

        if self.game_state == STATE_INGAME:

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()

            self.player_move()
            self.player.update()
            self.player.render(self.screen)

        clock.tick(100)

    def player_move(self):

        # move player and check for collision at the same time
        self.player.rect.x += self.player.velX
        self.player.rect.y += self.player.velY

Game().run()

我已经检查了很多次播放器文件,完全没有错误。好吧,不是我能看到的。感谢您的帮助!

【问题讨论】:

    标签: python python-2.7 pygame


    【解决方案1】:

    你需要一个while-loop:

    def run(self):
    
        clock = pygame.time.Clock()
        while True:
            if self.game_state == STATE_INGAME:
                for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                        pygame.quit()
                        sys.exit()
                self.player_move()
                self.player.update()
                self.player.render(self.screen)
    
            clock.tick(100)
    

    【讨论】:

    • 该死的。没看到那个。感谢您指出这一点。有时我只是忘记了要添加的部分:/
    • 再次感谢您的回答。但我遇到的另一个问题是我的播放器不动 :( 它会显示但根本不动。我可以在私人聊天中提供代码供您查看。再次感谢。
    • 这里是播放器代码:gist.github.com/GhostFrag1/bd85547df91122bcf1c0 然后主要代码在上面(现在当然是while循环)
    • 初始速度 velXvelY 设置为零,并且这些值永远不会改变。试试this。另外,我添加了pygame.display.flip()
    • 我想通了!在我的主游戏中,我不调用 def handle_inputs abd,因为您知道必须调用它才能工作并生效。谢谢你的帮助:)
    猜你喜欢
    • 2015-03-24
    • 2017-07-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-12
    • 2021-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多