2017-12-12 1 views
2

표시되지 않는 빨간색 직사각형 대신 파일에서 그림을 추가하는 것 같습니다. 이것은 내 코드의 한 클래스에 불과합니다. 나는이 작업을 수행하는 방법에 대한 다른 자습서를 살펴 봤지만 행운이 없었습니다. image.load 명령을 추가하려고 할 때 아무 것도 사용하지 않고 검은 색 창이 계속 나타납니다. 나는 이미지가 하드 디스크에서 이미지를로드하고에 할당하지만 여전히 등 같은 x와 y 값빨간색 사각형 대신 그림을 구현하려면 어떻게해야합니까?

def __init__(self, x, y): 
    pygame.sprite.Sprite.__init__(self) 

    self.x_change = 0 
    self.y_change = 0 
    self.jump_duration = 15 
    self.jumping = False 
    self.jump_cooldown = 0 
    self.move_unit = 5 
    self.jump_cooldown_duration = 0 
    self.width = 200 
    self.height = 200 
    self.image = pygame.Surface([self.width, self.height]) 
    self.image.fill(RED) 
    self.rect = self.image.get_rect() 
    self.x = x 
    self.y = y 
    self.rect.x = self.x 
    self.rect.y = self.y 

def move(self, movement): 
    if movement == "": 
     self.x_change = 0 
     self.y_change = 0 

    if movement == "L": 
     self.x_change =- self.move_unit 

    if movement == "R": 
     self.x_change = self.move_unit 

    if movement == "U" and self.jump_duration >= 0 and self.jump_cooldown == 0: 
     self.y_change =- (self.move_unit + 1) 
     self.jump_duration -= 1 
     self.jumping = True 
     print("jumping") 

    if self.jump_duration<0: 
     self.jump_duration=10 
     self.jump_cooldown=10 
     self.jumping=False 

def start_jump_cooldown(self): 
    if self.jumping: 
     self.jump_cooldown_duration = 60 

def update(self, movement): 
    if movement == "L": 
     self.x_change=-self.move_unit 
    if movement == "R": 
     self.x_change=self.move_unit 
    if self.jump_duration<0: 
     self.jump_duration=5 
     self.jump_cooldown_duration=10 
     self.jumping=False 
    if self.jump_cooldown_duration>0: 
     self.jump_cooldown_duration-=1 

    self.x += self.x_change 
    self.y += self.y_change 

    self.rect.x = self.x 
    self.rect.y = self.y 
+0

console/terminal/cmd.exe에서 실행하면 오류 메시지가 나타 납니까? 이미지를로드 할 코드를 보여줍니다. – furas

답변

1

사용 pygame.image.load 기능을 대신 빨간색 사각형으로 표시하고 싶습니다 클래스의 __init__ 메서드에서 self.image 특성

파일이 하위 디렉토리에있는 경우 os.path.join 함수를 사용하여 경로를 구성하여 다른 운영 체제에서 올바르게 작동하도록해야합니다.

성능을 향상 시키려면 convert (또는 투명도가있는 이미지의 경우 convert_alpha) 메서드를 호출해야합니다.

import os 
import pygame 

pygame.init() 
# The display has to be initialized. 
screen = pygame.display.set_mode((640, 480)) 
# Pass the path of the image to pygame.image.load. 
MY_IMAGE = pygame.image.load('image.png').convert_alpha() 
# If the image is in a subdirectory, for example "assets". 
MY_IMAGE = pygame.image.load(os.path.join('assets', 'image.png')).convert_alpha() 


class Player(pygame.sprite.Sprite): 

    def __init__(self, pos): 
     super().__init__() 
     self.image = MY_IMAGE # Assign the image. 
     self.rect = self.image.get_rect(center=pos) 
+1

우리는 상수라는 것을 알려주기 위해'MY_IMAGE'와 같은 대문자를 사용합니다. – skrx

관련 문제