My pygame won't run in replit

When I try to run my replit program it gives “Working” and doesn’t go from there (no error or anything else).

I won’t provide repl link because it contains personal images, but here’s the code:

PS. Not the best code to ever be written, still a draft and im not that good. Any help appreciated

import pygame, sys, random, math, time
from pygame.locals import QUIT

pygame.init()
surf = pygame.display.set_mode((600, 600))
pygame.display.set_caption("Game")


def loadImage(path, scale_factor):
  image = (
    pygame.transform.scale_by(pygame.image.load(path), scale_factor)
    .convert_alpha()
    .copy()
  )
  size = (image.get_width(), image.get_height())
  return image, size


player_img, player_size = loadImage("santa_sleigh.png", 0.2)
enemy_img, enemy_size = loadImage("enemy.png", 0.2)
enemy_img_dead, enemy_img_dead_size = loadImage("enemy_dead.png", 0.2)
bullet_img, bullet_size = loadImage("presentBullet.png", 0.05)
game_over_img, game_over_img_size = loadImage("game_over.png", 1)


player = pygame.Rect(0, 0, player_size[0], player_size[1])
clock = pygame.time.Clock()

pos = [300, 300]
velocity = 4
bulletVel = 2

camera_offset = [0, 0]

bulletDirections = {"N": 7, "NE": 8, "E": 7, "SE": 8, "S": 7, "SW": 8, "W": 7, "NW": 8}


def enemyGen(enemies):
  gen = random.randint(0, 300)
  if gen > 296:
    if gen > 298:
      enemies.append(
        pygame.Rect(
          random.randint(0, 600) - camera_offset[0],
          camera_offset[1] + enemy_size[1],
          enemy_size[0],
          enemy_size[1],
        )
      )
    else:
      enemies.append(
        pygame.Rect(
          random.randint(0, 600) - camera_offset[0],
          610 - camera_offset[1],
          enemy_size[0],
          enemy_size[1],
        )
      )
  return enemies


def render(
  pos, flippedY, rotateX, enemies, camera_offset, bullets, playerHealth, deadEnemies
):
  player.update(pos[0], pos[1], player_size[0], player_size[1])
  surf.fill((150, 75, 0))
  player_img_render = pygame.transform.rotate(
    pygame.transform.flip(player_img, False, flippedY), (rotateX * -90)
  )
  surf.blit(
    player_img_render, ((player.x + camera_offset[0]), (player.y + camera_offset[1]))
  )
  for enemy in enemies:
    if player.colliderect(enemy):
      enemies.remove(enemy)
      playerHealth -= 1
    else:
      surf.blit(enemy_img, ((enemy.x + camera_offset[0]), (enemy.y + camera_offset[1])))
  for deadEnemy in deadEnemies:
    deadEnemy[1] += 1
    if deadEnemy[1] < 20:
      surf.blit(
        enemy_img_dead,
        (deadEnemy[0][0] + camera_offset[0], deadEnemy[0][1] + camera_offset[1]),
      )
  if bullets != []:
    for bullet in bullets:
      for enemy in enemies:
        if bullet[0].colliderect(enemy):
          try:
            bullets.remove(bullet)
          except:
            pass
          deadEnemies.append([(enemy.x, enemy.y), 0])
          enemies.remove(enemy)
      surf.blit(
        bullet_img, (bullet[0].x + camera_offset[0], bullet[0].y + camera_offset[1])
      )
  return playerHealth, deadEnemies


def move(pos, rotateX, flippedY, camera_offset, direction):
  keys = pygame.key.get_pressed()
  if keys[pygame.K_w]:
    pos[1] -= velocity
    flippedY = False
    rotateX = 0
    direction = "N"
  if keys[pygame.K_s]:
    pos[1] += velocity
    flippedY = True
    rotateX = 0
    direction = "S"
  if keys[pygame.K_a]:
    pos[0] -= velocity
    direction = "W"
    if keys[pygame.K_w]:
      rotateX = -0.5
      flippedY = False
      direction = "NW"
    elif keys[pygame.K_s]:
      rotateX = 0.5
      flippedY = True
      direction = "SW"
    else:
      rotateX = -1
      flippedY = False
  if keys[pygame.K_d]:
    direction = "E"
    pos[0] += velocity
    if keys[pygame.K_w]:
      rotateX = 0.5
      flippedY = False
      direction = "NE"
    elif keys[pygame.K_s]:
      rotateX = -0.5
      flippedY = True
      direction = "SE"
    else:
      rotateX = 1
      flippedY = False
  return pos, flippedY, rotateX, camera_offset, direction


