0
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

4

1 回答 1

0

我想我已经找到了您遇到的问题,首先我为 bg 和 ball 创建了两个图像,然后运行您提供的返回的代码:

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()

试试这个改变,看看它是否有效!如果没有将您收到的错误作为评论发布,我们可以尝试修复它。

于 2013-05-22T17:02:58.607 回答