繁体   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