简体   繁体   English

在pygame中使用精灵时遇到麻烦

[英]Having trouble using sprites in pygame

I've been attempting to learn pygame and more specifically how to use sprites in pygame. 我一直在尝试学习pygame,更具体地说是如何在pygame中使用精灵。 However after a little bit of experimentation the code below is what I ended up with and I was wondering why my player class isn't showing up. 但是,经过一番试验,下面的代码是我最终得到的结果,我想知道为什么我的播放器类没有显示。

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()

First of all, you are not quite using the sprite class correctly. 首先,您不太正确地使用sprite类。 When you define a new sprite, you need at a minimum, two things: an image and a rect. 定义新的精灵时,至少需要两件事:一张图像和一个矩形。 The image is what will be displayed, and the rect defines where it will be placed when drawn on the screen. 图像即为要显示的图像,矩形定义了在屏幕上绘制图像时的放置位置。

For the image, to use a simple rectangle, just create a pygame Surface and fill it. 对于图像,要使用简单的矩形,只需创建一个pygame曲面并将其填充即可。 Then just use the built-in get_rect() command to define the rectangle: 然后只需使用内置的get_rect()命令来定义矩形:

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

Then, you actually need to create an instance of your object. 然后,您实际上需要创建对象的实例。 Python standard is to name classes with capital letters to distinguish them from variables: Python标准是用大写字母命名类,以将其与变量区分开:

class Player(pygame.sprite.Sprite):

Then you create an instance of a player sprite like so: 然后创建一个播放器精灵的实例,如下所示:

player = Player(50, 50)

In your game loop, you are calling draw on the sprite group twice, once before filling the screen, and once after - you only need to do this once, the first one is being drawn over anyway. 在您的游戏循环中,您要在精灵组上调用两次draw,一次是在填充屏幕之前,一次是在调用之后-您只需要执行一次,无论如何都将绘制第一个。 You can also call update() on your group, although it won't do anything until you define an update method on your sprite. 您也可以在组上调用update(),尽管直到您在Sprite上定义了update方法,它都不会做任何事情。

Here is some commented/cleaned up code: 这是一些已注释/清除的代码:

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()

Now, try defining an update method in your sprite, to make it move by changing the rect coordinates, for example 现在,尝试在Sprite中定义一个update方法,以通过更改rect坐标使其移动,例如

self.rect.x += 1  

You should also look at http://www.pygame.org/docs/ref/time.html#pygame.time.Clock to see how to set your framerate. 您还应该查看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