创建一个组以将我的类添加到Pygame中

时间:2017-12-02 00:22:28

标签: python class pygame sprite

我目前正在Pygame制作游戏,并希望在整个屏幕中随机生成多个平台。但是,我似乎无法弄清楚如何创建一个组,以便我可以一次绘制几个精灵。我已尝试使用super.__init__(self)并将self替换为(*Group),但它不起作用。我也试过把它添加到一个组中。我应该如何制作我的小组以及如何正确添加我的精灵呢? 这是代码(我要添加的精灵在这里创建但未绘制):

######## basic setup
import pygame, sys, time, random, threading, tkinter, ctypes
from threading import Timer
from pygame.locals import *
from tkinter import *
pygame.init()
WINDOWHEIGHT = 720
WINDOWWIDTH = 1280
windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32)
pygame.display.set_caption('Hitman Grandma | vB1.0 (prealpha)')
white = (255,255,255)
red = (255,0,0)
black = (0,0,0)
green = (0,255,0)
blue = (0,0,255)
cyan = (0,255,255)
lightgrey = (198,198,198)
windowSurface.fill(lightgrey)
pygame.display.update()
mainClock = pygame.time.Clock()
########## variables
level = 0
touching = False
global x_speed
x_speed = 0
y_speed = 0
leftallowed = True
rightallowed = True
hgturnright = True
hgjumpallowed = True
########### the grandma d'awesome murder sorts
hgimage = pygame.image.load('hgfinal.png')
hgimage.convert_alpha()
class HG(object):
    def __init__(self,x,y,image):
        self.image = image
        self.rect = self.image.get_rect()
        self.x = x
        self.y = y       
    def draw(self):
        windowSurface.blit(self.image,(self.x,self.y))
    def move(self):
        self.x += x_speed
        self.y += y_speed
    def topcollide(self,box):
        if not self.rect.colliderect(box.rect):
            global y_speed
            if y_speed < 20:
                y_speed += 1
            elif y_speed == 20:
                y_speed = 20
            print('shhoooo')
        elif self.rect.colliderect(box.rect):
            y_speed = 0
            print('flop')
hg = HG(0,0,hgimage)
########### land and boundary
lands = pygame.image.load('hgland1.png')
floorland = pygame.transform.scale(lands,(1280,50))
sideedge = pygame.Rect(0,0,1,720),pygame.Rect(1279,0,1,720)
topedge = pygame.Rect(0,0,1280,1)
class Floor(object):
    def __init__(self,x,y,image):
        self.image = image
        self.x = x
        self.y = y
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y
    def draw(self):
        windowSurface.blit(self.image,(self.x,self.y))
class Ground(object):
    def __init__(self,x,y,image):
        self.image = image
        self.x = x
        self.y = y
        self.rect = self.image.get_rect()
    def draw(self):
        windowSurface.blit(self.image,(self.x,self.y))
class Ground(object):
    def __init__(self,x,y,image):
        super.__init__(self)
        self.image = image
        self.x = x
        self.y = y
        self.rect = self.image.get_rect(topleft = (x,y))
    def draw(self):
        windowSurface.blit(self.image,(self.x,self.y))
floor = Floor(0,670,floorland)
platform1 = Ground((random.randint(0,800)),(random.randint(50,620)),lands)
########### WHILE
while True:
########### background
    windowSurface.fill(lightgrey)
########### hg movement
    for event in pygame.event.get():
        if event.type == KEYDOWN:
                if event.key == K_LEFT and hg.x > 0 and leftallowed:
                    x_speed = -4
                    hgturnright = False
                if event.key == K_RIGHT and (hg.x + 36) < WINDOWWIDTH and rightallowed:
                    x_speed = 4
                    hgturnright = True
                if event.key == K_UP and hgjumpallowed:
                    y_speed = -17
        if event.type == KEYUP:
            if event.key == K_RIGHT:
                x_speed = 0
            if event.key == K_LEFT:
                x_speed = 0
        if event.type == KEYDOWN:
########### ctrl+q
            if event.key == K_q and pygame.key.get_mods() & pygame.KMOD_CTRL:
                pygame.quit()
                sys.exit()
                exit
########### [x]
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
                exit
########### drawing and .move()
    floor.draw()
    hg.draw()
    hg.move()
    hg.topcollide(floor)
########### technicals
    pygame.display.update()
    mainClock.tick(40)

这是我想要使用的课程:

class Ground(object):
    def __init__(self,x,y,image):
        super.__init__(self)
        self.image = image
        self.x = x
        self.y = y
        self.rect = self.image.get_rect(topleft = (x,y))
    def draw(self):
        windowSurface.blit(self.image,(self.x,self.y))
platform1 = Ground((random.randint(0,800)),(random.randint(50,620)),lands)

此外,以下是两张图片:this is the grandmother here is the land

1 个答案:

答案 0 :(得分:2)

您的类应继承自pygame.sprite.Sprite,以便将它们放入精灵组,例如: class Platform(pg.sprite.Sprite):。不要忘记调用父类__init__的{​​{1}}方法。然后创建精灵组(super().__init__())和精灵实例,并调用组的all_sprites = pg.sprite.Group()方法来添加精灵。

然后您只需在主循环中调用addall_sprites.update()

all_sprites.draw(screen)