Pages

Thursday, February 20, 2014

Same Pygame code... cleaner.

Took me a bit to figure out how to make it work in a function, instead of in the 'while' loop.
  Got er' though.
  And you'd think getting the text to stay on top of the background would be simple....  but NO! had to play with it to figure it out.  See octothorps.
   
  #one of these is my culprit ... If I take out lines   4, 3, 2, 1 individually
  #the game runs black window,  after I took them out and put them back in
 #my text disappeared again....  It has to be here somewhere.
 # I like to save a working copy of the code before I mess with it. Just so I can
 # refer to old code, and in case I completely mess it up.
# Apparently the text has to be 'blit' ((load image to screen)) before the background
# Seems really out of order to me.  Gonna have to keep researching.  wanna code.

import pygame
import random
import sys

from pygame.locals import*

#trying to figure out sprite
width = 100
height = 100
size = [40, 40]
   
   
   
   
#the part that works
def main():   
    pygame.init()
   
    SCREEN = pygame.display.set_mode((800,800))
    pygame.display.set_caption('Nellie wants to be a Geek!')

    x = random.randint(0, 255)
    y = random.randint(10, 245)
    z = random.randint(15, 200)
    RAINBOW = (x, y, z)
    x1 = random.randint(0, 255)
    y1 = random.randint(10, 245)
    z1 = random.randint(15, 200)
    RAINBOW1 = (x1, y1, z1)
    BLACK = (0, 0, 0)
    WHITE = (255, 255, 255)
    PURPLE = (255, 0, 220)
    BLUE = (74, 74, 250)

   
    BACKGROUND = pygame.Surface(SCREEN.get_size())
    BACKGROUND = BACKGROUND.convert()
    BACKGROUND.fill((100,75,200))
   
   

    pygame.draw.rect(BACKGROUND, BLUE, (0, 0, 800, 675))
    pygame.draw.rect(BACKGROUND, BLACK, (0, 0, 800, 75))
    pygame.draw.line(BACKGROUND, WHITE, (0, 675), (800, 675), 10)
   
    (mouseX, mouseY) = pygame.mouse.get_pos()
    pygame.draw.circle(BACKGROUND, (RAINBOW1), (mouseX, mouseY), 20, 20)
    x1 = random.randint(0, 255)
    y1 = random.randint(10, 245)
    z1 = random.randint(15, 200)
    RAINBOW1 = (x1, y1, z1)
    font = pygame.font.Font(None, 49)
    text = font.render("Pooping Unicorn! Clicky clicky!", False, RAINBOW1)
    textPos = text.get_rect()
    textPos.centerx = BACKGROUND.get_rect().centerx

    #Sprite kinda....
    #speed = [2, 2]
    #ball = pygame.sprite.Sprite()
    #ball.image = pygame.image.load("100_0643.jpg").convert()
    #ball.rect = ball.image.get_rect()
    #ball.rect.topleft = [75,300]
    #screen.blit(ball.image, ball.rect)
   
   
    pygame.display.update()
    #screen.blit(ball.image, ball.rect) #1
    BACKGROUND.blit(text, textPos)    #2
    SCREEN.blit(BACKGROUND, (0,0))     #3  
    pygame.display.flip() #4

    while 1:   
       
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()   
            if event.type == MOUSEBUTTONDOWN :
                main()
               
                BACKGROUND.blit(text, textPos)
       

if __name__ == '__main__': main()

   

No comments:

Post a Comment