簡體   English   中英

pygame 不加載圖片 (python)(2.7.5)

[英]pygame doesn't loads picture (python)(2.7.5)

bif="bg.jpeg"

mif="ball.png"

import pygame

import sys

from pygame.locals import *

pygame.init()

screen_size=(640, 360)

screen=pygame.display.set_mode( screen_size, 0, 32)

background=pygame.image.load(bif).convert()

mouse_c=pygame.image.load(bif).convert_alpha()



while (True):

    for event in pygame.event.get():

        if event.type == QUIT:

            pygame.quit()

            sys.exit()

    screen.blit(background, (0,0) )

    x,y = pygame.mouse.get_pos()

    x -=mouse_c.get_width()/2

    y -=mouse_c.get_height()/2

    screeb.blit(mouse_c, (x,y) )

    pygame.display.update()

回溯(最近一次調用最后一次):

文件“C:\\Users\\Ofek\\Desktop\\game\\try.py”,第 12 行,在

背景=pygame.image.load(bif).convert()

pygame.error: 無法打開 C:\\Users\\Ofek\\Desktop\\game\\bg.jpeg

我想我已經發現了您的問題,首先我在油漆中為 bg 和球創建了兩個圖像,然后運行您提供的代碼,返回:

Traceback (most recent call last):
  File ".\pypic.py", line 17, in <module>
    background=pygame.image.load(bif).convert()
pygame.error: Couldn't open bg.jpeg

我認為當前工作目錄可能存在問題,所以我嘗試使用 os.getcwd() + "\\\\" + bg 這導致了同樣的錯誤。 所以我唯一能認為你有問題的是這條線:

bif="bg.jpeg"

我現在認為應該閱讀:

bif="bg.jpg"

如下:

import pygame
import sys
from pygame.locals import *

# Image variables
bif="bg.jpg"  # Have changed this from .jpeg to .jpg
mif="ball.png"

pygame.init()
screen_size = (640, 360)
screen=pygame.display.set_mode( screen_size, 0, 32)
background = pygame.image.load(bif).convert()
mouse_c = pygame.image.load(bif).convert_alpha()

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

    screen.blit(background, (0,0) )
    x,y = pygame.mouse.get_pos()
    x -= mouse_c.get_width() / 2
    y -= mouse_c.get_height() / 2
    screen.blit(mouse_c, (x, y))  # Had to change this because it said screeb!
    pygame.display.update()

嘗試一下這種變化,看看它是否有效! 如果沒有發布您作為評論收到的錯誤,我們可以嘗試修復它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM