Friday, March 7, 2014

03.03.2014 - 03.07.2014

After finishing the first python book last week, I started tinkering around with some of the pygame stuff not taught in the book. Here are the programs I made / edited.

Moving background program:
import pygame, random, sys
from pygame.locals import *
#import pygame.sprite as sprite

theClock = pygame.time.Clock()

WINDOWHEIGHT=600
WINDOWWIDTH=600

background = pygame.image.load('starBackground.jpg')

#background_size = background.get_size()
background_rect = background.get_rect()
screen = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
#w,h = background_size
x = 0
y = 0

x1 = 0
y1 = -WINDOWHEIGHT

def terminate():
    pygame.quit()
    sys.exit()

running = True

while running:
    screen.blit(background,background_rect)
    pygame.display.update()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
    y1 += 5
    y += 5
    screen.blit(background,(x,y))
    screen.blit(background,(x1,y1))
    if y > WINDOWHEIGHT:
        y = -WINDOWHEIGHT
    if y1 > WINDOWHEIGHT:
        y1 = -WINDOWHEIGHT
    #pygame.display.flip()
    pygame.display.update()
    theClock.tick(100)

    if event.type==KEYDOWN:
        if event.key==K_ESCAPE:
            terminate()

   
and this

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

theClock=pygame.time.Clock()

HEIGHT=600
WIDTH=600

r=0
g=0
b=0
BACKGROUNDCOLOR=(r, g, b)

reverse=False

windowSurface=pygame.display.set_mode((WIDTH, HEIGHT))

def terminate():
    pygame.quit()
    sys.exit()

running=True

while running:
    windowSurface.fill(BACKGROUNDCOLOR)

    for event in pygame.event.get():
        if event.type==KEYDOWN:
            if event.key==K_ESCAPE:
                terminate()

    if reverse==False:
        if r<255 and b==255:
            r+=1
        elif g<255 and r==255:
            g+=1
        elif b<255:
            b+=1
        elif r==255 and b==255 and g==255:
            reverse=True
    elif reverse==True:
        if r>0 and b==0:
            r-=1
        elif g>0 and r==0:
            g-=1
        elif b>0:
            b-=1
        elif r==0 and b==0 and g==0:
            reverse=False

    BACKGROUNDCOLOR=(r, g, b)
   
    pygame.display.update()
    theClock.tick(200)

No comments:

Post a Comment