python - Displaying an image using a class on pygame -
i new using pygame , trying make class display image on pygame. want make can put multiple images onto screen , move them around screen. @ moment want display image on screen using format. can show me code , explain why?
import pygame import time import random pygame.init() display_width = 1000 display_height = 750 gameplay = false gamedisplay = pygame.display.set_mode((display_width,display_height)) clock = pygame.time.clock() #starts auto clock updater background_image = pygame.image.load("image/background.jpg").convert() gamedisplay.blit(background_image, [0, 0]) enimg = pygame.image.load('image\enemy.gif') class enemy(pygame.sprite.sprite): def __init__(self,dx,dy,image): pygame.sprite.sprite.__init__(self) self.rect = pygame.image.load('image\enemy.gif') self.image = image self.rect.x = dx self.rect.y = dy def update(self): pass while not gameplay: event in pygame.event.get(): if event.type == pygame.quit: pygame.quit() quit() gamedisplay.blit(background_image, [0, 0]) enimg = enemy(500,500,enimg) enimg.draw(screen) game_change() pygame.display.update() clock.tick(60)
sprite
doesn't have draw()
function other functions (ie. pygame.sprite.group) use directly self.image
, self.rect
draw sprite.
you can create own function draw()
same
def draw(self, screen): screen.blit(self.image, self.rect)
you don't have load enemy.gif
2 times. inside enemy
class or outside , use image argument enemy( ..., image)
(or use filename enemy( ..., filename)
, load inside.)
it this.
import pygame # --- constants --- (upper_case names) display_width = 1000 display_height = 750 display_size = (display_width, display_height) # --- classes --- (camelcase names) class enemy(pygame.sprite.sprite): def __init__(self, dx, dy, filename): pygame.sprite.sprite.__init__(self) self.image = pygame.image.load(filename).convert() self.rect = self.image.get_rect() self.rect.x = dx self.rect.y = dy # - or shorter - #self.rect = self.image.get_rect(x=dx, y=dy) self.moving_left = false self.moving_right = false self.moving_up = false self.moving_down = false def update(self): if self.moving_left: self.rect.x -= 10 if self.moving_right: self.rect.x += 10 if self.moving_up: self.rect.y -= 10 if self.moving_down: self.rect.y += 10 def handle_event(self, event): if event.type == pygame.keydown: if event.key == pygame.k_left: self.moving_left = true elif event.key == pygame.k_right: self.moving_right = true elif event.key == pygame.k_up: self.moving_up = true elif event.key == pygame.k_down: self.moving_down = true elif event.type == pygame.keyup: if event.key == pygame.k_left: self.moving_left = false elif event.key == pygame.k_right: self.moving_right = false elif event.key == pygame.k_up: self.moving_up = false elif event.key == pygame.k_down: self.moving_down = false def draw(self, screen): screen.blit(self.image, self.rect) # --- fuctions --- (lower_case names) # empty # --- main --- (lower_case names) # - init - pygame.init() screen = pygame.display.set_mode(display_size) # - objects - background_image = pygame.image.load("image/background.jpg").convert() enemy = enemy(500,500, 'image/enemy.gif') # - mainloop - clock = pygame.time.clock() gameplay = true while gameplay: # - events - event in pygame.event.get(): if event.type == pygame.quit: gameplay = false elif event.type == pygame.keydown: if event.key == pygame.k_escape: gameplay = false # - objects event handle - enemy.handle_event(event) # - updates - enemy.update() # - draws - screen.blit(background_image, (0, 0)) enemy.draw(screen) pygame.display.update() # - fps - clock.tick(60) # - exit - pygame.quit()
btw: other pygame code templates , image
Comments
Post a Comment