1
你好,我試圖隨機產生我的Pipes類的副本,然後將它們顯示到屏幕上。我設置了一個隨機數生成器,有1/3的機會得到1,如果它得到1,我讓另一個隨機數生成器選擇一個0到600之間的數字(我的屏幕寬度)作爲x座標新的精靈。然後使用隨機x座標和預定義的y座標將該精靈顯示到屏幕上。Pygame如何創建Sprite類的重複項然後將它們放入組中?
class Pipes(pygame.sprite.Sprite):
def __init__(self, x):
pygame.sprite.Sprite.__init__(self)
self.img = pygame.image.load('C:\\Users\\Ben\\Documents\\sprite.png').convert()
self.imx = 0
self.imgy = 375
self.setDisplay = pygame.display.get_surface()
def playerRect(self):
self.x = self.imgx
self.y = self.imgy
self.rect = pygame.draw.rect(setDisplay, pygame.Color(0, 0, 255), pygame.Rect(self.x, self.y, 32, 32))
def draw(self):
self.setDisplay.blit(self.img)
def update(self):
self.spawn = random.randint(0, 3)
if self.spawn == 1:
self.spawnx = random.randint(0, 600)
setDisplay.blit(self.img, [self.spawnx, 375])
allPipes.add(new_instance(self.spawnx))
然後我製作了一組叫做allPipes的小精靈,它們將容納Pipe的新實例併爲Pipe構造一個構造函數。
allPipes = pygame.sprite.Group()
pipe = Pipes(0)
然後在我的gameloop我叫管道畫出函數隨機派生的新實例,並繪製和更新allPipes組,但對於精靈的圖像出現在屏幕上,但隨後當新的出現,這使得dissapear我相信它實際上並不是每次都會製作一個新的Pipes實例。可以somone請幫忙嗎?
def gameLoop():
imgx = 10
imgy = 10
lead_x_change = 0
lead_y_change = 0
move_variable = 100
jumpcounter = 0
while True:
for event in pygame.event.get():
#print (event)
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == timer_event:
trail
allPipes = pygame.sprite.RenderPlain(())
player.playerRect()
#setDisplay.blit(background.img, [0, 0])
setDisplay.blit(background.img2, [0, 0])
setDisplay.blit(player.img, [player.imgx, player.imgy])
#setDisplay.blit(background.img, [0, 0])
setDisplay.blit(ground.img, [ground.imgx, ground.imgy])
pipe.update()
allPipes.draw(setDisplay)
for sprite in allPipes:
sprite.update()
這是全碼:
import pygame
import sys
from pygame.locals import *
import random
#creates a clock to count framerate
clock = pygame.time.Clock()
#starts the program
pygame.init()
isFalling = True
allPipes = pygame.sprite.Group()
#creates a window of 800x600
setDisplay = pygame.display.set_mode((800, 600))
pygame.display.set_caption('Menu')
#loads image for sprite
img = pygame.image.load('C:\\Users\\Ben\\Documents\\sprite.png')
allPipes = pygame.sprite.Group()
#player class
class Player(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.img = pygame.image.load('C:\\Users\\Ben\\Documents\\sprite.png').convert()
self.imgx = 0
self.imgy = 375
self.setDisplay = pygame.display.get_surface()
def playerRect(self):
self.x = self.imgx
self.y = self.imgy
self.rect = pygame.draw.rect(setDisplay, pygame.Color(0, 0, 255), pygame.Rect(self.x, self.y, 32, 32))
def draw(self):
self.setDisplay.blit(self.img)
def load(self, filename):
self.img = pygame.image.load('C:\\Users\\Ben\\Documents\\sprite.png').convert_alpha()
class Trail(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.x = player.imgx - 1
self.y = player.imgy - 1
self.rect = pygame.draw.rect(setDisplay, pygame.Color(0, 0, 255), pygame.Rect(self.x, self.y, 10, 10))
self.setDisplay = pygame.display.get_surface()
def trailrect(self):
self.x = player.imgx - 1
self.y = player.imgy - 1
self.rect = pygame.draw.rect(setDisplay, pygame.Color(0, 0, 255), pygame.Rect(self.x, self.y, 10, 10))
def draw(self):
self.setDisplay.blit(self.img)
trail.trailrect()
class Background(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.img = pygame.image.load('C:\\Users\\Ben\\Documents\\background.png').convert()
self.img2 = pygame.image.load('C:\\Users\\Ben\\Documents\\trees1.png').convert()
self.treesx = 0
self.treesy = 70
self.treesrect = pygame.draw.rect(setDisplay, pygame.Color(0, 0, 255),pygame.Rect(self.treesx, self.treesy, 376, 100))
def draw(self):
self.setDisplay.blit(self.img)
self.setDisplay.blit(self.img2)
def load(self, filename):
self.img = pygame.image.load('C:\\Users\\Ben\\Documents\\background.png').convert_alpha()
self.img2 = pygame.image.load('C:\\Users\\Ben\\Documents\\trees1.png').convert_alpha()
class Ground(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.img = pygame.image.load('C:\\Users\\Ben\\Documents\\ground.png').convert()
self.imgx = 0
self.imgy = 400
self.setDisplay = pygame.display.get_surface()
self.x = self.imgx
self.y = self.imgy
self.rect = pygame.draw.rect(setDisplay, pygame.Color(0, 0, 255), pygame.Rect(self.x, self.y, 800, 200))
def draw(self):
self.setDisplay.blit(self.img)
def load(self, filename):
self.img = pygame.image.load('C:\\Users\\Ben\\Documents\\sprite.png').convert_alpha()
class Pipes(pygame.sprite.Sprite):
def __init__(self, x):
pygame.sprite.Sprite.__init__(self)
self.img = pygame.image.load('C:\\Users\\Ben\\Documents\\sprite.png').convert()
self.imx = 0
self.imgy = 375
self.setDisplay = pygame.display.get_surface()
def playerRect(self):
self.x = self.imgx
self.y = self.imgy
self.rect = pygame.draw.rect(setDisplay, pygame.Color(0, 0, 255), pygame.Rect(self.x, self.y, 32, 32))
def draw(self):
self.setDisplay.blit(self.img)
def update(self):
self.spawn = random.randint(0, 3)
if self.spawn == 1:
self.spawnx = random.randint(0, 600)
setDisplay.blit(self.img, [self.spawnx, 375])
allPipes.add(new_instance(self.spawnx))
player = Player()
background = Background()
ground = Ground()
trail = Trail()
timer_event = pygame.USEREVENT + 1
pygame.time.set_timer(timer_event, 250)
trail.trailrect()
pipe = Pipes(0)
def gameLoop():
imgx = 10
imgy = 10
lead_x_change = 0
lead_y_change = 0
move_variable = 100
jumpcounter = 0
while True:
for event in pygame.event.get():
#print (event)
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == timer_event:
trail
allPipes = pygame.sprite.RenderPlain(())
player.playerRect()
#setDisplay.blit(background.img, [0, 0])
setDisplay.blit(background.img2, [0, 0])
setDisplay.blit(player.img, [player.imgx, player.imgy])
#setDisplay.blit(background.img, [0, 0])
setDisplay.blit(ground.img, [ground.imgx, ground.imgy])
pipe.update()
allPipes.draw(setDisplay)
for sprite in allPipes:
sprite.update()
#rect = pygame.draw.rect(setDisplay, pygame.Color(0, 0, 255), pygame.Rect(player.x, player.y, 32, 32))
player.imgy += 30
isFalling = False
groundColliding = False
if isFalling == True:
player.imgy += 0
if player.rect.colliderect(ground.rect):
print("collided")
isFalling = False
groundColliding = True
player.imgy = 370
jumpcounter = 0
if player.rect.colliderect(background.treesrect):
print("c")
keys = pygame.key.get_pressed()
if keys[K_LEFT]:
player.imgx -= 20
if keys[K_RIGHT]:
player.imgx += 20
if keys[K_UP]:
if jumpcounter < 2:
player.imgy -= 50
isFalling = True
jumpcounter += 1
print(player.x, player.y)
print (isFalling, groundColliding)
pygame.display.flip()
#pygame.display.update()
clock.tick(70)
gameLoop()
#start (0, 71)
#length (376, 71)
#width (0, 168)
請確認您的代碼的縮進是正確的,並刪除在代碼中所有前導空格,以使其更具可讀性。如果這裏的代碼和你的代碼完全一樣,那麼看看下面的代碼:'for sprite in allPipes:'之後的行不在循環中執行,因爲它沒有縮進。 – Mailerdaimon
在Pipes的更新方法中添加'new_instance(self.spanx)',但我不認爲你實際上在任何地方定義了new_instance –