6125

How do I implement a picture instead of my red rectangle?

I cant seem to add a picture from a file instead of the red rectangle that is being displayed. This is only one class of my code. I have looked at other tutorials on how to do this but i haven't had any luck. When i tried adding the image.load command, I kept getting a black window without anything on it. I would like the image to be displayed instead of the red rectangle, but still with the same x and y values etc.

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

Answer1:

Use the pygame.image.load function to load the image from your hard disk and assign it to the self.image attribute in the __init__ method of your class.

If the file is in a subdirectory you should construct the path with the os.path.join function, so that it works correctly with different operating systems.

The convert (or convert_alpha for images with transparency) method should be called to improve the performance.

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)

Recommend

  • How to fall a sprite over another using And engine in android?
  • (Actionscript 3) Pixel-perfect collision detection between the walls and player?
  • How to calculate coordinates of the gunpoint
  • mouseover and addClass jQuery example
  • ActionScript 3 KeyboardEvent not firing
  • Sonoport how to play audio (actionscript)
  • faster way to apply alpha to a jpeg in an android app?
  • Cocos2D scene memory management
  • Using PathModifier or MoveYModifier to simulate sprite jumping
  • FlashDevelop/OpenFL: How to set default target to neko
  • Move Object along a straight line
  • Can't load image with IMG_Load()
  • custom cursor in actionscript 3 event doesn't work [duplicate]
  • Deleting the proper CCSprite from an array in a schedule
  • How to render a 2d side-scroller game
  • cocos2dx+box2d:Object stops abruptly, tied two ropes
  • How do I implement a picture instead of my red rectangle?
  • How to get the relationship between United Kingdom and England from DBpedia?
  • Why is there no InputManager.getInstance() for me? And injectInputEvent()?
  • Boost dijkstra string edge weight
  • Why do I get blank grey background instead of animations in PyGame?
  • How do I create a bitmap image as spritefont with multiple character regions?
  • Mixing Sprite Kit with Empty Application
  • 2D opengl rotation causes sprite distortion
  • Sprite Issue
  • Force Download with Flash AS3
  • five3d local3dtoglobal
  • How to call functions from sprites in groups?
  • How to Optimize mach_msg_trap
  • How many View Controllers should I have in my game?
  • OpenGL 3.3 on Mac OSX El Capitan with LWJGL
  • How to show dropdown in excel using jrxml (jasper api)?
  • NSLayoutConstraint that would pin a view to the bottom edge of a superview
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • How to stop GridView from loading again when I press back button?
  • Bitwise OR returns boolean when one of operands is nil
  • python draw pie shapes with colour filled
  • sending mail using smtp is too slow
  • costura.fody for a dll that references another dll
  • Binding checkboxes to object values in AngularJs