Hello I trying to randomly spawn copies of my Pipes class then display them to the screen. I set up a random number generator that has a 1/3 chance to get a 1. If it gets a one I made another random number generator to choose a number betweeen 0 and 600(the width of my screen) to be the x coordinate of the new sprite. Then I display that sprite to the screen using the random x coordinate and a predefined y coordinate.
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))
Then I made a group of sprites called allPipes that will hold the new instances of Pipe and make a constructor for Pipe.
allPipes = pygame.sprite.Group()
pipe = Pipes(0)
Then in my gameloop I called the Pipes draw function which randomly spawns the new instances and draw and update the allPipes group but the images for the sprite appear on the screen but then dissapear when the new one appears which makes me believe that its not actually making a new instance of Pipes each time. Can somone please help?
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()
This is the full code:
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)
Aucun commentaire:
Enregistrer un commentaire