簡體   English   中英

在pygame中使用精靈時遇到麻煩

[英]Having trouble using sprites in pygame

我一直在嘗試學習pygame,更具體地說是如何在pygame中使用精靈。 但是,經過一番試驗,下面的代碼是我最終得到的結果,我想知道為什么我的播放器類沒有顯示。

from pygame.locals import *
import pygame

pygame.init()
size = width, height = 500, 700
screen = pygame.display.set_mode(size)
plr_g = pygame.sprite.Group()

blue = (0,206,209)

class player(pygame.sprite.Sprite):
    def __init__(self, s_x, s_y):
        pygame.sprite.Sprite.__init__(self, plr_g)
        self.s_x = 300
        self.s_y = 300
        self.image.fill(blue)
        #self.plr_img = pygame.image.load("Xpic.png")
        plr_g.add(self)

        self.image = pygame.screen([s_x, s_y])

        self.rect = self.image.get_rect()

plr_g.update()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT: sys.exit()


    plr_g.draw(screen)

    screen.fill((50, 50, 50))

    #player.draw(screen)
    plr_g.draw(screen)

    pygame.display.flip()

首先,您不太正確地使用sprite類。 定義新的精靈時,至少需要兩件事:一張圖像和一個矩形。 圖像即為要顯示的圖像,矩形定義了在屏幕上繪制圖像時的放置位置。

對於圖像,要使用簡單的矩形,只需創建一個pygame曲面並將其填充即可。 然后只需使用內置的get_rect()命令來定義矩形:

self.image = pygame.Surface([s_x, s_y])
self.image.fill(blue)
self.rect = self.image.get_rect()

然后,您實際上需要創建對象的實例。 Python標准是用大寫字母命名類,以將其與變量區分開:

class Player(pygame.sprite.Sprite):

然后創建一個播放器精靈的實例,如下所示:

player = Player(50, 50)

在您的游戲循環中,您要在精靈組上調用兩次draw,一次是在填充屏幕之前,一次是在調用之后-您只需要執行一次,無論如何都將繪制第一個。 您也可以在組上調用update(),盡管直到您在Sprite上定義了update方法,它都不會做任何事情。

這是一些已注釋/清除的代碼:

from pygame.locals import *
import pygame

pygame.init()
# use caps to indicate constants (Python convention)
SIZE = WIDTH, HEIGHT = 500, 700
screen = pygame.display.set_mode(SIZE)
# consider naming your group something more friendly
plr_g = pygame.sprite.Group()

BLUE = (0, 206, 209)

class Player(pygame.sprite.Sprite):
    # assuming s_x and s_y are supposed to be size. What about using width/height?
    def __init__(self, width, height):
        pygame.sprite.Sprite.__init__(self, plr_g)
        # what are these for?
        # self.s_x = 300
        # self.s_y = 300
        self.image = pygame.Surface([width, height])
        self.image.fill(BLUE)
        self.rect = self.image.get_rect()
        # this isn't necessary, since you're adding to the group in the __super__ call
        # plr_g.add(self)
        # set your location via the rect:
        self.rect.center = (WIDTH / 2, HEIGHT / 2)


player = Player(50, 50)
while True:
    for event in pygame.event.get():
        # don't put multiple statements on a single line
        if event.type == pygame.QUIT:
            sys.exit()

    # update sprites in group
    plr_g.update()

    # fill screen, then draw
    screen.fill((50, 50, 50))
    plr_g.draw(screen)
    pygame.display.flip()

現在,嘗試在Sprite中定義一個update方法,以通過更改rect坐標使其移動,例如

self.rect.x += 1  

您還應該查看http://www.pygame.org/docs/ref/time.html#pygame.time.Clock以了解如何設置幀頻。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM