ive been working on this script as a project, and when i run it and click on the button to sleep(its going to be an rpg game) it loops and sends the command a bunch of times through the function.

import sys, random, time, pygame
from pygame.locals import *

def print_text(font, x, y, text, color=(255,255,255)):
    imgText = font.render(text, True, color)
    screen.blit(imgText, (x,y))

    
#main program begins
pygame.init()
screen = pygame.display.set_mode((600,500))
pygame.display.set_caption("Critter")
white = 255,255,255
red = 220, 50, 50
yellow = 230,230,50
black = 0,0,0
white = 255,255,255
blue = 0,191,255
red = 255,0,0
green = 50,205,50
background = pygame.image.load("background.jpg")
sleepbtn = pygame.image.load('sleep.png')

screen.fill(blue)
font1 = pygame.font.Font(None, 24)

pos_x = 1
pos_y = 1
mouse_down_x = None
mouse_down_y = None
text1 = False

#Health:
sleep = [100,165]

delay = [50,0]

print_text(font1, 5, 425, "Sleep")

def sleeping():
    temp = sleep[1]
    temp += random.randrange(1, 50, 1)
    print temp
    if temp >= 165:
        sleep[1] = 165
        temp = 0
    else:
        sleep[1] = temp
    sleep[0] = sleep[1]-65


while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()            
    keys = pygame.key.get_pressed()
    if keys[K_ESCAPE]:
        pygame.quit()
        sys.exit()
    elif event.type == MOUSEMOTION:
            pos_x,pos_y = event.pos
    elif event.type == MOUSEBUTTONDOWN:
        mouse_down = event.button
        mouse_down_x,mouse_down_y = event.pos
    elif event.type == MOUSEBUTTONUP:
        sleeping()
        mouse_up = event.button
        mouse_up_x,mouse_up_y = event.pos


            
    if pos_x == 0 and pos_y == 0:
        text1 = True





    #draw background
    screen.blit(background, (0,0))
    pygame.draw.line(screen,white,(410,0),(410,410),10) #Right wall
    pygame.draw.line(screen,white,(0,410),(415,410),10) #left wall
    if text1 == True:
        print_text(font1, 0, 0, "Mouse Events")
    sleepbtn = pygame.transform.scale(sleepbtn, (100, 45))
    screen.blit(sleepbtn, (420,20))
    
    pygame.draw.line(screen,white,(60,430),(170,430),20) #sleep bar bckgrnd
    pygame.draw.line(screen,red,(65,430),(165,430),14) #sleep bar bckgrnd red
    pygame.draw.line(screen,green,(65,430),(sleep[1],430),14) #sleep bar bckgrnd red
    
    pygame.display.update()
    temp = random.randrange(0,50,1)
    
    if sleep[1] >=65 and temp == 5:
        sleep[1] -= 1

You need to change your while loop you have no loop control variable so your while loop is just looping thru you need a variable for that; Example. While(event.type == MOUSEBUTTONUP) or something similar . But as you can see from your code there is nothing to end your while loop.And also in python, python doesn't operate on semicolons so it looks like this code:


#draw background
screen.blit(background, (0,0))
pygame.draw.line(screen,white,(410,0),(410,410),10) #Right wall
pygame.draw.line(screen,white,(0,410),(415,410),10) #left wall
if text1 == True:
print_text(font1, 0, 0, "Mouse Events")
sleepbtn = pygame.transform.scale(sleepbtn, (100, 45))
screen.blit(sleepbtn, (420,20))

pygame.draw.line(screen,white,(60,430),(170,430),20) #sleep bar bckgrnd
pygame.draw.line(screen,red,(65,430),(165,430),14) #sleep bar bckgrnd red
pygame.draw.line(screen,green,(65,430),(sleep[1],430),14) #sleep bar bckgrnd red

pygame.display.update()
temp = random.randrange(0,50,1)

if sleep[1] >=65 and temp == 5:
sleep[1] -= 1

is also in the while loop I dont know if that was an accident or not.

either way if Im wrong your error is in the while loop I believe.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.