【问题标题】:Pygame Not Loading Images CorrectlyPygame 未正确加载图像
【发布时间】:2016-02-19 01:27:50
【问题描述】:

我将图像加载到我的项目中的方式在我学校的 Windows 机器上工作,但是当我回到家并在 OpenSUSE 上以相同格式编写一些代码时,我的图像无法正确加载。

import pygame
import os

pygame.init()

os.environ['SDL_VIDEODRIVER']='windib'

class game(object):

def __init__(self):
    self._running = True
    self._screen = pygame.display.set_mode((800,600))
    self._background = pygame.Surface(self._screen.get_size())
    self._background.fill((0,0,0))
    pygame.display.set_caption('The Spice is Right')
    self.scroller1 = pygame.image.load("scroller.png")
    self.scroller2 = pygame.image.load("scroller.png")
    self.playerCross = pygame.image.load("")
    self.playerRect = self.playerCross.get_rect()
    self.head = pygame.image.load("tyler.png")
    self.headRect = self.head.get_rect()
    self.title = pygame.image.load("spiceMain.png")
    self.timer = 100
    self.attack = False
    self.spiceLevel = 0

    self.headRight = False
    self.headLeft = False
    self.headUp = False
    self.headDown = False

    self.titleScreen = True
    self.gameMode = False
    self.bgy = 0
    self.bgy2 = -600

def run(self):
    while self._running:
        self.handle_input()
        pygame.display.flip()
        self.bgMethod()
        if self.titleScreen:
            self.backgrounScroll()
        if self.gameMode:
            self.bgMethod()
            self.headMethod()
            self.player()

    pygame.quit()
def handle_input(self):
    for evt in pygame.event.get():
        if evt.type == pygame.QUIT:
            self._running = False
        elif evt.type == pygame.KEYDOWN:
            if evt.key == pygame.K_ESCAPE:
                self._running = False
def bgMethod(self):
    self._screen.blit(self._background,(0,0))

def backgroundScroll(self):
    self._screen.blit(self.scroller1,(0,self.bgy))
    self._screen.blit(self.scroller2,(0,self.bgy2))
    self._screen.blit(self.title,(400,300))
    self.bgy += 4
    self.bgy += 4
    if self.bgy >= 600:
        self.bgy == 0
    if self.bgy2 >= 600:
        self.bgy2 == 0
def headMethod(self):
    self._screen.blit(self.head(self.headRect.x,self.headRect.y))
    if self.headRect.x <= 800:
        self.headRight = True
    if self.headRect.x >= 800:
        self.headLeft = True
    if self.headRect.x <= 0:
        self.headRight = True
    if self.headRect.x >= 0:
        self.headLeft = True

    if self.headRight:
        self.headRect.x += 6
    if self.headLeft:
        self.headRect.x -= 6

    if self.headRect.y <= 600:
        self.headDown
    if self.headRect.y >= 600:
        self.headUp
    if self.headRect.y <= 0:
        self.headDown
    if self.headRect.y >= 0:
        self.headUp

    if self.headUp:
        self.rectHead.y -= 6
    if self.headDown:
        self.rectHead.y  += 6
def player(self):
    self._screen.blit(self.playerCross,(pygame.mouse.get_pos()))
def attackMethod(self):
    if self.timer == 100:
        if self.attack:
            self.timer == 0
            self.timer += 1
            if self.timer == 100:
                self.spiceLevel += 1



g= game()
g.run

不,我还不能在 Windows 机器上测试这段代码。

【问题讨论】:

    标签: python pygame opensuse


    【解决方案1】:
    os.environ['SDL_VIDEODRIVER']='windib'
    

    删除此行。

    SDL_VIDEODRIVER 并不是真正需要的,除非你正在做一些奇怪的事情。从this site 可以看出,windib 指的是Win95/98/NT/2000 GDI (Win32)...显然在 OpenSUSE 上不可用。 SDL(以及因此的 pygame)应该有合理的默认值,并为平台选择正确的视频驱动程序,因此无需设置此 envvar。

    【讨论】:

    • 我完全忘记了,这是在我们学校的Windows机器上工作的必需线路,谢谢你的帮助,我回家后测试一下
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-27
    • 1970-01-01
    • 2016-09-25
    • 2021-03-18
    • 2018-02-25
    相关资源
    最近更新 更多