在pygame上使用类显示图像

时间:2016-11-05 21:31:55

标签: python class pygame

我是使用pygame的新手,我正在尝试创建一个将在pygame上显示图像的类。我想这样做,所以我最终可以将多个图像放到屏幕上并在屏幕上移动它们。目前我只想用这种格式在屏幕上显示图像。任何人都可以向我展示代码并解释原因吗?

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: 
    for 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) 

1 个答案:

答案 0 :(得分:1)

Sprite没有draw()函数,但其​​他函数(即。pygame.sprite.Group)直接使用self.imageself.rect来绘制此精灵。

您可以创建自己的函数draw()来执行相同的操作

    def draw(self, screen):
        screen.blit(self.image, self.rect)

您不必加载Enemy.gif两次。在Enemy类或外部进行,然后使用image作为参数Enemy( ..., image)(或使用文件名Enemy( ..., filename)并加载到内部。)

它可能看起来像这样。

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 -

    for 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:其他Pygame code templatesimage