Pygame help


#1

Just a quick question
When I run the game everything runs perfectly execpt when my ship dies and the 'player' explosion ends the game will not end and ive tried to find the problem and i think i just need someone else to take a look at it please

import pygame
import random
from os import path

img_dir = path.join(path.dirname(file), 'img')
snd_dir = path.join(path.dirname(file),'music')

font_name = pygame.font.match_font('comic sans')
def draw_text(surf, text, size, x, y):
font = pygame.font.Font(font_name, size)
text_surface = font.render(text, True, WHITE)
text_rect = text_surface.get_rect()
text_rect.midtop = (x,y)
surf.blit(text_surface, text_rect)

WIDTH = 480
HEIGHT = 600
FPS = 60

define colors

WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)

initialize pygame and create window

pygame.mixer.pre_init(44100, -16, 1, 512)
pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Shmup")
clock = pygame.time.Clock()

def draw_sheild_bar(surf, x, y, pct):
if pct < 0:
pct = 0
BAR_LENGTH = 100
BAR_HEIGHT = 10
fill = (pct / 100) * BAR_LENGTH
outline_rect = pygame.Rect(x, y, BAR_LENGTH, BAR_HEIGHT)
fill_rect = pygame.Rect(x, y, fill, BAR_HEIGHT)
pygame.draw.rect(surf, GREEN, fill_rect)
pygame.draw.rect(surf, WHITE, outline_rect, 2)

class Explostion(pygame.sprite.Sprite):
def init(self, center, size):
pygame.sprite.Sprite.init(self)
self.size = size
self.image = explosion_anim[self.size][0]
self.rect = self.image.get_rect()
self.rect.center = center
self.frame = 0
self.last_update = pygame.time.get_ticks()
self.frame_rate = 75

def update(self):
    now = pygame.time.get_ticks()
    if now - self.last_update > self.frame_rate:
        self.last_update = now
        self.frame += 1
        if self.frame == len(explosion_anim[self.size]):
            self.kill()
        else:
            center = self.rect.center
            self.image = explosion_anim[self.size][self.frame]
            self.rect = self.image.get_rect()
            self.rect.center = center

class Player(pygame.sprite.Sprite):
def init(self):
pygame.sprite.Sprite.init(self)
self.image = pygame.transform.scale(player_img, (50,50))
self.image.set_colorkey(BLACK)
self.rect = self.image.get_rect()
self.radius = 23
self.rect.centerx = WIDTH / 2
self.rect.bottom = HEIGHT - 10
self.speedx = 0
self.speedy = 0
self.shield = 100
self.shoot_delay = 250
self.last_shot = pygame.time.get_ticks()
self.lives = 3

def update(self):
    self.speedx = 0
    self.speedy = 0
    keystate = pygame.key.get_pressed()
    if keystate[pygame.K_d]:
        self.speedx = 6
    if keystate[pygame.K_a]:
        self.speedx = -6
    if keystate[pygame.K_w]:
        self.speedy = -6
    if keystate[pygame.K_s]:
        self.speedy = 6
    if keystate[pygame.K_SPACE]:
        self.shoot()
    self.rect.x += self.speedx
    self.rect.y += self.speedy
    if self.rect.right > WIDTH:
        self.rect.right = WIDTH
    if self.rect.left < 0:
        self.rect.left = 0
    if self.rect.bottom > HEIGHT:
        self.rect.bottom = HEIGHT
    if self.rect.top < 0:
        self.rect.top = 0




def shoot(self):
    now = pygame.time.get_ticks()
    if now - self.last_shot > self.shoot_delay:
        self.last_shot = now
        bullet = Bullets(self.rect.centerx,self.rect.top)
        all_sprites.add(bullet)
        bullets.add(bullet)
        shoot_sound.play()

class Bullets(pygame.sprite.Sprite):
def init(self, x ,y):
pygame.sprite.Sprite.init(self)
self.image = bullet_img
self.image.set_colorkey(BLACK)
self.rect = self.image.get_rect()
self.rect.bottom = y
self.rect.centerx = x
self.speedy = -10

def update(self):
    self.rect.y += self.speedy
    if self.rect.bottom < 0:
        self.kill()

class Pow(pygame.sprite.Sprite):
def init(self, center):
pygame.sprite.Sprite.init(self)
self.type = random.choice(['sheild','gun'])
self.image = powerup_images[self.type]
self.image.set_colorkey(BLACK)
self.rect = self.image.get_rect()
self.rect.center = center
self.speedy = 5

def update(self):
    self.rect.y += self.speedy
    if self.rect.top > HEIGHT:
        self.kill()

class Mob(pygame.sprite.Sprite):
def init(self):
pygame.sprite.Sprite.init(self)
self.image_orig = random.choice(meteor_images)
self.image_orig.set_colorkey(BLACK)
self.image = self.image_orig.copy()
self.rect = self.image.get_rect()
self.radius = self.rect.width / 2
self.rect.x = random.randrange(WIDTH - self.rect.width)
self.rect.y = random.randrange(-150, -100)
self.speedy = random.randrange(1, 8)
self.speedx = random.randrange(-3, 3)
self.rot = 0
self.rot_speed = random.randrange(-8, 8)
self.last_update = pygame.time.get_ticks()

