can someone help me make my code more simple.
i uploaded the source to my website in a zip file with all the images because i used pygame and if you want to test it you will need the images.
you can download everything here http://2nh.co/Tk or here is the code.

import sys, random, time, pygame, os, threading
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
color = 0
sprite=None
#Customize:

screen.fill(white)

mainmenu = pygame.image.load("customizecolor.png")
mainmenu = pygame.transform.scale(mainmenu, (600, 500))
screen.blit(mainmenu, (0,0))


pygame.display.update()

while color == 0:


    for event in pygame.event.get():
        #Keydown
        if event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                pygame.quit()
                sys.exit()
            if event.key == K_3:
                color = 3
            if event.key == K_1:
                color = 1
            if event.key == K_4:
                color = 4
            if event.key == K_2:
                color = 2 
   
if color == 1:
    sprite = pygame.image.load('black.png')
elif color == 2:
    sprite = pygame.image.load('pink.png')
elif color == 3:
    sprite = pygame.image.load('red.png')
elif color == 4:
    sprite = pygame.image.load('blue.png')
sprite = pygame.transform.scale(sprite, (105, 150))

background = pygame.image.load("background.jpg")
sleepbtn = pygame.image.load('sleep.png')
pizzabtn = pygame.image.load('pizza.png')
pizzabtn = pygame.transform.scale(pizzabtn, (50,50))

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,0, 0] #health range 0-100, health rage 65-165 to make up for pixels, delay counter, sleeping function enabled
hunger = [100,175,0,0]








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



    
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
        hunger[3] = 1
        sleep[3] = 1
    elif event.type == MOUSEBUTTONUP:
        hunger[3] = 0
        sleep[3] = 0
        mouse_up = event.button
        mouse_up_x,mouse_up_y = event.pos
        
    if mouse_down_x in range(420,470) and mouse_down_y in range(70, 120) and hunger[2] > 20 and hunger[3] == 1:
            temp = hunger[1]
            temp += random.randrange(10, 25, 1)
            print temp
            if temp >= 175:
                hunger[1] = 175
                temp = 0
                hunger[3] = 0
            else:
                hunger[1] = temp
                hunger[3] = 0
            hunger[0] = sleep[1]-75
            hunger[2] = 0
            hunger[3] = 0
    if mouse_down_x in range(420,520) and mouse_down_y in range(20, 65) and sleep[2] > 20 and sleep[3] == 1:
        temp = sleep[1]
        temp += random.randrange(10, 25, 1)
        print temp
        if temp >= 165:
            sleep[1] = 165
            temp = 0
        else:
            sleep[1] = temp
        sleep[0] = sleep[1]-65
        sleep[2] = 0
        sleep[3] = 0

    if keys[97]:
        print'yes'
        time.sleep(.1)
            
    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))
    screen.blit(pizzabtn, (420,70))
    print_text(font1, 475, 75, "Eat")
    
    screen.blit(sprite, (150,250))
    
    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 green

    pygame.draw.line(screen,white,(70,460),(180,460),20) #hunger bar bckgrnd
    pygame.draw.line(screen,red,(75,460),(175,460),14) #hunger bar bckgrnd red
    pygame.draw.line(screen,green,(75,460),(hunger[1],460),14) #hunger bar bckgrnd green
    
    pygame.display.update()
    temp = random.randrange(0,500,1)
    temp1 = random.randrange(0,100,1)
    sleep[2] += .01
    hunger[2] += .05
    if sleep[1] >=65 and temp == 5:
        sleep[1] -= 1
    if hunger[1] >=75 and temp1 == 5:
        hunger[1] -= 1

    for event in pygame.event.get():
        #Keydown
        if event.type == KEYDOWN:
            if event.key == K_9:
                sleep[1] = 70
                hunger[1] = 80

Look into breaking your code up into classes and functions. I also suggest you use dict for some things, such as setting the image to load.

import sys, random, time, pygame, os, threading #where you are using threading etc
from pygame.locals import *
### for example this      
## do not produce list and run linear search from long range    if mouse_down_x in range(420,470) and mouse_down_y in range(70, 120) and hunger[2] > 20 and hunger[3] == 1:
      if 420 <= mouse_down_x < 470 and 70 <= mouse_down_y < 120) and hunger[2] > 20 and hunger[3]:
            #temp = hunger[1]
            #temp += random.randrange(10, 25, 1)
            #print temp
            #if temp >= 175:
            #    hunger[1] = 175
            #    temp = 0
            #    hunger[3] = 0
            #else:
            #    hunger[1] = temp
            #    hunger[3] = 0
            #hunger[0] = sleep[1]-75
            #hunger[2] = 0
            #hunger[3] = 0
            hunger = [sleep[1] - 75, min(175, hunger[1] + random.randint(10, 25), 0, 0]

And there is another similar piece so you might put it to function to reuse (hunger as parameter and 175 parameterized also)

Edited 4 Years Ago by pyTony: n/a

min closing ) missing:

hunger = [sleep[1] - 75, min(175, hunger[1] + random.randint(10, 25)), 0, 0]
This article has been dead for over six months. Start a new discussion instead.