简体   繁体   English

如何在pygame / python中出现精灵?

[英]How do you make a sprite appear in pygame/python?

I'm trying to make a basic (Mario style) game but my sprite(plumber) doesn't appear, it could be hidden behind background? 我正在尝试制作一个基本的(马里奥风格)游戏,但我的精灵(水管工)没有出现,它可能隐藏在背景背后? i'm not exactly sure, i am not getting any errors either. 我不确定,我也没有收到任何错误。

import pygame
import sys
import itertools
import pygame
from pygame.sprite import Sprite

cloud_background = pygame.image.load('clouds.bmp')
brick_tile = pygame.image.load('brick_tile.png')

pink = (255, 64, 64)
w = 640
h = 480
screen = pygame.display.set_mode((w, h))
running = 1

def setup_background():
    screen.fill((pink))
    screen.blit(cloud_background,(0,0))
    brick_width, brick_height = brick_tile.get_width(), brick_tile.get_height()
    for x,y in itertools.product(range(0,640,brick_width),
                                 range(390,480,brick_height)):
        # print(x,y)
        screen.blit(brick_tile, (x, y))
    pygame.display.flip()

while running:
    setup_background()    
    event = pygame.event.poll()
    if event.type == pygame.QUIT: sys.exit()

class plumber(sprite):
    def __init__(   
        self, screen, img_filename, init_position, 
        init_direction, speed):

        Sprite.__init__(self)

        self.screen = screen
        self.speed = speed

        self.base_image = pygame.image.load(Mario_sideways_sprite_2xL.png).convert_alpha()
        self.image = self.base_image


        self.pos = 50,50

First problem found is that you must modify 发现的第一个问题是你必须修改

pygame.image.load(Mario_sideways_sprite_2xL.png)

with something like. 喜欢的东西。

pygame.image.load("Mario_sideways_sprite_2xL.png")

Besides this, the code has many problems that impedes it to work. 除此之外,代码还有许多阻碍其工作的问题。 For example, 例如,

  • you do not instantiate your plumber class . 你没有实例化你的水管工班
  • class plumber(sprite) should be plumber(Sprite) (still better Plumber(Sprite) ) class plumber(sprite)应该是plumber(Sprite) (还是更好的Plumber(Sprite)

You need something like: 你需要这样的东西:

myplumber = Plumber()
allsprites = pygame.sprite.RenderPlain((myplumber, ....))
clock = pygame.time.Clock()

You could see here the main parts of a simple program like yours. 你可以在这里看到像你这样的简单程序的主要部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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