def shoot(bullets, direction, bulletCD):
  bulletCD = max(bulletCD - 1, 0)
  if bullets != []:
    for bullet in bullets:
      if bullet[1] == "N":
        bullet[0].y -= bulletDirections[bullet[1]] * bulletVel
      elif bullet[1] == "NE":
        bullet[0].x += bulletDirections[bullet[1]] * -math.cos(60) * bulletVel
        bullet[0].y -= bulletDirections[bullet[1]] * -math.sin(30) * bulletVel
      elif bullet[1] == "E":
        bullet[0].x += bulletDirections[bullet[1]] * bulletVel
      elif bullet[1] == "SE":
        bullet[0].x += bulletDirections[bullet[1]] * -math.cos(60) * bulletVel
        bullet[0].y += bulletDirections[bullet[1]] * -math.sin(30) * bulletVel
      elif bullet[1] == "S":
        bullet[0].y += bulletDirections[bullet[1]] * bulletVel
      elif bullet[1] == "SW":
        bullet[0].x -= bulletDirections[bullet[1]] * -math.cos(60) * bulletVel
        bullet[0].y += bulletDirections[bullet[1]] * -math.sin(30) * bulletVel
      elif bullet[1] == "W":
        bullet[0].x -= bulletDirections[bullet[1]] * bulletVel
      elif bullet[1] == "NW":
        bullet[0].x -= bulletDirections[bullet[1]] * -math.cos(60) * bulletVel
        bullet[0].y -= bulletDirections[bullet[1]] * -math.sin(30) * bulletVel
  keys = pygame.key.get_pressed()
  if keys[pygame.K_SPACE] and bulletCD == 0:
    bulletDir = direction
    bullets.append(
      [
        pygame.Rect(player.centerx, player.centery, bullet_size[0], bullet_size[1]),
        bulletDir,
      ]
    )
    bulletCD = 30
  return bullets, bulletCD


def enemyMove(enemies, gameTime):
  for enemy in enemies:
    diffX = player.centerx - enemy.centerx
    diffY = player.centery - enemy.centery
    if diffX > 0:
      enemy.x += max(1, min(gameTime * 0.02, 3))
    if diffY > 0:
      enemy.y += max(1, min(gameTime * 0.02, 3))
    if diffX < 0:
      enemy.x -= max(1, min(gameTime * 0.02, 3))
    if diffY < 0:
      enemy.y -= max(1, min(gameTime * 0.02, 3))


def gameOver():
  surf.blit(game_over_img, (0, 0))
  time.sleep(3)
  sys.exit()


rotateX = 0
flippedY = False
enemies = []
deadEnemies = []
gameTime = 0
bullets = []
direction = "N"
bulletCD = 0
playerHealth = 1
while True:
  gameTime += 0.1
  for event in pygame.event.get():
    if event.type == QUIT:
      pygame.quit()
      sys.exit()
  camera_offset[0] = 300 - player.centerx
  camera_offset[1] = 300 - player.centery
  pos, flippedY, rotateX, camera_offset, direction = move(
    pos, rotateX, flippedY, camera_offset, direction
  )
  bullets, bulletCD = shoot(bullets, direction, bulletCD)
  enemies = enemyGen(enemies)
  enemyMove(enemies, gameTime)
  playerHealth, deadEnemies = render(
    pos, flippedY, rotateX, enemies, camera_offset, bullets, playerHealth, deadEnemies
  )
  pygame.display.update()
  if playerHealth <= 0:
    gameOver()

  clock.tick(60)

Hi @BM380 , welcome to the forums!
Try entering kill 1 in the Shell and see if that works.
Hope this helps!

2 Likes

replace sys.exit with pygame.quit

3 Likes

@UMARismyname
Wait what, how would that cause the replit to not load?

1 Like

the issue is the lack of pygame.quit inside the def gameOver before the program exits. So pygame doesn’t clean up and this seems to confuse prybar somehow.

2 Likes

From what I understand though, his program won’t run in the first place. I get how that could cause an issue on a idle, but I don’t believe it would interfere with the startup.

the problem is that you can only play the game (fully, until the “game over”) once after the repl boots. After that, the repl fails to re-run because the pygame instance didn’t get to clean up.

1 Like

Huh, the way they phrase it ‘…gives “Working” but doesn’t go from there (no error or anything else)’, makes me think that there’s no output at all to begin with (remains a blank screen and such). I get how it could cause an issue later on in the code, but it wouldn’t be run until the game is actually quit.

Thanks this worked. Also I will note that “kill 1” in the shell also works once ive messed up pygame but this is the fix

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.