Im doing this game for school and it's supposed to be finished real soon and then of course al the problems start coming up, the game now crashes, sometimes after a few seconds and sometimes directly on start. Don't really know why, a thought is that its doing to much at the same time, or doing something it doesent need to do. The code is verry badly done, should use classes and functions but I have really goten in to that since im really new to this (first game im doing on my own). So please, if anyone could help I would appreciate it, give me tips on what to improve and if someone could fix my problem that would be great!
The pics will be attached to this post
import time
import pygame
from pygame.locals import*
pygame.init()
screenWidth = 1018
screenHeight = 720
screen = pygame.display.set_mode((screenWidth,screenHeight))
facingLeft = False
#Player
Player = pygame.image.load('GoodGuyFramPng.png')
PlayerRect = Player.get_rect()
#Boss
BossIm = pygame.image.load('Boss.png')
Boss = BossIm.get_rect()
#Shot for use later
#shotIm = pygame.image.load('Bullet.png')
#shot = shotIm.get_rect()
counter = 1
secondsPerFrame = 1.0/30
done = False
#Player var
x = 0
y = 0
vx = 0
vy = 0
g = 2
onGround = False
jumping = False
moving = False
#Boss var
bx = 560
by = 0
goingLeft = True
goingRight = False
bvx = 0
bvy = 0
radius = 10
BonGround = False
Bjumping = False
while not done:
delayTime = time.clock()
#Comands
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
#Shoot
if event.type == KEYDOWN and event.key == K_LCTRL:
print('shoot')
#jump
if event.type == KEYDOWN and event.key == K_SPACE and onGround:
vy = -20
onGround = False
jumping = True
if event.type == KEYUP and event.key == K_SPACE and not onGround and vy < 0:
vy = 0
jumping = False
pressedKeys = pygame.key.get_pressed()
#walk/run
if pressedKeys[K_LEFT]:
PlayerRect.x -= (4 + pressedKeys[K_LSHIFT] * 8)
facingLeft = True
moving = True
Player = pygame.image.load('GubbeVanster.png')
if PlayerRect.left < 0:
PlayerRect.x += (4 + pressedKeys[K_LSHIFT] * 8)
if pressedKeys[K_RIGHT]:
PlayerRect.x += (4 + pressedKeys[K_LSHIFT] * 8)
facingLeft = False
moving = True
Player = pygame.image.load('GubbeSidan.png')
if PlayerRect.right > screenWidth:
PlayerRect.right -= (4 + pressedKeys[K_LSHIFT] * 8)
if event.type == KEYUP:
if event.key == K_RIGHT or event.key == K_LEFT:
moving = False
if pressedKeys[K_ESCAPE]:
done = True
if not moving:
Player = pygame.image.load('GoodGuyFramPng.png')
#Physics
PlayerRect.y += vy
PlayerRect.x += vx
vy = min(vy + g, 20)
onGround = False
#Check colition with floor
if vy > 0 and PlayerRect.bottom >= screenHeight:
if PlayerRect.bottom >= screenHeight and PlayerRect.bottom < screenHeight + vy:
PlayerRect.bottom = screenHeight
vy = 0
onGround = True
#--------Bossen------------------------------------------------------------
#Walk
if goingLeft:
Boss.x -= 4
if Boss.left <= 0:
goingLeft = False
goingRight = True
if goingRight:
Boss.x += 4
if Boss.right >= screenWidth:
goingRight = False
goingLeft = True
#Physics
Boss.y += bvy
Boss.x += bvx
bvy = min(bvy + g, 20)
BonGround = False
#check colition with floor
if bvy > 0 and Boss.bottom >= screenHeight:
if Boss.bottom >= screenHeight and Boss.bottom < screenHeight + bvy:
Boss.bottom = screenHeight
bvy = 0
BonGround = True
#-----------------------------------------------------------------------
jumping = jumping and vy < 0
Bjumping = Bjumping and bvy < 0
#draw stuff
#Backgrund drawn once
for i in range (1):
background = pygame.image.load('bakgrund.jpg')
screen.blit(background, (0,0))
#Player
screen.blit(BossIm,Boss)
screen.blit(Player,PlayerRect)
pygame.display.flip()
counter += 1
time.sleep(secondsPerFrame - (time.clock() - delayTime))
pygame.quit()