简体   繁体   中英

Sprites won't show up

Cannot figure out why my code won't show sprites when I run it. The code is from a how-to book I found at my library, I have reread the code multiple times and haven't found anything different than what the book says to do. This is my first time coding on python and I haven't seen anything to help me solve my problem yet. This is all the code I've written for the game so far.

import pygame
from pygame import *
from random import randint

pygame.init()
WINDOW_WIDTH = 1100
WINDOW_HEIGHT = 600
WINDOW_RES = (WINDOW_WIDTH, WINDOW_HEIGHT)

WIDTH = 100
HEIGHT = 100

WHITE = (255, 255, 255)

SPAWN_RATE = 360

GAME_WINDOW = display.set_mode(WINDOW_RES)
display.set_caption('Attack of the vampire Pizzas!')

pizza_img = image.load('vampire.png')
pizza_surf = Surface.convert_alpha(pizza_img)
VAMPIRE_PIZZA = transform.scale(pizza_surf, (WIDTH, HEIGHT))

background_img = image.load('restaurant.jpg')
background_surf = Surface.convert_alpha(background_img)
BACKGROUND = transform.scale(background_surf, WINDOW_RES)


class VampireSprite(sprite.Sprite):

    def __init__(self):
        super().__init__()
        self.speed = 2
        self.lane = randint(0, 4)
        all_vampires.add(self)
        self.image = VAMPIRE_PIZZA.copy()
        y = 50 + self.lane * 100
        self.rect = self.image.get_rect(center = (1100, y))


    def update(self, game_window):
        game_window.blit(self.image, (self.rect.x, self.rect.y))


        all_vampires = sprite.Group()
        tile_color = WHITE
        for row in range(6):
           for column in range(11):
               draw.rect(BACKGROUND, tile_color, (WIDTH * column, HEIGHT * row, WIDTH, HEIGHT), 1)

        GAME_WINDOW.blit(BACKGROUND, (0,0))

----------------------------------------------

#Start Main Game Loop
game_running = True
#Game Loop
while game_running:

    for event in pygame.event.get():

        #Exit loop on quit
        if event.type == QUIT:
            game_running = False

   if randint(1, SPAWN_RATE) == 1:
       VampireSprite()

   for vampire in all_vampires:
       vampire.update(GAME_WINDOW)

   display.update()

pygame.quit()
           

The Code seems to have all the correct components, except that it's a bit mixed up.

Generally when you make a sprite, it has the __init__() function - obviously for initialisation, and additionally an update() function. The update() function usually does not draw the object to the display/surface, but adjusts the position (ie: the sprite.rect) or changes the "look" (image) used for the sprite, for drawing later .

Sprites are usually grouped into an aptly-namedSprite Group . Once sprites are in a group, a single call to group .update() will call the update() function on every sprite contained. It's really convenient, and works well.

So tweaking your Vampire Pizza Sprite:

class VampireSprite(sprite.Sprite):

    def __init__(self):
        super().__init__()
        self.speed = 2
        self.lane = randint(0, 4)
        self.image = VAMPIRE_PIZZA.copy()
        y = 50 + self.lane * 100
        self.rect = self.image.get_rect(center = (1100, y))

    def update(self):
        # TODO - do Vampire Pizzas Move?
        #     if so, update the self.rect
        pass

And that's all that's needed. I removed the painting code, this will be handled by a sprite group.

So later on:

# Make a group of vampire sprites
all_vampires = sprite.Group()
all_vampires.add( VampireSprite() )   # Start with a single vampire

# Game Loop
game_running = True
while game_running:

    # handle events
    for event in pygame.event.get():
        #Exit loop on quit
        if event.type == QUIT:
            game_running = False

    # spawn some vampires, maybe
    if randint(1, SPAWN_RATE) == 1:
       all_vampires.add( VampireSprite() ) # add to the group

    # Update/move all vampire sprites
    all_vampires.update()                  # updates every sprite in group

    # repaint the screen
    GAME_WINDOW.blit(BACKGROUND, (0,0))
    # draw some columns(?)
    tile_color = WHITE
    for row in range(6):
       for column in range(11):
           draw.rect(GAME_WINDOW, tile_color, (WIDTH * column, HEIGHT * row, WIDTH, HEIGHT), 1)

    all_vampires.draw()                    # paints every sprite in group

    display.update()

pygame.quit()

There's two calls from the all_vampires sprite group - all_vampires.update() and all_vampires.draw() ? With just these two calls, all sprites in the group are moved (adjusted/whatever), and painted to the screen.

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