jeudi 9 novembre 2023

Why does my program freeze at seemingly random moments?

I'm trying to make a small game where you move a circle and you have to avoid being hit by the enemy. I'm very new to processing and python so its a big mess but I'm at a loss. Basically, the program freezes at seemingly inconsistent intervals just before the enemy randomises its starting position. Try the program and you'll see what I mean. I couldn't find any consistencies myself for when it happens, but I just know that it is something to do with with the randomisation. I'd really appreciate any help if possible.

import random
play = False
score = 0
highscore = 0
exStartpos = -25
eyStartpos = random.randint(0, 500)
xVel = random.randint(1, 10)
yVel = random.randint(-10, 10)
difficulty = 50
sidetop = 0
leftright = 0
topdown = 0

def setup():
    size(500, 500)

def draw():
    global play, score, highscore, exStartpos, eyStartpos, xVel, yVel, difficulty, reset, sidetop, leftright, topdown
    if keyPressed == True:                                                           #Makes the game begin
        play = True
        score = 0
    background(255)
    if play == False:                                                                #If play == False I show the title screen and scores
        fill(0)
        textSize(100)
        text("Damn,", 100, 100)
        textSize(30)
        text("just missed him!", 130, 150)
        text("Score: " + str(score), 170, 300)
        text("Highscore: " + str(highscore), 150, 330)
        text("Press any key to play", 110, 400)
    
    if play == True:                                                                #If play == True the game begins here
        fill(255)
        circle(mouseX, mouseY, difficulty)
        circle(exStartpos, eyStartpos, 50)
        exStartpos = exStartpos + xVel
        eyStartpos = eyStartpos + yVel
        
        if dist(mouseX, mouseY, exStartpos, eyStartpos) < (difficulty/2)+25:                    #This checks for collision and ends the game if the circles collide
            play = False
            if highscore < score:
                highscore = score
            difficulty = 50
        
        while (exStartpos < -25) or (exStartpos > 525) or (eyStartpos < -25) or (eyStartpos > 525):     #This runs when the enemy is offscreen and the entire section is to randomise which side it reappears from and how fast it  moves
            score = score + 10
            difficulty = difficulty + 1
            sidetop = random.randint(1, 2)                                   #The best I could come up with was to first randomise between the sides or the top and bottom,
            if sidetop == 1:
                leftright = random.randint(1, 2)                             #then depending on what was chosen I randomised between the two remaining options (left and right in this case)
                print("leftright:" + str(leftright))# This print is left over from when I tried to resolve the issue myself
                if leftright == 1:
                    exStartpos = -25
                    xVel = random.randint(1, 10)
                    yVel = random.randint(-10, 10)
                elif leftright == 2:
                    exStartpos = 525
                    xVel = random.randint(-1, -10)
                    yVel = random.randint(-10, 10)
                eyStartpos = random.randint(0, 500)
            elif sidetop == 2:
                topdown = random.randint(1, 2)
                if topdown == 1:
                    eyStartpos = -25
                    xVel = random.randint(-10, 10)
                    yVel = random.randint(1, 10)
                elif topdown == 2:
                    eyStartpos = 525
                    xVel = random.randint(-10, 10)
                    yVel = random.randint(-10, -1)
                exStartpos = random.randint(0, 500)

I tried to identify if there was anything wrong with how i've randomised sidetop, leftright or topdown, but I could'nt find anything that hinted for what I should do.




Aucun commentaire:

Enregistrer un commentaire