简体   繁体   中英

Having trouble using sprites in pygame

I've been attempting to learn pygame and more specifically how to use sprites in 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. 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. Then just use the built-in get_rect() command to define the rectangle:

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:

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. You can also call update() on your group, although it won't do anything until you define an update method on your sprite.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM