When making the Menu the rest of the code broke - python

How make menu with the code I have now?
# Space Invaders
from pygame import *
import sys
from os.path import abspath, dirname
from random import choice
BASE_PATH = abspath(dirname(__file__))
FONT_PATH = BASE_PATH + '/fonts/'
IMAGE_PATH = BASE_PATH + '/images/'
SOUND_PATH = BASE_PATH + '/sounds/'
# Colors (R, G, B)
WHITE = (255, 255, 255)
GREEN = (78, 255, 87)
YELLOW = (241, 255, 0)
BLUE = (80, 255, 239)
PURPLE = (203, 0, 255)
RED = (237, 28, 36)
SCREEN = display.set_mode((800, 600))
FONT = FONT_PATH + 'space_invaders.ttf'
IMG_NAMES = ['ship', 'mystery',
'enemy1_1', 'enemy1_2',
'enemy2_1', 'enemy2_2',
'enemy3_1', 'enemy3_2',
'explosionblue', 'explosiongreen', 'explosionpurple',
'laser', 'enemylaser']
IMAGES = {name: image.load(IMAGE_PATH + '{}.png'.format(name)).convert_alpha()
for name in IMG_NAMES}
BLOCKERS_POSITION = 450
ENEMY_DEFAULT_POSITION = 65 # Initial value for a new game
ENEMY_MOVE_DOWN = 35
#The insert of the character
class Ship(sprite.Sprite):
def __init__(self):
sprite.Sprite.__init__(self)
self.image = IMAGES['ship']
self.rect = self.image.get_rect(topleft=(375, 540))
self.speed = 5
#The movement of the character
def update(self, keys, *args):
if keys[K_LEFT] and self.rect.x > 10:
self.rect.x -= self.speed
if keys[K_RIGHT] and self.rect.x < 740:
self.rect.x += self.speed
game.screen.blit(self.image, self.rect)
#Shooting of the player and enemy
class Bullet(sprite.Sprite):
def __init__(self, xpos, ypos, direction, speed, filename, side):
sprite.Sprite.__init__(self)
self.image = IMAGES[filename]
self.rect = self.image.get_rect(topleft=(xpos, ypos))
self.speed = speed
self.direction = direction
self.side = side
self.filename = filename
def update(self, keys, *args):
game.screen.blit(self.image, self.rect)
self.rect.y += self.speed * self.direction
if self.rect.y < 15 or self.rect.y > 600:
self.kill()
#The insert of the enemies
class Enemy(sprite.Sprite):
def __init__(self, row, column):
sprite.Sprite.__init__(self)
self.row = row
self.column = column
self.images = []
self.load_images()
self.index = 0
self.image = self.images[self.index]
self.rect = self.image.get_rect()
def toggle_image(self):
self.index += 1
if self.index >= len(self.images):
self.index = 0
self.image = self.images[self.index]
def update(self, *args):
game.screen.blit(self.image, self.rect)
def load_images(self):
images = {0: ['1_2', '1_1'],
1: ['2_2', '2_1'],
2: ['2_2', '2_1'],
3: ['3_1', '3_2'],
4: ['3_1', '3_2'],
}
img1, img2 = (IMAGES['enemy{}'.format(img_num)] for img_num in
images[self.row])
self.images.append(transform.scale(img1, (40, 35)))
self.images.append(transform.scale(img2, (40, 35)))
#The insert of the enemies near each other
class EnemiesGroup(sprite.Group):
def __init__(self, columns, rows):
sprite.Group.__init__(self)
self.enemies = [[None] * columns for _ in range(rows)]
self.columns = columns
self.rows = rows
self.leftAddMove = 0
self.rightAddMove = 0
self.moveTime = 600
self.direction = 1
self.rightMoves = 30
self.leftMoves = 30
self.moveNumber = 15
self.timer = time.get_ticks()
self.bottom = game.enemyPosition + ((rows - 1) * 45) + 35
self._aliveColumns = list(range(columns))
self._leftAliveColumn = 0
self._rightAliveColumn = columns - 1
def update(self, current_time):
if current_time - self.timer > self.moveTime:
if self.direction == 1:
max_move = self.rightMoves + self.rightAddMove
else:
max_move = self.leftMoves + self.leftAddMove
if self.moveNumber >= max_move:
self.leftMoves = 30 + self.rightAddMove
self.rightMoves = 30 + self.leftAddMove
self.direction *= -1
self.moveNumber = 0
self.bottom = 0
for enemy in self:
enemy.rect.y += ENEMY_MOVE_DOWN
enemy.toggle_image()
if self.bottom < enemy.rect.y + 35:
self.bottom = enemy.rect.y + 35
else:
velocity = 10 if self.direction == 1 else -10
for enemy in self:
enemy.rect.x += velocity
enemy.toggle_image()
self.moveNumber += 1
self.timer += self.moveTime
def add_internal(self, *sprites):
super(EnemiesGroup, self).add_internal(*sprites)
for s in sprites:
self.enemies[s.row][s.column] = s
def remove_internal(self, *sprites):
super(EnemiesGroup, self).remove_internal(*sprites)
for s in sprites:
self.kill(s)
self.update_speed()
def is_column_dead(self, column):
return not any(self.enemies[row][column]
for row in range(self.rows))
def random_bottom(self):
col = choice(self._aliveColumns)
col_enemies = (self.enemies[row - 1][col]
for row in range(self.rows, 0, -1))
return next((en for en in col_enemies if en is not None), None)
def update_speed(self):
if len(self) == 1:
self.moveTime = 200
elif len(self) <= 10:
self.moveTime = 400
def kill(self, enemy):
self.enemies[enemy.row][enemy.column] = None
is_column_dead = self.is_column_dead(enemy.column)
if is_column_dead:
self._aliveColumns.remove(enemy.column)
if enemy.column == self._rightAliveColumn:
while self._rightAliveColumn > 0 and is_column_dead:
self._rightAliveColumn -= 1
self.rightAddMove += 5
is_column_dead = self.is_column_dead(self._rightAliveColumn)
elif enemy.column == self._leftAliveColumn:
while self._leftAliveColumn < self.columns and is_column_dead:
self._leftAliveColumn += 1
self.leftAddMove += 5
is_column_dead = self.is_column_dead(self._leftAliveColumn)
class Blocker(sprite.Sprite):
def __init__(self, size, color, row, column):
sprite.Sprite.__init__(self)
self.height = size
self.width = size
self.color = color
self.image = Surface((self.width, self.height))
self.image.fill(self.color)
self.rect = self.image.get_rect()
self.row = row
self.column = column
def update(self, keys, *args):
game.screen.blit(self.image, self.rect)
class Mystery(sprite.Sprite):
def __init__(self):
sprite.Sprite.__init__(self)
self.image = IMAGES['mystery']
self.image = transform.scale(self.image, (75, 35))
self.rect = self.image.get_rect(topleft=(-80, 45))
self.row = 5
self.moveTime = 25000
self.direction = 1
self.timer = time.get_ticks()
self.mysteryEntered = mixer.Sound(SOUND_PATH + 'mysteryentered.wav')
self.mysteryEntered.set_volume(0.3)
self.playSound = True
def update(self, keys, currentTime, *args):
resetTimer = False
passed = currentTime - self.timer
if passed > self.moveTime:
if (self.rect.x < 0 or self.rect.x > 800) and self.playSound:
self.mysteryEntered.play()
self.playSound = False
if self.rect.x < 840 and self.direction == 1:
self.mysteryEntered.fadeout(4000)
self.rect.x += 2
game.screen.blit(self.image, self.rect)
if self.rect.x > -100 and self.direction == -1:
self.mysteryEntered.fadeout(4000)
self.rect.x -= 2
game.screen.blit(self.image, self.rect)
if self.rect.x > 830:
self.playSound = True
self.direction = -1
resetTimer = True
if self.rect.x < -90:
self.playSound = True
self.direction = 1
resetTimer = True
if passed > self.moveTime and resetTimer:
self.timer = currentTime
class EnemyExplosion(sprite.Sprite):
def __init__(self, enemy, *groups):
super(EnemyExplosion, self).__init__(*groups)
self.image = transform.scale(self.get_image(enemy.row), (40, 35))
self.image2 = transform.scale(self.get_image(enemy.row), (50, 45))
self.rect = self.image.get_rect(topleft=(enemy.rect.x, enemy.rect.y))
self.timer = time.get_ticks()
#staticmethod
def get_image(row):
img_colors = ['purple', 'blue', 'blue', 'green', 'green']
return IMAGES['explosion{}'.format(img_colors[row])]
def update(self, current_time, *args):
passed = current_time - self.timer
if passed <= 100:
game.screen.blit(self.image, self.rect)
elif passed <= 200:
game.screen.blit(self.image2, (self.rect.x - 6, self.rect.y - 6))
elif 400 < passed:
self.kill()
class MysteryExplosion(sprite.Sprite):
def __init__(self, mystery, score, *groups):
super(MysteryExplosion, self).__init__(*groups)
self.text = Text(FONT, 20, str(score), WHITE,
mystery.rect.x + 20, mystery.rect.y + 6)
self.timer = time.get_ticks()
def update(self, current_time, *args):
passed = current_time - self.timer
if passed <= 200 or 400 < passed <= 600:
self.text.draw(game.screen)
elif 600 < passed:
self.kill()
class ShipExplosion(sprite.Sprite):
def __init__(self, ship, *groups):
super(ShipExplosion, self).__init__(*groups)
self.image = IMAGES['ship']
self.rect = self.image.get_rect(topleft=(ship.rect.x, ship.rect.y))
self.timer = time.get_ticks()
def update(self, current_time, *args):
passed = current_time - self.timer
if 300 < passed <= 600:
game.screen.blit(self.image, self.rect)
elif 900 < passed:
self.kill()
class Life(sprite.Sprite):
def __init__(self, xpos, ypos):
sprite.Sprite.__init__(self)
self.image = IMAGES['ship']
self.image = transform.scale(self.image, (23, 23))
self.rect = self.image.get_rect(topleft=(xpos, ypos))
def update(self, *args):
game.screen.blit(self.image, self.rect)
class Text(object):
def __init__(self, textFont, size, message, color, xpos, ypos):
self.font = font.Font(textFont, size)
self.surface = self.font.render(message, True, color)
self.rect = self.surface.get_rect(topleft=(xpos, ypos))
def draw(self, surface):
surface.blit(self.surface, self.rect)
class SpaceInvaders(object):
def __init__(self):
mixer.pre_init(44100, -16, 1, 4096)
init()
self.clock = time.Clock()
self.caption = display.set_caption('Astro Fighters')
self.screen = SCREEN
self.background = image.load(IMAGE_PATH + 'background.jpg').convert()
self.startGame = False
self.mainScreen = True
self.gameOver = False
# Counter for enemy starting position (increased each new round)
self.enemyPosition = ENEMY_DEFAULT_POSITION
self.titleText = Text(FONT, 50, 'Space Invaders', WHITE, 164, 155)
self.titleText2 = Text(FONT, 25, 'Press any key to continue', WHITE,
201, 225)
self.gameOverText = Text(FONT, 50, 'Game Over', WHITE, 250, 270)
self.nextRoundText = Text(FONT, 50, 'Next Round', WHITE, 240, 270)
self.enemy1Text = Text(FONT, 25, ' = 10 pts', GREEN, 368, 270)
self.enemy2Text = Text(FONT, 25, ' = 20 pts', BLUE, 368, 320)
self.enemy3Text = Text(FONT, 25, ' = 30 pts', PURPLE, 368, 370)
self.enemy4Text = Text(FONT, 25, ' = ?????', RED, 368, 420)
self.scoreText = Text(FONT, 20, 'Score', WHITE, 5, 5)
self.livesText = Text(FONT, 20, 'Lives ', WHITE, 640, 5)
self.life1 = Life(715, 3)
self.life2 = Life(742, 3)
self.life3 = Life(769, 3)
self.livesGroup = sprite.Group(self.life1, self.life2, self.life3)
def reset(self, score):
self.player = Ship()
self.playerGroup = sprite.Group(self.player)
self.explosionsGroup = sprite.Group()
self.bullets = sprite.Group()
self.mysteryShip = Mystery()
self.mysteryGroup = sprite.Group(self.mysteryShip)
self.enemyBullets = sprite.Group()
self.make_enemies()
self.allSprites = sprite.Group(self.player, self.enemies,
self.livesGroup, self.mysteryShip)
self.keys = key.get_pressed()
self.timer = time.get_ticks()
self.noteTimer = time.get_ticks()
self.shipTimer = time.get_ticks()
self.score = score
self.create_audio()
self.makeNewShip = False
self.shipAlive = True
def make_blockers(self, number):
blockerGroup = sprite.Group()
for row in range(4):
for column in range(9):
blocker = Blocker(10, GREEN, row, column)
blocker.rect.x = 50 + (200 * number) + (column * blocker.width)
blocker.rect.y = BLOCKERS_POSITION + (row * blocker.height)
blockerGroup.add(blocker)
return blockerGroup
def create_audio(self):
self.sounds = {}
for sound_name in ['shoot', 'shoot2', 'invaderkilled', 'mysterykilled',
'shipexplosion']:
self.sounds[sound_name] = mixer.Sound(
SOUND_PATH + '{}.wav'.format(sound_name))
self.sounds[sound_name].set_volume(0.2)
self.musicNotes = [mixer.Sound(SOUND_PATH + '{}.wav'.format(i)) for i
in range(4)]
for sound in self.musicNotes:
sound.set_volume(0.5)
self.noteIndex = 0
def play_main_music(self, currentTime):
if currentTime - self.noteTimer > self.enemies.moveTime:
self.note = self.musicNotes[self.noteIndex]
if self.noteIndex < 3:
self.noteIndex += 1
else:
self.noteIndex = 0
self.note.play()
self.noteTimer += self.enemies.moveTime
#staticmethod
def should_exit(evt):
return evt.type == QUIT or (evt.type == KEYUP and evt.key == K_ESCAPE)
def check_input(self):
self.keys = key.get_pressed()
for e in event.get():
if self.should_exit(e):
sys.exit()
if e.type == KEYDOWN:
if e.key == K_SPACE:
if len(self.bullets) == 0 and self.shipAlive:
if self.score < 1000:
bullet = Bullet(self.player.rect.x + 23,
self.player.rect.y + 5, -1,
15, 'laser', 'center')
self.bullets.add(bullet)
self.allSprites.add(self.bullets)
self.sounds['shoot'].play()
else:
leftbullet = Bullet(self.player.rect.x + 8,
self.player.rect.y + 5, -1,
15, 'laser', 'left')
rightbullet = Bullet(self.player.rect.x + 38,
self.player.rect.y + 5, -1,
15, 'laser', 'right')
self.bullets.add(leftbullet)
self.bullets.add(rightbullet)
self.allSprites.add(self.bullets)
self.sounds['shoot2'].play()
def make_enemies(self):
enemies = EnemiesGroup(10, 5)
for row in range(5):
for column in range(10):
enemy = Enemy(row, column)
enemy.rect.x = 157 + (column * 50)
enemy.rect.y = self.enemyPosition + (row * 45)
enemies.add(enemy)
self.enemies = enemies
def make_enemies_shoot(self):
if (time.get_ticks() - self.timer) > 700 and self.enemies:
enemy = self.enemies.random_bottom()
self.enemyBullets.add(
Bullet(enemy.rect.x + 14, enemy.rect.y + 20, 1, 5,
'enemylaser', 'center'))
self.allSprites.add(self.enemyBullets)
self.timer = time.get_ticks()
def calculate_score(self, row):
scores = {0: 30,
1: 20,
2: 20,
3: 10,
4: 10,
5: choice([50, 100, 150, 300])
}
score = scores[row]
self.score += score
return score
def create_main_menu(self):
self.enemy1 = IMAGES['enemy3_1']
self.enemy1 = transform.scale(self.enemy1, (40, 40))
self.enemy2 = IMAGES['enemy2_2']
self.enemy2 = transform.scale(self.enemy2, (40, 40))
self.enemy3 = IMAGES['enemy1_2']
self.enemy3 = transform.scale(self.enemy3, (40, 40))
self.enemy4 = IMAGES['mystery']
self.enemy4 = transform.scale(self.enemy4, (80, 40))
self.screen.blit(self.enemy1, (318, 270))
self.screen.blit(self.enemy2, (318, 320))
self.screen.blit(self.enemy3, (318, 370))
self.screen.blit(self.enemy4, (299, 420))
def check_collisions(self):
sprite.groupcollide(self.bullets, self.enemyBullets, True, True)
for enemy in sprite.groupcollide(self.enemies, self.bullets,
True, True).keys():
self.sounds['invaderkilled'].play()
self.calculate_score(enemy.row)
EnemyExplosion(enemy, self.explosionsGroup)
self.gameTimer = time.get_ticks()
for mystery in sprite.groupcollide(self.mysteryGroup, self.bullets,
True, True).keys():
mystery.mysteryEntered.stop()
self.sounds['mysterykilled'].play()
score = self.calculate_score(mystery.row)
MysteryExplosion(mystery, score, self.explosionsGroup)
newShip = Mystery()
self.allSprites.add(newShip)
self.mysteryGroup.add(newShip)
for player in sprite.groupcollide(self.playerGroup, self.enemyBullets,
True, True).keys():
if self.life3.alive():
self.life3.kill()
elif self.life2.alive():
self.life2.kill()
elif self.life1.alive():
self.life1.kill()
else:
self.gameOver = True
self.startGame = False
self.sounds['shipexplosion'].play()
ShipExplosion(player, self.explosionsGroup)
self.makeNewShip = True
self.shipTimer = time.get_ticks()
self.shipAlive = False
if self.enemies.bottom >= 540:
sprite.groupcollide(self.enemies, self.playerGroup, True, True)
if not self.player.alive() or self.enemies.bottom >= 600:
self.gameOver = True
self.startGame = False
sprite.groupcollide(self.bullets, self.allBlockers, True, True)
sprite.groupcollide(self.enemyBullets, self.allBlockers, True, True)
if self.enemies.bottom >= BLOCKERS_POSITION:
sprite.groupcollide(self.enemies, self.allBlockers, False, True)
def create_new_ship(self, createShip, currentTime):
if createShip and (currentTime - self.shipTimer > 900):
self.player = Ship()
self.allSprites.add(self.player)
self.playerGroup.add(self.player)
self.makeNewShip = False
self.shipAlive = True
def create_game_over(self, currentTime):
self.screen.blit(self.background, (0, 0))
passed = currentTime - self.timer
if passed < 750:
self.gameOverText.draw(self.screen)
elif 750 < passed < 1500:
self.screen.blit(self.background, (0, 0))
elif 1500 < passed < 2250:
self.gameOverText.draw(self.screen)
elif 2250 < passed < 2750:
self.screen.blit(self.background, (0, 0))
elif passed > 3000:
self.mainScreen = True
for e in event.get():
if self.should_exit(e):
sys.exit()
def main(self):
while True:
action = ''
for e in event.get():
if self.should_exit(e):
sys.exit()
if e.type == 'h':
action = 'help'
if action == 'help' :
helpScreen()
if action == 'playGame' :
playGame()
def playGame():
while True:
if self.mainScreen:
self.screen.blit(self.background, (0, 0))
self.titleText.draw(self.screen)
self.titleText2.draw(self.screen)
self.enemy1Text.draw(self.screen)
self.enemy2Text.draw(self.screen)
self.enemy3Text.draw(self.screen)
self.enemy4Text.draw(self.screen)
self.create_main_menu()
for e in event.get():
if self.should_exit(e):
sys.exit()
if e.type == KEYUP:
# Only create blockers on a new game, not a new round
self.allBlockers = sprite.Group(self.make_blockers(0),
self.make_blockers(1),
self.make_blockers(2),
self.make_blockers(3))
self.livesGroup.add(self.life1, self.life2, self.life3)
self.reset(0)
self.startGame = True
self.mainScreen = False
elif self.startGame:
if not self.enemies and not self.explosionsGroup:
currentTime = time.get_ticks()
if currentTime - self.gameTimer < 3000:
self.screen.blit(self.background, (0, 0))
self.scoreText2 = Text(FONT, 20, str(self.score),
GREEN, 85, 5)
self.scoreText.draw(self.screen)
self.scoreText2.draw(self.screen)
self.nextRoundText.draw(self.screen)
self.livesText.draw(self.screen)
self.livesGroup.update()
self.check_input()
if currentTime - self.gameTimer > 3000:
# Move enemies closer to bottom
self.enemyPosition += ENEMY_MOVE_DOWN
self.reset(self.score)
self.gameTimer += 3000
else:
currentTime = time.get_ticks()
self.play_main_music(currentTime)
self.screen.blit(self.background, (0, 0))
self.allBlockers.update(self.screen)
self.scoreText2 = Text(FONT, 20, str(self.score), GREEN,
85, 5)
self.scoreText.draw(self.screen)
self.scoreText2.draw(self.screen)
self.livesText.draw(self.screen)
self.check_input()
self.enemies.update(currentTime)
self.allSprites.update(self.keys, currentTime)
self.explosionsGroup.update(currentTime)
self.check_collisions()
self.create_new_ship(self.makeNewShip, currentTime)
self.make_enemies_shoot()
elif self.gameOver:
currentTime = time.get_ticks()
# Reset enemy starting position
self.enemyPosition = ENEMY_DEFAULT_POSITION
self.create_game_over(currentTime)
display.update()
self.clock.tick(60)
if __name__ == '__main__':
game = SpaceInvaders()
game.main()
not very good English plz be patient and plz help.
I have start creation of menu in the defmain but I am stuck as I am just getting black screen on computer now. so would like help badly. I think I have broke game and am not calling actual game anymore but I am not sure and I would still need your help to do this.

Related

I can't seem to properly blit surfaces onto screen

So here's my code, I am trying to understand how I can be able to display the surfaces I created onto the main screen which is SCREEN. I drew the maze onto another surface and then I blitted that surface onto the main screen. Then I blitted the PacMan surface onto the main screen, I am also trying to blit the background image onto the screen as well. My question is how do I blit them properly, like for example do I blit the maze first then the PacMan then the background, or what would be the correct order to blit them?
As for the PacManBite function, I am updating the PacManStartSurface to give it the effect that it is biting the dots, but then I also am updating the entire screen, is there a way to properly use the pygame.update() function because I think that's why the screen tends to flicker?
I also wanted to use SCREEN.fill(BLACK) so that it can get rid of any trails the PacMan leaves but for some reason I don't know where to place it in my main loop. Any help would be appreciated.
import pygame
pygame.init()
pygame.display.set_caption("Pac-Man")
# Sets the size of the screen via (WIDTH, HEIGHT)
SCREEN_WIDTH = 475
SCREEN_HEIGHT = 608
# Speed of Characters
SPEED = 1
# Frames per second, how fast the game runs
FPS = 50
# Colors (RED,GREEN,BLUE)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
YELLOW = (255, 255, 0)
BLUE = (0, 0, 255)
HIGH_SCORE = 0
# Sets the WIDTH and HEIGHT of the window
WINDOW = (SCREEN_WIDTH, SCREEN_HEIGHT)
# Displays the screen
SCREEN = pygame.display.set_mode(WINDOW)
CLOCK = pygame.time.Clock()
PacManStartSurface = pygame.transform.scale(pygame.image.load
("PacManStart.png").convert(),
(25, 25))
PacManStartRect = PacManStartSurface.get_rect(topleft =
(((SCREEN_WIDTH - 22) // 2),
(SCREEN_HEIGHT + 225) // 2))
PacManSurface = pygame.transform.scale(pygame.image.load
("PacManRight.png").convert(), (25, 25))
PacManRect = PacManStartSurface.get_rect(topleft =
(((SCREEN_WIDTH - 22) // 2),
(SCREEN_HEIGHT + 225) // 2))
CurrentSurface = PacManStartSurface
CurrentRect = PacManStartRect
BackgroundSurface = pygame.image.load("Background.png").convert()
class PacMan():
def __init__(self):
self.LIVES = 3
class PowerUp(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.transform.scale(pygame.image.load("PowerUp.png")
.convert(), (23, 23))
self.mask = pygame.mask.from_surface(self.image)
class YellowGhost(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.transform.scale(pygame.image.load("YellowGhost.png")
.convert(), (23, 23))
self.rect = self.image.get_rect(topleft = (235, 347))
self.mask = pygame.mask.from_surface(self.image)
class RedGhost(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.transform.scale(pygame.image.load("RedGhost.png")
.convert(), (23, 23))
self.rect = self.image.get_rect(topleft = (235, 347))
self.mask = pygame.mask.from_surface(self.image)
class BlueGhost(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.transform.scale(pygame.image.load("BlueGhost.png")
.convert(), (23, 23))
self.rect = self.image.get_rect(topleft = (235, 347))
self.mask = pygame.mask.from_surface(self.image)
class PinkGhost(pygame.sprite.Sprite):
def __init__(self):
self.image = pygame.transform.scale(pygame.image.load("PinkGhost.png")
.convert(), (23, 23))
self.rect = self.image.get_rect(topleft = (235, 347))
self.mask = pygame.mask.from_surface(self.image)
class Maze():
def __init__(self):
self.DOTS = []
self.WALLS = []
self.BLOCK_WIDTH = 22
self.BLOCK_HEIGHT = 22
self.MARGIN = 3
# 0 - Dots
# 1 - Walls
# 2 - Power Up
# 3 - Empty Space
# 4 - Ghosts
self.MATRIX = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1], \
[1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1], \
[1,2,1,1,0,1,1,1,0,1,0,1,1,1,0,1,1,2,1], \
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1], \
[1,0,1,1,0,1,0,1,1,1,1,1,0,1,0,1,1,0,1], \
[1,0,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,1], \
[1,1,1,1,0,1,1,1,3,1,3,1,1,1,0,1,1,1,1], \
[3,3,3,1,0,1,3,3,3,3,3,3,3,1,0,1,3,3,3], \
[1,1,1,1,0,1,3,1,1,1,1,1,3,1,0,1,1,1,1], \
[0,0,0,0,0,3,3,1,4,4,4,1,3,3,0,0,0,0,0], \
[1,1,1,1,0,1,3,1,1,1,1,1,3,1,0,1,1,1,1], \
[3,3,3,1,0,1,3,3,3,3,3,3,3,1,0,1,3,3,3], \
[1,1,1,1,0,1,3,1,1,1,1,1,3,1,0,1,1,1,1], \
[1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1], \
[1,2,1,1,0,1,1,1,0,1,0,1,1,1,0,1,1,2,1], \
[1,0,0,1,0,0,0,0,0,3,0,0,0,0,0,1,0,0,1], \
[1,1,0,1,0,1,0,1,1,1,1,1,0,1,0,1,0,1,1], \
[1,0,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,1], \
[1,0,1,1,1,1,1,1,0,1,0,1,1,1,1,1,1,0,1], \
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1], \
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
self.MAZE_HEIGHT = (self.MARGIN + self.BLOCK_HEIGHT) \
* len(self.MATRIX)
self.MAZE_WIDTH = (self.MARGIN + self.BLOCK_WIDTH) \
* len(self.MATRIX[0])
def DrawGrid(self, MazeSurface):
for ROW in range(len(self.MATRIX)):
for COLUMN in range(len(self.MATRIX[0])):
if self.MATRIX[ROW][COLUMN] == 0:
self.DOTS.append(pygame.draw.circle(MazeSurface, YELLOW,
[((self.MARGIN + self.BLOCK_WIDTH)
* COLUMN + self.MARGIN) + 9,
((self.MARGIN + self.BLOCK_HEIGHT)
* ROW + self.MARGIN) + 50], 2))
if self.MATRIX[ROW][COLUMN] == 1:
self.WALLS.append(pygame.draw.rect(MazeSurface, WHITE,
[((self.MARGIN + self.BLOCK_WIDTH)
* COLUMN + self.MARGIN) - 2,
((self.MARGIN + self.BLOCK_HEIGHT)
* ROW + self.MARGIN) + 41,
self.BLOCK_WIDTH, self.BLOCK_HEIGHT]))
class Main(Maze):
def __init__(self):
# Inherits Maze class
Maze.__init__(self)
self.DIRECTION = ""
self.SCORE = 0
def Movement(self):
key = pygame.key.get_pressed()
if key[pygame.K_LEFT] and not key[pygame.K_UP] \
and not key[pygame.K_DOWN]:
self.DIRECTION = "LEFT"
elif key[pygame.K_RIGHT] and not key[pygame.K_UP] \
and not key[pygame.K_DOWN]:
self.DIRECTION = "RIGHT"
elif key[pygame.K_UP] and not key[pygame.K_LEFT] \
and not key[pygame.K_RIGHT]:
self.DIRECTION = "UP"
elif key[pygame.K_DOWN] and not key[pygame.K_LEFT] \
and not key[pygame.K_RIGHT]:
self.DIRECTION = "DOWN"
def ContinueMovement(self):
if self.DIRECTION == "LEFT":
CurrentRect.x -= SPEED
self.WallDetection(-1, 0, CurrentRect)
if self.DIRECTION == "RIGHT":
CurrentRect.x += SPEED
self.WallDetection(1, 0, CurrentRect)
if self.DIRECTION == "UP":
CurrentRect.y -= SPEED
self.WallDetection(0, -1, CurrentRect)
if self.DIRECTION == "DOWN":
CurrentRect.y += SPEED
self.WallDetection(0, 1, CurrentRect)
def ChangeImage(self):
global CurrentSurface
if self.DIRECTION == "LEFT":
CurrentSurface = pygame.transform.rotate(PacManSurface, 180)
if self.DIRECTION == "RIGHT":
CurrentSurface = PacManSurface
if self.DIRECTION == "UP":
CurrentSurface = pygame.transform.rotate(PacManSurface, 90)
if self.DIRECTION == "DOWN":
CurrentSurface = pygame.transform.rotate(PacManSurface, 270)
def Teleport(self):
if CurrentRect.right < 0:
CurrentRect.right = SCREEN_WIDTH + 20
if CurrentRect.left > SCREEN_WIDTH:
CurrentRect.right = 0
def WallDetection(self, x, y, CurrentRect):
CurrentRect.right += x
for wall in self.WALLS:
collide = CurrentRect.colliderect(wall)
if collide:
if x < 0:
CurrentRect.left = wall.right
elif x > 0:
CurrentRect.right = wall.left
break
CurrentRect.top += y
for wall in self.WALLS:
collide = CurrentRect.colliderect(wall)
if collide:
if y < 0:
CurrentRect.top = wall.bottom
if y > 0:
CurrentRect.bottom = wall.top
break
def EatDots(self):
for ROW in range(len(self.MATRIX)):
for COLUMN in range(len(self.MATRIX[0])):
for DOT in self.DOTS:
chomp = CurrentRect.colliderect(DOT)
if chomp:
self.DOTS.remove(DOT)
self.MATRIX[ROW][COLUMN] = 3
self.SCORE += 10
return str(self.SCORE)
def EatGhosts(self):
pass
def EatPowerUp(self):
pass
def ShowText(self):
Font = pygame.font.Font("emulogic.ttf", 15)
OneUpText = Font.render("1UP", True, WHITE)
OneUpTextRect = OneUpText.get_rect(center = (70, 10))
OneUpScoreText = Font.render("00", True, WHITE)
UpdateScore = Main.EatDots(self)
if int(self.SCORE) > 0:
OneUpScoreText = Font.render(UpdateScore, True, WHITE)
OneUpScoreRect = OneUpScoreText.get_rect(center =
((SCREEN_WIDTH - 290)
// 2, 26))
HighScoreText = Font.render("High Score", True, WHITE)
HighScoreTextRect = HighScoreText.get_rect(center =
(SCREEN_WIDTH // 2, 10))
HighScoreNumber = Font.render("00", True, WHITE)
HighScoreNumberRect = HighScoreNumber.get_rect(center =
((SCREEN_WIDTH + 90)
// 2, 26))
SCREEN.blit(OneUpText, OneUpTextRect)
SCREEN.blit(OneUpScoreText, OneUpScoreRect)
SCREEN.blit(HighScoreText, HighScoreTextRect)
SCREEN.blit(HighScoreNumber, HighScoreNumberRect)
def PacManBite(self):
SCREEN.blit(PacManStartSurface, PacManStartRect)
pygame.display.update()
Player = Main()
BackgroundImage = pygame.transform.scale(BackgroundSurface,
(Player.MAZE_WIDTH,
Player.MAZE_HEIGHT))
BackgroundRect = BackgroundImage.get_rect(topleft = (0, 41))
MazeSurface = pygame.Surface((SCREEN_WIDTH, SCREEN_HEIGHT - 10))
MazeRect = MazeSurface.get_rect(topleft = ((SCREEN_WIDTH - 475,
SCREEN_HEIGHT - 608)))
Player.DrawGrid(MazeSurface)
'''
pregame = True
while pregame:
if key button pressed:
pregame = False
run = True
'''
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
Player.Movement()
Player.ChangeImage()
Player.Teleport()
Player.ContinueMovement()
Player.PacManBite()
SCREEN.blit(MazeSurface, MazeRect)
MazeSurface.blit(CurrentSurface, CurrentRect)
#SCREEN.blit(BackgroundImage, BackgroundRect)
Player.ShowText()
pygame.display.update()
CLOCK.tick(FPS)
pygame.quit()
You have to blit the CurrentSurface on the MazeSurface, before you blit the MazeSurface on the SCREEN:
MazeSurface.blit(CurrentSurface, CurrentRect)
SCREEN.blit(MazeSurface, MazeRect)

Bullets Are not Drawing on Simple Pygame

I am trying to create a simple bullet function in which am shooting bullets vertically. No bullets seem to be drawing in the program Help would be much appreciated.
class Bullet(pg.sprite.Sprite):
def __init__(self):
super().__init__()
pygame.sprite.Sprite.__init__(self)
self.image = pg.image.load(os.path.join(path, 'img', 'bullet.png'))
self.image = pygame.Surface([4, 10])
self.image.fill(RED)
self.rect = self.image.get_rect()
def update(self):
self.rect.y -= 5 enter code here
elif e.type == pg.KEYDOWN and e.key == pg.K_SPACE:
# Fire a bullet if the user clicks the space button
bullet = Bullet()
# Set the bullet so it is where the player is
bullet.rect.x = player.rect.x
bullet.rect.y = player.rect.y
# Add the bullet to the lists
cars_group.add(bullet)
bullet_list.add(bullet)
# Calculate mechanics for each bullet
for bullet in bullet_list:
# See if it hit a block
block_hit_list = pygame.sprite.spritecollide(bullet, cars_group, True)
# For each block hit, remove the bullet and add to the score
for block in block_hit_list:
bullet_list.remove(bullet)
cars_group.remove(bullet)
disclosure this isnt fully my code
Full code here:
import pygame as pg
import pygame.freetype
import random
import os
import math
os.environ['SDL_VIDEO_CENTERED'] = '1'
path = os.path.dirname(os.path.abspath(__file__))
record_file = os.path.join(path, 'Record', 'record.txt')
try:
with open(record_file, 'x') as f:
f.write(str(0))
except (BaseException, OSError):
pass
SIZE = WIDTH, HEIGHT = 800, 600
GREY = (128, 128, 128)
GREEN = (0, 128, 0)
WHITE = (200, 200, 200)
RED = (230, 0, 0)
rgb = [0, 250, 0]
block = False
pause = [False, True]
count = [0]
car_accident = 0
level = 40
start = 255
blink = 0
hit_old = None
play, out = None, None
def icon():
size, text = 32, '\u0056\u004F'
sur = pg.Surface((size, size), pg.SRCALPHA)
pg.draw.circle(
sur, '#44475a59', (size // 2, size // 2), size // 2)
font_text = pg.freetype.SysFont('Arial', 16, True)
rect = font_text.get_rect(text)
x, y = (size - rect.width) // 2, (size - rect.height) // 2
font_text.render_to(sur, (x, y), text, fgcolor='#ff0000')
pg.display.set_icon(sur)
pg.init()
icon()
pg.display.set_caption('Rally')
screen = pg.display.set_mode(SIZE)
FPS = 120
clock = pg.time.Clock()
font = pygame.freetype.Font(os.path.join(path, 'font', 'seguisym.ttf'), 30)
cars = [pg.image.load(os.path.join(path, 'img', 'car1.png')),
pg.image.load(os.path.join(path, 'img', 'car2.png')),
pg.image.load(os.path.join(path, 'img', 'car3.png'))]
alarm = [pg.image.load(os.path.join(path, 'alarm', '1.png')),
pg.image.load(os.path.join(path, 'alarm', '2.png'))]
sound_car_accident = pg.mixer.Sound(os.path.join(path, 'sound', 'udar.wav'))
sound_canister = pg.mixer.Sound(os.path.join(path, 'sound', 'canister.wav'))
sound_accident = pg.mixer.Sound(os.path.join(path, 'sound', 'accident.wav'))
button_start = pg.image.load(os.path.join(path, 'img', 'btn_play.png'))
button_start_rect = button_start.get_rect(center=(WIDTH // 2, HEIGHT // 2 - 100))
button_stop = pg.image.load(os.path.join(path, 'img', 'btn_exit.png'))
button_stop_rect = button_stop.get_rect(center=(WIDTH // 2, HEIGHT // 2 + 100))
fuel_image = pg.image.load(os.path.join(path, 'img', 'fuel.png'))
canister_image = pg.image.load(os.path.join(path, 'img', 'canister.png'))
water_image = pg.image.load(os.path.join(path, 'img', 'water.png'))
bullet_image = pg.image.load(os.path.join(path, 'img', 'bullet.png'))
u1_event = pg.USEREVENT + 1
pg.time.set_timer(u1_event, random.randrange(6000, 26001, 4000))
u2_event = pg.USEREVENT + 2
pg.time.set_timer(u2_event, random.randrange(13000, 28001, 5000))
class Player(pg.sprite.Sprite):
def __init__(self):
pg.sprite.Sprite.__init__(self)
self.image = pg.image.load(os.path.join(path, 'img', 'car4.png'))
self.orig_image = self.image
self.angle = 0
self.speed = 2
self.acceleration = 0.02
self.rect = self.image.get_rect(center=(WIDTH - 20, HEIGHT - 70))
self.position = pg.math.Vector2()
self.velocity = pg.math.Vector2()
self.vx = 0 # velocity.x for speedometer
def update(self):
self.image = pg.transform.rotate(self.orig_image, self.angle)
self.position += self.velocity
self.rect = self.image.get_rect(center=self.position)
keys = pg.key.get_pressed()
if keys[pg.K_RIGHT]:
self.velocity.x = self.speed
self.angle -= 1
self.vx -= self.acceleration
if self.angle < -25:
self.angle = -25
elif keys[pg.K_LEFT]:
self.velocity.x = -self.speed
self.angle += 1
self.vx -= self.acceleration
if self.angle > 25:
self.angle = 25
else:
self.vx += self.acceleration if self.vx < 0 else 0
self.velocity.x = 0
if self.angle < 0:
self.angle += 1
elif self.angle > 0:
self.angle -= 1
if keys[pg.K_UP]:
self.velocity.y -= self.acceleration
if self.velocity.y < -self.speed:
self.velocity.y = -self.speed
elif keys[pg.K_DOWN]:
self.velocity.y += self.acceleration
if self.velocity.y > self.speed:
self.velocity.y = self.speed
else:
if self.velocity.y < 0:
self.velocity.y += self.acceleration
if self.velocity.y > 0:
self.velocity.y = 0
elif self.velocity.y > 0:
self.velocity.y -= self.acceleration
if self.velocity.y < 0:
self.velocity.y = 0
def for_speedometer(self):
if self.vx <= -0.4:
self.vx = -0.4 # left-right speedometer max
if self.velocity.y < self.vx or self.velocity.y > 0:
self.vx = self.velocity.y
if self.vx >= 1.04:
self.vx = 1.04 # speedometer min for speed=2
return self.vx
class Alarm(pg.sprite.Sprite):
def __init__(self):
pg.sprite.Sprite.__init__(self)
self.images = alarm
self.index = 0
self.range = len(self.images)
self.image = self.images[self.index]
self.rect = self.image.get_rect()
self.speed = 1
def update(self):
self.index += 0.02
self.image = self.images[int(self.index % self.range)]
self.rect.y += self.speed
if self.rect.top > HEIGHT:
self.kill()
class Car(pg.sprite.Sprite):
def __init__(self, x, y, img):
pg.sprite.Sprite.__init__(self)
if img == fuel_image:
self.image = img
self.speed = 0
elif img == canister_image or img == water_image:
self.image = img
self.speed = 1
else:
self.image = pg.transform.flip(img, False, True)
self.speed = random.randint(2, 3)
self.rect = self.image.get_rect(center=(x, y))
def update(self):
self.rect.y += self.speed
if self.rect.top >= HEIGHT:
if self is canister or self is water:
self.kill()
else:
if 40 < player.rect.centerx < WIDTH - 40 \
and player.rect.top < HEIGHT and player.rect.bottom > 0:
count[0] += 1
list_x.remove(self.rect.centerx)
while True:
self.rect.centerx = random.randrange(80, WIDTH, 80)
if self.rect.centerx in list_x:
continue
else:
list_x.append(self.rect.centerx)
self.speed = random.randint(2, 3)
self.rect.bottom = 0
break
class Road(pg.sprite.Sprite):
def __init__(self, x, y):
pg.sprite.Sprite.__init__(self)
self.image = pg.Surface(screen.get_size())
self.image.fill(GREY)
pg.draw.line(self.image, GREEN, (20, 0), (20, 600), 40)
pg.draw.line(self.image, GREEN, (780, 0), (780, 600), 40)
for xx in range(10):
for yy in range(10):
pg.draw.line(
self.image, WHITE,
(40 + xx * 80, 0 if xx == 0 or xx == 9 else 10 + yy * 60),
(40 + xx * 80, 600 if xx == 0 or xx == 9 else 50 + yy * 60), 5)
self.rect = self.image.get_rect(topleft=(x, y))
self.speed = 1
def update(self):
self.rect.y += self.speed
if self.rect.top >= HEIGHT:
self.rect.bottom = 0
class Volume(pg.sprite.Sprite):
def __init__(self, x, y):
pg.sprite.Sprite.__init__(self)
self.image = pg.Surface((20, 140), pg.SRCALPHA)
self.rect = self.image.get_rect(center=(x, y))
self.radius = 10
self.x = self.y = self.radius
self.color_circle = (0, 255, 0, 128)
self.color_rect = (0, 180, 0, 128)
self.color_text = (255, 255, 255)
self.alpha = 255
self.volume = 1
def update(self):
self.image.set_alpha(self.alpha)
pg.draw.rect(
self.image, self.color_rect, [0, 0, *self.rect[2:]],
border_radius=self.radius)
pg.draw.circle(self.image, self.color_circle, (self.x, self.y), self.radius)
text = str(round(self.volume * 100))
text_rect = font.get_rect(text, size=11)
font.render_to(
self.image, (self.x - text_rect[2] / 2., self.y - text_rect[3] / 2.), text,
self.color_text, rotation=0, size=11)
sp = "\U0001F507" if self.volume == 0 else "\U0001F508" if self.volume < 0.2 \
else "\U0001F509" if self.volume < 0.7 else "\U0001F50A"
font.render_to(
screen, (self.rect.x, self.rect.y - font.size), sp, [*WHITE, self.alpha])
def render(self, e_buttons, e_pos):
if self.rect.left < e_pos[0] < self.rect.right and \
self.rect.top < e_pos[1] < self.rect.bottom and \
e_buttons:
self.y = abs(self.rect.top - e_pos[1])
if self.y > self.rect.h - self.radius:
self.y = self.rect.h - self.radius
elif self.y < self.radius:
self.y = self.radius
self.volume = (100 - (self.y - self.radius) / 1.2) / 100.
sound_car_accident.set_volume(self.volume)
sound_canister.set_volume(self.volume)
sound_accident.set_volume(self.volume)
class Speedometer(pg.sprite.Sprite):
def __init__(self):
pg.sprite.Sprite.__init__(self)
w, h = 150, 150
self.radius = 140
self.image = pg.Surface((w, h), pg.SRCALPHA)
self.rect = self.image.get_rect(bottomright=(WIDTH, HEIGHT))
font.render_to(self.image, (w - 50, h - 50), 'km/h', WHITE, size=20)
for deg in range(5, 84, 6):
length = 18 if deg == 5 or deg == 23 or deg == 41 or deg == 59 or deg == 77 else 10
cos = math.cos(math.radians(deg))
sin = math.sin(math.radians(deg))
pg.draw.line(
self.image, WHITE,
(w - self.radius * cos, h - self.radius * sin),
(w - (self.radius - length) * cos, h - (self.radius - length) * sin), 2)
for value, deg in enumerate(range(9, 78, 17)):
cos = math.cos(math.radians(deg))
sin = math.sin(math.radians(deg))
font.render_to(self.image, (
round(w - (self.radius - 30) * cos), round(h - (self.radius - 30) * sin)),
str(value * 100), WHITE, size=15)
def render(self):
s = 30 - player.for_speedometer() * 25 # from 4 to 80
pg.draw.line(
screen, RED, self.rect.bottomright,
(self.rect.right - (self.radius - 10) * math.cos(math.radians(s)),
self.rect.bottom - (self.radius - 10) * math.sin(math.radians(s))), 4)
pg.draw.circle(screen, WHITE, self.rect.bottomright, 25)
vol.update()
class Bullet(pg.sprite.Sprite):
def __init__(self):
super().__init__()
pygame.sprite.Sprite.__init__(self)
self.image = pg.image.load(os.path.join(path, 'img', 'bullet.png'))
self.image = pygame.Surface([4, 10])
self.image.fill(RED)
self.rect = self.image.get_rect()
def update(self):
self.rect.y -= 5
all_sprite = pg.sprite.LayeredUpdates()
cars_group = pg.sprite.Group()
canister_group = pg.sprite.Group()
bullet_list = pg.sprite.Group()
for r in range(2):
bg = Road(x=0, y=0)
all_sprite.add(Road(0, 0 if r == 0 else -HEIGHT), layer=0)
speedometer = Speedometer()
all_sprite.add(speedometer, layer=0)
player = Player()
list_x = []
n = 0
while n < 6:
car_x = random.randrange(80, WIDTH, 80)
if car_x in list_x:
continue
else:
list_x.append(car_x)
cars_group.add(Car(car_x, random.randint(
-cars[0].get_height() * 3, -cars[0].get_height()),
cars[n] if n < 3 else random.choice(cars)))
n += 1
fuel = Car(WIDTH - 80, 40, fuel_image)
canister = Car(0, 0, canister_image)
water = Car(0, 0, water_image)
vol = Volume(20, HEIGHT - 80)
all_sprite.add(*cars_group, layer=1)
all_sprite.add(player, layer=3)
all_sprite.add(fuel, layer=4)
all_sprite.add(vol, layer=4)
def my_record():
with open(record_file, 'r+') as d:
record = d.read()
if count[0] > int(record):
record = str(count[0])
d.seek(0)
d.truncate()
d.write(record)
return record
def home_screen(b):
screen.blit(all_sprite.get_sprite(0).image, (0, 0))
screen.blit(speedometer.image, speedometer.rect)
button_start.set_alpha(start)
button_stop.set_alpha(start)
screen.blit(button_start, button_start_rect)
screen.blit(button_stop, button_stop_rect)
font.render_to(screen, (48, 10), f'Record: {rec}', [
*RED, 255 if count[0] <= record_old else 255 if int(b) % 2 else 0], size=24)
font.render_to(screen, (48, 40), f'Points: {count[0]}', RED, size=24)
font.render_to(screen, (48, 70), f'Accidents: {car_accident}', RED, size=24)
vol.update()
screen.blit(vol.image, vol.rect)
rec = my_record()
record_old = int(rec)
game = True
while game:
for e in pg.event.get():
if e.type == pg.QUIT or e.type == pg.KEYDOWN and e.key == pg.K_ESCAPE:
game = False
elif e.type == pg.KEYDOWN and e.key == pg.K_p and start == 0:
pause.reverse()
if pause[0]:
pg.mouse.set_visible(True)
vol.alpha = 255
else:
pg.mouse.set_visible(False)
vol.alpha = 0
elif e.type == pg.MOUSEMOTION and (start == 255 or pause[0]):
vol.render(e.buttons[0], e.pos)
elif e.type == pg.KEYDOWN and e.key == pg.K_SPACE:
# Fire a bullet if the user clicks the space button
bullet = Bullet()
# Set the bullet so it is where the player is
bullet.rect.x = player.rect.x
bullet.rect.y = player.rect.y
# Add the bullet to the lists
cars_group.add(bullet)
bullet_list.add(bullet)
elif e.type == pg.MOUSEBUTTONDOWN and start == 255:
if e.button == 1:
if button_start_rect.collidepoint(e.pos):
player.angle = 0
player.velocity.x, player.velocity.y = 0, 0
player.position.x, player.position.y = WIDTH - 400, HEIGHT - 70
player.update()
all_sprite.remove_sprites_of_layer(2)
water.kill()
canister.kill()
for cr in cars_group:
cr.speed = random.randint(2, 3)
cr.rect.bottom = 0
level = 40
car_accident = 0
count[0] = 0
start -= 1
record_old = int(rec)
pg.mouse.set_visible(False)
elif button_stop_rect.collidepoint(e.pos):
game = False
elif e.type == u1_event and not pause[0] and not all_sprite.has(water): # water.alive():
all_sprite.add(water, layer=0)
water.rect.center = \
random.randrange(80, WIDTH, 80), -water.rect.h
timer1 = random.randrange(6000, 26001, 4000)
pg.time.set_timer(u1_event, timer1)
elif e.type == u2_event and not pause[0] and not canister_group.has(canister):
canister_group.add(canister)
all_sprite.add(canister, layer=0)
canister.rect.center = \
random.randrange(80, WIDTH, 80), -canister.rect.h
timer2 = random.randrange(13000, 28001, 5000)
pg.time.set_timer(u2_event, timer2)
# Calculate mechanics for each bullet
for bullet in bullet_list:
# See if it hit a block
block_hit_list = pygame.sprite.spritecollide(bullet, cars_group, True)
# For each block hit, remove the bullet and add to the score
for block in block_hit_list:
bullet_list.remove(bullet)
cars_group.remove(bullet)
hit = pg.sprite.spritecollideany(player, cars_group) # hit -> sprite car
if hit and hit.speed != 1:
player.position.x += 50 * random.randrange(-1, 2, 2)
player.angle = 50 * random.randrange(-1, 2, 2)
hit.speed = 1
car_alarm = Alarm()
all_sprite.add(car_alarm, layer=2)
car_alarm.rect.center = hit.rect.center
car_accident += 1
if car_accident > 10:
car_accident = 10
sound_car_accident.play()
if pg.sprite.spritecollide(player, canister_group, True):
level = 40
sound_canister.play()
if pg.sprite.collide_rect(player, water):
if not block:
player.angle = random.randint(60, 90) * random.randrange(-1, 2, 2)
sound_accident.play()
block = True
else:
block = False
if start > 0:
home_screen(blink)
blink = 0 if blink > 99 else blink + .02
if start != 255:
start -= 1
vol.alpha = start
else:
if not pause[0]:
level -= .01
if level < 0 or car_accident > 9:
rec = my_record()
pg.mouse.set_visible(True)
vol.alpha = start
elif level < 10:
rgb[:2] = 250, 0
elif level < 20:
rgb[0] = 250
else:
rgb[:2] = 0, 250
all_sprite.update()
else:
vol.update()
all_sprite.draw(screen)
pg.draw.rect(
screen, rgb,
(fuel.rect.left + 10, fuel.rect.bottom - level - 8, 21, level))
font.render_to(screen, (48, 10), f'accidents: {car_accident}', GREEN, size=24)
font.render_to(screen, (48, HEIGHT - 30), f'{count[0]}', GREEN, size=24)
speedometer.render() # speedometer
pg.display.update()
clock.tick(FPS)
pg.display.set_caption(f'Rally FPS: {int(clock.get_fps())}')
pygame.quit()
# pg.image.save(screen, 'road.jpg')
aos;dfihj ;aslkdjfh ;salkj
askl;fjas;lkfjd
a sdlfkj as;ldkfj as
asld;kj f;laskjd
asl;kjdf;lasjk f
sdla ;fjkas;ldkf j
asdlf jkasl;dkf j
asld flasdkjf ;
You are drawing all the Sprites in the all_sprites Group:
all_sprite.draw(screen)
However, t he bullets are not in this Group Either add the bullets to the all_sprites Group or draw the Sprites contained in the bullet_list Group.
Either
elif e.type == pg.KEYDOWN and e.key == pg.K_SPACE:
# [...]
bullet_list.add(bullet)
bullet_list.draw(screen)
or
bullet_list.draw(screen)
Remove the bullets form all Groups using kill():
for bullet in bullet_list:
# See if it hit a block
block_hit_list = pygame.sprite.spritecollide(bullet, cars_group, True)
# For each block hit, remove the bullet and add to the score
for block in block_hit_list:
bullet.kill()

highscore in pygame game

I've made a game in pygame and I want to make it possible to have a high score thaat will be displayed at the the top of the show_go_screen. I just need a simple solution - I don't need to write who has the highest score, just the number itself at the top of the show_go_screen. I know there are already questions about this on Stack Overflow but the solutions don't work for me.
code:
# Pygame template
# Attributions
# (Heart) Icons made by Freepik from www.flaticon.com
# (Play)Background vector created by freepik - www.freepik.com
# (GameOver)Background vector created by pikisuperstar - www.freepik.com
# (Backing)Frozen Jam by tgfcoder <https://twitter.com/tgfcoder> licensed under CC-BY-3 <http://creativecommons.org/licenses/by/3.0/>
# (Graphics)Art by Kenney.nl
# Imports
import pygame as pg
import random
from os import path
# images / sound
img_dir = path.join(path.dirname(__file__), 'img')
snd_dir = path.join(path.dirname(__file__), 'snd')
# Constants
WIDTH = 500
HEIGHT = 700
FPS = 60
POWERUP_TIME = 60000
ENCO_TIME = 40000
# Define Colors
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
MAGENTA = (255, 0, 255)
BLACK = (0, 0, 0)
YELLOW = (255, 255, 0)
CYAN = (0, 255, 255)
GREENISH_BLUE = (0, 255, 180)
# Initialize pygame and create window
pg.init()
pg.mixer.init()
screen = pg.display.set_mode((WIDTH, HEIGHT))
pg.display.set_caption("Galaxy Shooters")
clock = pg.time.Clock()
score = 0
# save score
# import shelve
# d = shelve.open('score.txt') # here you will save the score variable
# d['highscore'] = score # thats all, now it is saved on disk.
# d.close()
font_name = pg.font.match_font('arial')
def draw_text(surf, text, size, x, y):
font = pg.font.Font(font_name, size)
text_surface = font.render(text, True, CYAN)
text_rect = text_surface.get_rect()
text_rect.midtop = (x, y)
surf.blit(text_surface, text_rect)
def spawnmob():
m = Mob()
all_sprites.add(m)
mobs.add(m)
def draw_shield_status_bar(surf, x, y, pct):
if pct < 0:
pct = 0
BAR_LENGTH = 100
BAR_HEIGHT = 10
fill = (pct / 100) * BAR_LENGTH
outline_rect = pg.Rect(x, y, BAR_LENGTH, BAR_HEIGHT)
fill_rect = pg.Rect(x, y, fill, BAR_HEIGHT)
pg.draw.rect(surf, GREENISH_BLUE, fill_rect)
pg.draw.rect(surf, WHITE, outline_rect, 2)
def draw_lives(surf, x, y, lives, img):
for i in range(lives):
img_rect = img.get_rect()
img_rect.x = x + 30 * i
img_rect.y = y
surf.blit(img, img_rect)
class Player(pg.sprite.Sprite):
def __init__(self):
pg.sprite.Sprite.__init__(self)
self.image = pg.transform.scale(player_img, (50, 38))
self.image.set_colorkey(BLACK)
self.rect = self.image.get_rect()
self.radius = 20
# pg.draw.circle(self.image, RED, self.rect.center, self.radius)
self.rect.centerx = WIDTH / 2
self.rect.bottom = HEIGHT - 10
self.speedx = 0
self.shield = 100
self.shoot_delay = 370
self.last_shot = pg.time.get_ticks()
self.lives = 3
self.hidden = False
self.hide_timer = pg.time.get_ticks()
self.power = 1
self.power_time = pg.time.get_ticks()
def update(self):
# Timeout for powerups
if self.power >= 2 and pg.time.get_ticks() - self.hide_timer > POWERUP_TIME:
self.power -= 1
self.power_time = pg.time.get_ticks
# Unhide if necessary
if self.hidden and pg.time.get_ticks() - self.hide_timer > 1000:
self.hidden = False
self.rect.centerx = WIDTH / 2
self.rect.bottom = HEIGHT - 10
self.speedx = 0
keystate = pg.key.get_pressed()
if keystate[pg.K_LEFT]:
self.speedx = -5
if keystate[pg.K_RIGHT]:
self.speedx = 5
if keystate[pg.K_SPACE]:
self.shoot()
self.rect.x += self.speedx
if self.rect.right > WIDTH:
self.rect.right = WIDTH
if self.rect.left < 0:
self.rect.left = 0
def powerup(self):
self.power += 1
self.power_time = pg.time.get_ticks()
def shoot(self):
now = pg.time.get_ticks()
if now - self.last_shot > self.shoot_delay:
self.last_shot = now
if self.power == 1:
laser = Laser(self.rect.centerx, self.rect.top)
all_sprites.add(laser)
lasers.add(laser)
random.choice(shoot_sounds).play()
if self.power >= 2:
laser1 = Laser(self.rect.left, self.rect.centery)
laser2 = Laser(self.rect.right, self.rect.centery)
all_sprites.add(laser1)
all_sprites.add(laser2)
lasers.add(laser1)
lasers.add(laser2)
random.choice(shoot_sounds).play()
def hide(self):
self.hidden = True
self.hide_timer = pg.time.get_ticks()
self.rect.center = (WIDTH / 2, HEIGHT + 200)
class Mob(pg.sprite.Sprite):
def __init__(self):
pg.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 = int(self.rect.width * .85 / 2)
# pg.draw.circle(self.image, RED, self.rect.center, self.radius)
self.rect.x = random.randrange(WIDTH - self.rect.width)
self.rect.y = random.randrange(-150, -40)
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 = pg.time.get_ticks()
def rotate(self):
now = pg.time.get_ticks()
if now - self.last_update > 50:
self.last_update = now
self.rot = (self.rot + self.rot_speed) % 360
new_image = pg.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.x += self.speedx
self.rect.y += self.speedy
if self.rect.top > HEIGHT + 10 or self.rect.right < -25 or self.rect.left > WIDTH + 20:
self.rect.x = random.randrange(WIDTH - self.rect.width)
self.rect.y = random.randrange(-100, -40)
self.speedy = random.randrange(1, 8)
class Laser(pg.sprite.Sprite):
def __init__(self, x, y):
pg.sprite.Sprite.__init__(self)
self.image = laser_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
# Kill it if moves off the top of the screen
if self.rect.bottom < 0:
self.kill()
class Explosion(pg.sprite.Sprite):
def __init__(self, center, size):
pg.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 = pg.time.get_ticks()
self.frame_rate = 75
def update(self):
now = pg.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 Pow(pg.sprite.Sprite):
def __init__(self, center):
pg.sprite.Sprite.__init__(self)
self.type = random.choice(['shield', 'laser', 'star'])
self.image = powerup_images[self.type]
self.image.set_colorkey(BLACK)
self.rect = self.image.get_rect()
self.rect.center = center
self.speedy = 4
def update(self):
self.rect.y += self.speedy
# Kill it if moves off the bottom of the screen
if self.rect.top > HEIGHT:
self.kill()
class Enco(pg.sprite.Sprite):
def __init__(self):
pg.sprite.Sprite.__init__(self)
self.image = draw_text(screen, (random.choice(encos), 37, WIDTH / 2, HEIGHT/ 4))
self.rect = self.image.get_rect()
self.hidden = True
self.hide_timer = pg.time.get_ticks()
def hide(self):
self.rect.y = HEIGHT - 40
self.rect.x = WIDTH / 2
def show_go_screen():
screen.blit(background2, background_rect2)
draw_text(screen, str(score), 25, WIDTH / 2, 10)
draw_text(screen, " Guardians of the Galaxy ", 37, WIDTH / 2, HEIGHT / 4)
draw_text(screen, "Arrow Keys move, Spacebar to shoot", 29, WIDTH / 2, HEIGHT / 2)
draw_text(screen, "Press any key to BEGIN", 22, WIDTH / 2, HEIGHT * 3/ 4)
pg.display.flip()
print(d)
waiting = True
while waiting:
clock.tick(FPS)
for event in pg.event.get():
if event.type == pg.QUIT:
pg.quit()
if event.type == pg.KEYUP:
waiting = False
# Load all game graphics
background = pg.image.load(path.join(img_dir, "2397814.jpg")).convert()
background_rect = background.get_rect()
background2 = pg.image.load(path.join(img_dir, "2474216.jpg")).convert()
background_rect2 = background2.get_rect()
pg.transform.scale(background, (500, 700))
player_img = pg.image.load(path.join(img_dir, "playerShip3_blue.png")).convert()
heart_img = pg.image.load(path.join(img_dir, "heart.png")).convert()
mini_heart_img = pg.transform.scale(heart_img, (23, 23))
mini_heart_img.set_colorkey(BLACK)
laser_img = pg.image.load(path.join(img_dir, "laserBlue01.png")).convert()
meteor_images = []
meteor_list = ['meteorBrown_big1.png', 'meteorBrown_big2.png', 'meteorBrown_big3.png',
'meteorBrown_med1.png','meteorBrown_small1.png','meteorBrown_small2.png',
'meteorBrown_tiny2.png',]
for img in meteor_list:
meteor_images.append(pg.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 = pg.image.load(path.join(img_dir, filename)).convert()
img.set_colorkey(BLACK)
img_lg = pg.transform.scale(img, (75, 75))
explosion_anim['lg'].append(img_lg)
img_sm = pg.transform.scale(img, (32, 32))
explosion_anim['sm'].append(img_sm)
filename = 'sonicExplosion0{}.png'.format(i)
img = pg.image.load(path.join(img_dir, filename)).convert()
img.set_colorkey(BLACK)
explosion_anim['player'].append(img)
powerup_images = {}
powerup_images['shield'] = pg.image.load(path.join(img_dir, 'shield_silver.png')).convert()
powerup_images['laser'] = pg.image.load(path.join(img_dir, 'bolt_gold.png')).convert()
powerup_images['star'] = pg.image.load(path.join(img_dir, 'star_silver.png')).convert()
encos = ("Nice Job!", "You're rocking it man!", " Great!", "What a blast!", "Good one!", "Those asteroids stand no chance against you!"
"You're the master!")
# Load all game sounds
laser_powerup = pg.mixer.Sound(path.join(snd_dir, 'Laser_Powerup.wav'))
shield_powerup = pg.mixer.Sound(path.join(snd_dir, 'Shield_Powerup.wav'))
shoot_sounds = []
for snd in ['Laser_Shoot.wav', 'Laser_Shoot2.wav']:
shoot_sounds.append(pg.mixer.Sound(path.join(snd_dir, snd)))
expl_sounds = []
for snd in ['Expl.wav', 'Expl2.wav']:
expl_sounds.append(pg.mixer.Sound(path.join(snd_dir, snd)))
player_die_sound = pg.mixer.Sound(path.join(snd_dir, 'rumble1.ogg'))
pg.mixer.music.load(path.join(snd_dir, 'tgfcoder-FrozenJam-SeamlessLoop.ogg'))
pg.mixer.music.set_volume(0.7)
pg.mixer.music.play(loops = -1)
# Game loop
game_over = True
running = True
while running:
if game_over:
show_go_screen()
game_over = False
# Sprite groups
all_sprites = pg.sprite.Group()
mobs = pg.sprite.Group()
lasers = pg.sprite.Group()
powerups = pg.sprite.Group()
player = Player()
all_sprites.add(player)
for i in range(8):
spawnmob()
score = 0
# Keep loop running at the right speed
clock.tick(FPS)
# Events
for event in pg.event.get():
# Check for closing window
if event.type == pg.QUIT:
running = False
# Update
all_sprites.update()
# Check to see if a laser hit a mob
hits = pg.sprite.groupcollide(mobs, lasers, True, True)
for hit in hits:
score += 50 - hit.radius
random.choice(expl_sounds).play()
expl = Explosion(hit.rect.center, 'lg')
all_sprites.add(expl)
if random.random() > 0.93:
pow = Pow(hit.rect.center)
all_sprites.add(pow)
powerups.add(pow)
spawnmob()
# Check to see if a mob hit the player
hits = pg.sprite.spritecollide(player, mobs, True, pg.sprite.collide_circle)
for hit in hits:
player.shield -= hit.radius * 2
expl = Explosion(hit.rect.center, 'sm')
all_sprites.add(expl)
spawnmob()
if player.shield <= 0:
player_die_sound.play()
death_expl = Explosion(player.rect.center, 'player')
all_sprites.add(death_expl)
player.hide()
player.lives -= 1
player.shield = 100
# Check to see if player hit a powerup
hits = pg.sprite.spritecollide(player, powerups, True)
for hit in hits:
if hit.type == 'shield':
pow.kill()
player.shield += random.randrange(10, 30)
shield_powerup.play()
if player.shield >= 100:
player.shield = 100
if hit.type == 'laser':
player.powerup()
laser_powerup.play()
if hit.type == 'star':
player.lives += 1
if player.lives >= 3:
player.lives = 3
# If the player died and the explosion finished playing
if player.lives == 0 and not death_expl.alive():
game_over = True
# Render
screen.fill(BLACK)
screen.blit(background, background_rect)
all_sprites.draw(screen)
draw_text(screen, str(score), 25, WIDTH / 2, 10)
draw_shield_status_bar(screen, 5, 5, player.shield)
draw_lives(screen, WIDTH - 100, 5, player.lives, mini_heart_img)
if score == 500:
draw_text(screen, "Nice Job!", 37, WIDTH / 2, HEIGHT / 2)
now = pg.time.get_ticks()
last.enco = pg.time.get_ticks()
if now - last.enco == ENCO_TIME:
hide_enco
# *After* drawing everything, flip the display
pg.display.flip()
pg.quit()
thanks!
Create a variable called highscore then then when the player looses, check if the current score is higher than highscore and if it is, then assign the score to highscore. Then you can just easily add a label with the highscore variable as a text
score = 0
highscore = 0
# save score
# import shelve
# d = shelve.open('score.txt') # here you will save the score variable
# d['highscore'] = score # thats all, now it is saved on disk.
# d.close()
# A lot of your code
def show_go_screen():
screen.blit(background2, background_rect2)
draw_text(screen, str(highscore), 50, WIDTH / 2, 10) #change the x value accordingly
# more of your code
while running:
if game_over:
show_go_screen()
game_over = False
# Sprite groups
all_sprites = pg.sprite.Group()
mobs = pg.sprite.Group()
lasers = pg.sprite.Group()
powerups = pg.sprite.Group()
player = Player()
all_sprites.add(player)
for i in range(8):
spawnmob()
if score > highscore:
highscore = score
score = 0
# the rest of your code

How to add a background image in python-pygame

I'm making a game, brickbreaker.
I'm tring to add an image on the background, but it doesn't show up.
I tried some other ways:
Class background(pygame.sprite.Sprite) - I can see image, but it hides game
import pygame
from pygame.locals import *
import sys
class Brickbreaker:
def __init__(self):
self.screen = pygame.display.set_mode((600, 600))
self.image = pygame.image.load("bg.png")
self.blocks = []
self.paddle = [[pygame.Rect(300, 500, 20, 10), 120],
[pygame.Rect(320, 500, 20, 10),100],
[pygame.Rect(340, 500, 20, 10),80],
[pygame.Rect(360, 500, 20, 10),45],
]
self.ball = pygame.Rect(300, 490, 12, 12)
self.direction = -1
self.yDirection = -1
self.angle = 80
self.speeds = {
120:(-10, -3),
100:(-10, -8),
80:(10, -8),
45:(10, -3),
}
self.swap = {
120:45,
45:120,
100:80,
80:100,
}
pygame.font.init()
self.font = pygame.font.SysFont("Italic", 40)
self.score = 0
def createBlocks(self):
self.blocks = []
y = 50
for __ in range(90 // 10):
x = 40
for _ in range(400 // 25 - 6):
block = pygame.Rect(x, y, 50, 20)
self.blocks.append(block)
x += 52
y += 22
def ballUpdate(self):
for _ in range(2):
speed = self.speeds[self.angle]
xMovement = True
if _:
self.ball.x += speed[0] * self.direction
else:
self.ball.y += speed[1] * self.direction * self.yDirection
xMovement = False
if self.ball.x <= 0 or self.ball.x >= 600:
self.angle = self.swap[self.angle]
if self.ball.x <= 0:
self.ball.x = 1
else:
self.ball.x = 599
if self.ball.y <= 0:
self.ball.y = 1
self.yDirection *= -1
for paddle in self.paddle:
if paddle[0].colliderect(self.ball):
self.angle = paddle[1]
self.direction = -1
self.yDirection = -1
break
check = self.ball.collidelist(self.blocks)
if check != -1:
block = self.blocks.pop(check)
if xMovement:
self.direction *= -1
self.yDirection *= -1
self.score += 1
if self.ball.y > 600:
self.createBlocks()
self.score = 0
self.ball.x = self.paddle[1][0].x
self.ball.y = 490
self.yDirection = self.direction = -1
def paddleUpdate(self):
pos = pygame.mouse.get_pos()
on = -1
for p in self.paddle:
p[0].x = pos[0] + 20 * on
on += 1
def main(self):
pygame.mouse.set_visible(False)
clock = pygame.time.Clock()
self.createBlocks()
while True:
clock.tick(60)
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
self.screen.fill([255, 255, 255])
self.screen.blit("bg.png")
self.paddleUpdate()
self.ballUpdate()
for block in self.blocks:
pygame.draw.rect(self.screen, (80,80,80), block)
for paddle in self.paddle:
pygame.draw.rect(self.screen, (255,255,255), paddle[0])
pygame.draw.rect(self.screen, (120,200,255), self.ball)
self.screen.blit(self.font.render(str(self.score), -1, (200,200,255)), (40, 540)) #스코어 색
pygame.display.update()
if __name__ == "__main__":
Brickbreaker().main()
I got your code to have a background image by changing
self.screen.blit("bg.png")
to
self.screen.blit(self.image, (0,0))
blit() takes two arugments the image and the position of it.

Collision detection not working in pygame

I am making a game at the moment and I am experiencing problems with collision detection. I am making an end of level block but it can not detect if the player is standing on it to change to level 2. The collision detection for the block is found in player.updater(). As well as this the block is a class and in a group called endPlatform to allow the collision detection to work. The game runs perfectly fine however it can not detect when the Player hits Endplatform. I get no errors which show up.
EndPlatform:
class EndPlatform(pygame.sprite.Sprite):
def __init__(self, display):
super().__init__()
self.image = pygame.image.load("endPlatform.png")
self.rect = self.image.get_rect()
display.blit(self.image, self.rect)
Player:
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
# Is it touching the floor?
self.standing = True
# Rendering image and creating some variables
self.image = pygame.image.load("Slime.png")
self.sprite_x_change = 0
self.sprite_y_change = 0
self.rect = self.image.get_rect()
self.rect.y = 460
self.rect.x = 120
# Mobility: Left, right, up and stop
def move_right(self):
self.sprite_x_change = 8
def move_left(self):
self.sprite_x_change = -8
def move_up(self, platform):
if self.standing == True:
self.sprite_y_change = -25
self.standing = False
def stop(self):
self.sprite_x_change = 0
def sprint(self):
self.sprite_x_change += 10
def updater(self, platforms, powerups, score, endPlatform):
self.gravity()
self.rect.x += self.sprite_x_change
self.standing = False
platforms_hit = pygame.sprite.spritecollide(self, platforms, False)
for blocks in platforms_hit:
if self.sprite_x_change > 0:
self.rect.right = blocks.rect.left
elif self.sprite_x_change < 0:
self.rect.left = blocks.rect.right
self.rect.y += self.sprite_y_change
platforms_hit = pygame.sprite.spritecollide(self, platforms, False)
for blocks in platforms_hit:
# Going down
if self.sprite_y_change > 0:
self.rect.bottom = blocks.rect.top - 1
self.standing = True
# Going up
elif self.sprite_y_change < 0:
self.rect.top = blocks.rect.bottom
self.standing = False
self.sprite_y_change = 0
coins_hit = pygame.sprite.spritecollide(self, powerups, True)
if len(coins_hit) > 0:
score.add()
endLevel = pygame.sprite.spritecollide(self, endPlatform, True)
if len(endLevel) > 0:
score.add()
All the code:
import pygame
import random
pygame.font.init()
# Colours + Global constants
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
BLACK = (0, 0, 0)
RANDOM = (12, 211, 123)
WIDTH = 800
HEIGHT = 600
SIZE = (WIDTH, HEIGHT)
AGENT = pygame.font.SysFont("Agent Orange", 30)
# CLASSES
# Block is the common platform
class EndPlatform(pygame.sprite.Sprite):
def __init__(self, display):
super().__init__()
self.image = pygame.image.load("endPlatform.png")
self.rect = self.image.get_rect()
display.blit(self.image, self.rect)
class Coins(pygame.sprite.Sprite):
def __init__(self, display):
super().__init__()
self.image = pygame.image.load("hud_coins.png")
self.rect = self.image.get_rect()
display.blit(self.image, self.rect)
class Score:
def __init__(self):
self.score = 0
def msgs(self, msg, colour, display):
screen_text = AGENT.render(msg, True, colour)
display.blit(screen_text, [0, 0])
def add(self):
self.score += 1
class Monster(pygame.sprite.Sprite):
def __init__(self, length, height, colour):
super().__init__()
self.image = pygame.Surface([length, height])
self.image.fill(colour)
self.rect = self.image.get_rect()
# Setting Y coordinates
self.rect.y = HEIGHT - 80
def jump(self):
self.rect.y = -10
class Block(pygame.sprite.Sprite):
def __init__(self, length, height, colour):
super().__init__()
# Making image
self.image = pygame.Surface([length, height])
self.image.fill(colour)
self.rect = self.image.get_rect()
# Setting Y coordinates
self.rect.y = 468
class Platform(pygame.sprite.Sprite):
def __init__(self, display, x_screen, y_screen, x_sheet, y_sheet, height, length):
super().__init__()
self.tiles = pygame.image.load("tiles_spritesheet.png")
self.image = self.tiles.subsurface(pygame.Rect(x_sheet, y_sheet, height, length))
self.rect = self.image.get_rect(x=x_screen, y=y_screen)
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
# Is it touching the floor?
self.standing = True
# Rendering image and creating some variables
self.image = pygame.image.load("Slime.png")
self.sprite_x_change = 0
self.sprite_y_change = 0
self.rect = self.image.get_rect()
self.rect.y = 460
self.rect.x = 120
# Mobility: Left, right, up and stop
def move_right(self):
self.sprite_x_change = 8
def move_left(self):
self.sprite_x_change = -8
def move_up(self, platform):
if self.standing == True:
self.sprite_y_change = -25
self.standing = False
def stop(self):
self.sprite_x_change = 0
def sprint(self):
self.sprite_x_change += 10
def updater(self, platforms, powerups, score, endPlatform):
self.gravity()
self.rect.x += self.sprite_x_change
self.standing = False
platforms_hit = pygame.sprite.spritecollide(self, platforms, False)
for blocks in platforms_hit:
if self.sprite_x_change > 0:
self.rect.right = blocks.rect.left
elif self.sprite_x_change < 0:
self.rect.left = blocks.rect.right
self.rect.y += self.sprite_y_change
platforms_hit = pygame.sprite.spritecollide(self, platforms, False)
for blocks in platforms_hit:
# Going down
if self.sprite_y_change > 0:
self.rect.bottom = blocks.rect.top - 1
self.standing = True
# Going up
elif self.sprite_y_change < 0:
self.rect.top = blocks.rect.bottom
self.standing = False
self.sprite_y_change = 0
coins_hit = pygame.sprite.spritecollide(self, powerups, True)
if len(coins_hit) > 0:
score.add()
endLevel = pygame.sprite.spritecollide(self, endPlatform, True)
if len(endLevel) > 0:
score.add()
def gravity(self):
self.sprite_y_change += 3
class Level:
def __init__(self):
# Creating groups
self.endPlatform = pygame.sprite.Group()
self.powerups = pygame.sprite.Group()
self.sprites = pygame.sprite.Group()
self.all_things = pygame.sprite.Group()
self.platforms = pygame.sprite.Group()
self.entities = pygame.sprite.Group()
self.shift_x = 0
self.shift_y = 0
def updater(self, display, score):
self.all_things.draw(display)
score.msgs("Score: " + str(score.score), RED, display)
def scroll_x(self, shift_x_change):
self.shift_x += shift_x_change
for platform in self.entities:
platform.rect.x += shift_x_change
def scroll_y(self, shift_y_change):
self.shift_y += shift_y_change
for platform in self.entities:
platform.rect.y += shift_y_change
class Level01(Level):
def __init__(self, player1, monster, display):
# Initialise level1
super().__init__()
# Level01 things
block = Block(245, 3, BLACK)
Level.all_things = self.all_things
self.sprites.add(player1, monster)
self.platforms.add(block)
self.all_things.add(player1, block, monster)
self.entities.add(block)
theLevel = []
level = [[600, 400, 648, 0, 70, 70],
[740, 320, 648, 0, 70, 70],
[380, 400, 648, 0, 70, 70],
[900, 280, 648, 0, 70, 70],
[1200, 530, 648, 0, 70, 70],
[1350, 450, 648, 0, 70, 70],
[1500, 550, 648, 0, 70, 70],
[1680, 500, 648, 0, 70, 70],
]
for platform in theLevel:
block = Block(platform[0], platform[1], RED)
block.rect.x = platform[2]
block.rect.y = platform[3]
self.platforms.add(block)
self.all_things.add(block)
self.entities.add(block)
for goodPlatform in level:
platform = Platform(display, goodPlatform[0], goodPlatform[1], goodPlatform[2], goodPlatform[3], goodPlatform[4], goodPlatform[5])
self.platforms.add(platform)
self.all_things.add(platform)
self.entities.add(platform)
for n in range(1):
coin = Coins(display)
coin.rect.x = random.randint(0, WIDTH*3)
coin.rect.y = 400
self.all_things.add(coin)
self.entities.add(coin)
self.powerups.add(coin)
platforms_hit = pygame.sprite.spritecollide(coin, self.entities, False)
for hit in platforms_hit:
coin.rect.x = random.randrange(0, WIDTH*3)
finalPlatform = EndPlatform(display)
finalPlatform.rect.x = 1900
finalPlatform.rect.y = 420
self.all_things.add(finalPlatform)
self.entities.add(finalPlatform)
self.platforms.add(finalPlatform)
self.endPlatform.add(finalPlatform)
class Level02(Level):
def __init__(self, player1, monster):
super().__init__()
# Level01 things
block = Block(245, 3, BLACK)
Level.all_things = self.all_things
self.sprites.add(player1, monster)
self.platforms.add(block)
self.all_things.add(player1, block, monster)
def main():
# Init pygame
pygame.init()
# Set screen
backgrounds = ["background2.jpg", "background.jpg"]
background = pygame.image.load(backgrounds[0])
backgroundRect = background.get_rect()
display = pygame.display.set_mode(background.get_size())
# Creating FPS thingy
clock = pygame.time.Clock()
# Making levels + Player
score = Score()
monster = Monster(30, 30, RANDOM)
player = Player()
level_1 = Level01(player, monster, display)
level_2 = Level02(player, monster)
# Choosing level
levelList = []
levelList.append(level_1)
levelList.append(level_2)
currentLevelNumber = 0
# Game loop
loop = True
while loop == True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
player.move_right()
if event.key == pygame.K_LEFT:
player.move_left()
if event.key == pygame.K_UP:
player.move_up(currentLevel.platforms)
if event.key == pygame.KMOD_LSHIFT and event.key == pygame.K_RIGHT:
player.sprint()
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT and player.sprite_x_change < 0:
player.stop()
if event.key == pygame.K_RIGHT and player.sprite_x_change > 0:
player.stop()
if event.key == pygame.KMOD_LSHIFT:
player.sprite_x_change -= 10
# Update things
#monster.jump()
if player.rect.x > 400:
player.rect.x = 400
currentLevel.scroll_x(-10)
if player.rect.x >= WIDTH:
player.rect.x = WIDTH
currentLevel.scroll(0)
if player.rect.y >= HEIGHT:
main()
if player.sprite_x_change < 0 and player.rect.x >= 120:
currentLevel.scroll_x(0)
if player.rect.left <= 120 and player.sprite_x_change < 0:
player.rect.x = 120
player.rect.left = 120
currentLevel.scroll_x(10)
'''
if player.rect.y <= 300:
if player.standing == False and player.sprite_y_change < 0:
currentLevel.scroll_y(10)
if currentLevel.shift_y > 0:
y_speed = -4
if player.standing == True and player.rect.y < 300:
y_speed = 4
print(currentLevel.shift_y)
currentLevel.scroll_y(y_speed)
'''
currentLevel = levelList[currentLevelNumber]
if currentLevel.shift_x > 0:
currentLevel.scroll_x(currentLevel.shift_x * -1)
display.blit(background, backgroundRect)
player.updater(currentLevel.platforms, currentLevel.powerups, score, currentLevel.endPlatform)
currentLevel.updater(display, score)
# Refresh screen
clock.tick(30)
pygame.display.update()
pygame.quit()
loop = False
if __name__ == "__main__":
main()
It seems I found the problem, for some daft reason you can't use collision detection twice on the same object. I used it once so that the player could stand on the block and another time so that you could go on to the next level!
The reason why it doesn't switch to the next level is that you change the position of the rect when it collides with a platform, so the player.rect gets moved out of the blocks.rect and therefore can't collide again when you call spritecollide with the endPlatform group.
A quick and dirty fix would be to check in the for blocks in platforms_hit: loops if the block is in the endPlatform group and then return True:
for blocks in platforms_hit:
if blocks in endPlatform:
score.add()
return True
And then increase the currentLevelNumber in the main function if True is returned:
change_level = player.updater(currentLevel.platforms, currentLevel.powerups, score, currentLevel.endPlatform)
if change_level:
currentLevelNumber += 1

Categories