我最近遇到了一个叫做 PyGame Sprite 的东西,我想学习创建 Pygame 精灵,因为它对我的项目非常有用。
这是我的代码:
import pygame
import sys
from pygame.locals import *
white = (255,255,255)
black = (0,0,0)
objs = []
MAIN_BUTTON = 1
class Pane(pygame.sprite.Sprite):
def __init__(self):
pygame.init()
self.font = pygame.font.SysFont('Arial', 25)
pygame.display.set_caption('Box Test')
self.screen = pygame.display.set_mode((600,400), 0, 32)
self.screen.fill((white))
pygame.display.update()
def addRect(self):
self.rect = pygame.draw.rect(self.screen, (black), (175, 75, 200, 100), 2)
pygame.display.update()
def addText(self):
self.screen.blit(self.font.render('Hello', True, (black)), (250, 115))
pygame.display.update()
def delText(self):
self.screen = pygame.display.set_mode((600,400), 0, 32)
self.screen.fill((white))
pygame.display.update()
def mousePosition(self): #Ignore this.
global clickPos
global releasePos
for event in pygame.event.get():
if event.type == MAIN_BUTTON:
self.Pos = pygame.mouse.get_pos()
return MAIN_BUTTON
else:
return False
def groupSp(self):
pygame.sprite.Sprite.__init__(self, group)
if __name__ == '__main__':
Pan3 = Pane()
Pan3.addRect()
Pan3.addText()
Pan3.mousePosition()
group = pygame.sprite.Group(Pan3.addRect(), Pan3.addText())
Pan3.groupSp()
group.sprites()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit(); sys.exit();
所以我想知道你是否可以将 addRect(self) 和 addText(self) 分组为 Sprite?如果是这样,有没有办法可以将它随机放置在屏幕上?或者有没有更好的方法来做到这一点?
提前感谢您的宝贵时间。:)