def rotate(self):
    now = pygame.time.get_ticks()
    if now - self.last_update > 50:
        self.last_update = now
        self.rot = (self.rot + self.rot_speed) % 360
        new_image = pygame.transform.rotate(self.image_orig, self.rot)
        old_center = self.rect.center
        self.image = new_image
        self.rect = self.image.get_rect()
        self.rect.center = old_center


def update(self):
    self.rotate()
    self.rect.y += self.speedy
    self.rect.x += self.speedx
    if self.rect.top > HEIGHT + 10 or self.rect.left < 0 or self.rect.right > WIDTH:
        self.rect.x = random.randrange(WIDTH - self.rect.width)
        self.rect.y = random.randrange(-100, -40)
        self.speedy = random.randrange(7, 16)

background = pygame.image.load(path.join(img_dir, "background.png")).convert()
background_rect = background.get_rect()
player_img = pygame.image.load(path.join(img_dir, "ufoBlue.png")).convert()
player_mini_img = pygame.transform.scale(player_img, (25, 19))
player_mini_img.set_colorkey(BLACK)
bullet_img = pygame.image.load(path.join(img_dir, "laserRed01.png")).convert()
meteor_images = []
meteor_list = ['meteorBrown_med1.png', 'meteorBrown_med3.png', 'meteorBrown_small1.png']
for img in meteor_list:
meteor_images.append(pygame.image.load(path.join(img_dir, img)).convert())

explosion_anim = {}
explosion_anim['lg'] = []
explosion_anim['sm'] = []
explosion_anim['player'] = []
for i in range(9):
filename = 'regularExplosion0{}.png'.format(i)
img = pygame.image.load(path.join(img_dir, filename)).convert()
img.set_colorkey(BLACK)
img_lg = pygame.transform.scale(img,(75, 75))
explosion_anim['lg'].append(img_lg)
img_sm = pygame.transform.scale(img, (32, 32))
explosion_anim['sm'].append(img_sm)
filename = 'sonicExplosion0{}.png'.format(i)
img = pygame.image.load(path.join(img_dir, filename)).convert()
img.set_colorkey(BLACK)
explosion_anim['player'].append(img)

powerup_images = {}
powerup_images['sheild'] = pygame.image.load(path.join(img_dir, 'shield_gold.png')).convert()
powerup_images['gun'] = pygame.image.load(path.join(img_dir, 'bolt_gold.png')).convert()

def newmob():
m = Mob()
all_sprites.add(m)
mobs.add(m)

shoot_sound = pygame.mixer.Sound(path.join(snd_dir,'Laser_Shoot10.wav' ))
exp_sound = pygame.mixer.Sound(path.join(snd_dir, "Explosion2.wav"))
pygame.mixer.music.load(path.join(snd_dir, 'gamemusic.ogg'))
shoot_sound.set_volume(0.2)
exp_sound.set_volume(0.2)
pygame.mixer.music.set_volume(0.3)

score = 0
all_sprites = pygame.sprite.Group()
mobs = pygame.sprite.Group()
player = Player()
all_sprites.add(player)
bullets = pygame.sprite.Group()
powerups = pygame.sprite.Group()

for i in range(9):
newmob()
pygame.mixer.music.play(loops = -1)

Game loop

running = True
while running:
# keep loop running at the right speed
clock.tick(FPS)
# Process input (events)
for event in pygame.event.get():
# check for closing window
keystate = pygame.key.get_pressed()
if event.type == pygame.QUIT or keystate[pygame.K_ESCAPE]:
running = False

# Update
all_sprites.update()

hits = pygame.sprite.groupcollide(mobs, bullets, True, True)
for hit in hits:
    score += 50 - hit.radius
    exp_sound.play()
    expl = Explostion(hit.rect.center, 'lg')
    all_sprites.add(expl)
    newmob()
    if random.random() > 0.8:
        pow = Pow(hit.rect.center)
        all_sprites.add(pow)
        powerups.add(pow)
hits = pygame.sprite.spritecollide(player, mobs, True, pygame.sprite.collide_circle)
for hit in hits:
    player.shield -= hit.radius * 1.3
    expl = Explostion(hit.rect.center, 'sm')
    all_sprites.add(expl)
    newmob()
    if player.shield <= 0:
        death_explosion = Explostion(player.rect.center, 'player')
        all_sprites.add(death_explosion)
        player.kill()


    if not player.alive() and not death_explosion.alive():
        running = False

# Draw / render
screen.fill(BLACK)
screen.blit(background, background_rect)
all_sprites.draw(screen)
draw_text(screen, str(score), 18, WIDTH / 2, 10)
draw_sheild_bar(screen, 5, 5, player.shield)
# *after* drawing everything, flip the display
pygame.display.flip()

pygame.quit()


#2

The easiest way we can help you is by copying your code and debugging it in our own environment. Please make it easier to do that by formatting your code correctly.