Monday, March 31, 2014

Marking period wrap up

Over the past two weeks, I finished nearly all of the python programs on codingbat. That's pretty much it

Friday, March 14, 2014

03.10.14 - 03.14.14

Played around with some rotational vectors in pygame:
import pygame, random, sys
from pygame.locals import *

WINDOWWIDTH=600
WINDOWHEIGHT=600
BACKGROUNDCOLOR=(0, 0, 0)
FPS=40

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

pygame.init()
mainClock=pygame.time.Clock()
windowSurface=pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
pygame.display.set_caption('Test program')
pygame.mouse.set_visible(False)

image1=pygame.image.load('arrowUp.png')
width1, height1=image1.get_size()

while True:
    #playerRect.topleft=(WINDOWWIDTH/2, WINDOWHEIGHT/2)

    while True:
        image2=pygame.transform.rotate(image1, 5)
        width2, height2=image2.get_size()
        for event in pygame.event.get():
            if event.type==QUIT:
                terminate()

            if event.type==KEYDOWN:
                if event.key==K_RIGHT:
                    image2=pygame.transform.rotate(image1, 5)
                    width2, height2=image2.get_size()
                if event.key==K_ESCAPE:
                    terminate()

        windowSurface.fill(BACKGROUNDCOLOR)
        windowSurface.blit(image2, [round(WINDOWWIDTH/2-(width1-width2)/2), round(WINDOWHEIGHT/2-(height1-height2)/2)])
        pygame.display.update()

        mainClock.tick(FPS)

I also got images to change when certain actions are taken
import pygame, random, sys
from pygame.locals import *

WINDOWWIDTH=600
WINDOWHEIGHT=600
BACKGROUNDCOLOR=(0, 0, 0)
FPS=40
PLAYERMOVERATE=5

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

#Initalize
pygame.init()
mainClock=pygame.time.Clock()
windowSurface=pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
pygame.display.set_caption('Test program')
pygame.mouse.set_visible(False)

playerImage=pygame.image.load('arrowUp.png')
playerRect=playerImage.get_rect()

while True:
    playerRect.topleft=(WINDOWWIDTH/2, WINDOWHEIGHT-50)
    moveLeft=moveRight=moveUp=moveDown=False

    while True:
        for event in pygame.event.get():
            if event.type==QUIT:
                terminate()

            if event.type==KEYDOWN:
                if event.key==K_UP and event.key==K_RIGHT:
                    playerImage=pygame.image.load('arrowUpRight.png')
                if event.key==K_LEFT:
                    playerImage=pygame.image.load('arrowLeft.png')
                    moveRight=False
                    moveLeft=True
                if event.key==K_RIGHT:
                    playerImage=pygame.image.load('arrowRight.png')
                    moveLeft=False
                    moveRight=True
                if event.key==K_UP:
                    playerImage=pygame.image.load('arrowUp.png')
                    moveDown=False
                    moveUp=True
                if event.key==K_DOWN:
                    playerImage=pygame.image.load('arrowDown.png')
                    moveUp=False
                    moveDown=True

            if event.type==KEYUP:
                if event.key==K_LEFT:
                    moveLeft=False
                if event.key==K_RIGHT:
                    moveRight=False
                if event.key==K_UP:
                    moveUp=False
                if event.key==K_DOWN:
                    moveDown=False
                if event.key==K_ESCAPE:
                    terminate()
                   
            if event.type==MOUSEMOTION:
                playerRect.move_ip(event.pos[0] - playerRect.centerx, event.pos[1] - playerRect.centery)


        if moveLeft and playerRect.left>0:
            playerRect.move_ip(-1*PLAYERMOVERATE, 0)
        if moveRight and playerRect.right<WINDOWWIDTH:
            playerRect.move_ip(PLAYERMOVERATE, 0)
        if moveUp and playerRect.top>0:
            playerRect.move_ip(0, -1*PLAYERMOVERATE)
        if moveDown and playerRect.bottom<WINDOWHEIGHT:
            playerRect.move_ip(0, PLAYERMOVERATE)

        pygame.mouse.set_pos(playerRect.centerx, playerRect.centery)

        windowSurface.fill(BACKGROUNDCOLOR)
        windowSurface.blit(playerImage, playerRect)
        pygame.display.update()

        mainClock.tick(FPS)








         


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)