[英]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.这是我第一次在 python 上编码,我还没有看到任何东西可以帮助我解决我的问题。 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.通常,当您制作精灵时,它具有__init__()
函数 - 显然用于初始化,另外还有一个update()
函数。 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 . update()
函数通常不会将对象绘制到显示/表面,而是调整位置(即:sprite.rect)或更改用于精灵的“外观”(图像),以便稍后绘制。
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.一旦精灵在一个组中,对group .update() 的单次调用将在包含的每个精灵上调用update()
函数。 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()
? all_vampires
精灵组有两个调用 - all_vampires.update()
和all_vampires.draw()
? With just these two calls, all sprites in the group are moved (adjusted/whatever), and painted to the screen.仅通过这两个调用,组中的所有精灵都会被移动(调整/无论如何),并绘制到屏幕上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.