我的图片没有通过pygame加载

时间:2014-07-30 12:08:23

标签: python pygame

你可以看到,我刚刚使用Pygame从Python复制了我的游戏。现在,'游戏'不会加载我的图像,我把它们放在同一个文件夹中。

我真的需要帮助,我尝试了几件事,比如重新定位文件夹和东西。

另外,我对python很新。

# This is my first game demonstration

import pygame, sys, random
from pygame import *
pygame.init()


def game():
    #make window called screen and initialize the background

    width, height = 600, 400
    screen=pygame.display.set_mode((width,height))
    pygame.display.set_caption("My Star Catcher Game")
    background=pygame.image.load("bg.jpg")
    background=pygame.transform.scale(background,(width, height))
    screen.blit(background, (0,0))

    #load target image and player

    target=pygame.image.load("star.png")
    target=pygame.transform.scale(target,(20,20))
    target.pos=target.get_rect_()
    screen.blit(target,targetpos)

    player = pygame.image.load("myplayer.png")
    player=pygame.transform.scale(player, (30,30))
    px,py=width/2,height/2
    screren.blit(player, (px,py))

    movex = movey = 0

    speed=[random.randint(1,3),random.randint(1,4)]


    # running of the game loop
    while True:
        #image display updates
        targetpos.move_ip(speed)
        screren.blit(background, (0,0))
        screen.blit(target, targetpos)
        screren.blit(player, (px,py))

        pygame.display.update()

        #keyboard and/or mouse movements
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()

            elif event.type == KEYDOWN:
                if event.key == K_RIGHT:
                    movex = 2
                if event.key == K_LEFT:
                    movex = -2
                if event.key == K_UP:
                    movey = -2
                if event.key == K_DOWN:
                    movey = 2
            elif event.type == KEYUP:
                if event.key == K_RIGHT:
                    movex = 0`enter code here`
                if event.key == K_LEFT:
                    movex = -0
                if event.key == K_UP:
                    movey = -0
                if event.key == K_DOWN:
                    movey = 0
                    px = px + movex
                    py = py + movey



        if targetpos[0]+20>width or targetpos[0]<0:
            speed[0]= -speed[0]

        if targetpos[1]>height or targetpos[1]<0:
            speed[1]= -speed[1]
            targetpos[1]+=speed[1]


enter code here

# python's way of running the main routine
if __name__=="__main__":
    game()

2 个答案:

答案 0 :(得分:0)

您的文件是否在同一目录中?

阅读以下内容:

Why are my pygame images not loading?

Python 2.6: "Couldn't open image" error

答案 1 :(得分:0)

你写了&#39; screren &#39;两次而不是&#39; 屏幕&#39;

    targetpos.move_ip(speed)
    screren.blit(background, (0,0))
    screen.blit(target, targetpos)
    screren.blit(player, (px,py))

    pygame.display.update()

还有一些语法错误,请务必在收到错误时查看代码,以便查找错误。