Pages

Saturday, March 15, 2014

simple introduction screen pygame

###  An intro screen.  Trying to make my personality test, ###
### will post it when it's ready so you can try it out! ###

import pygame
from pygame.locals import*
import math
from random import randint, choice
from math import*
from sys import exit

pygame.init()




H = 800
W = 800
screen = pygame.display.set_mode((H, W))

font = pygame.font.Font(None, 32)
L_font = pygame.font.Font(None, 64)   
clock = pygame.time.Clock()
BLUE = (45, 0, 200)
GREEN = (0, 200, 45)
PURPLE = (200, 95, 255)
BLACK = (0, 0, 0)   

def display_box():
       
    # the needed variables
    pygame.draw.rect(screen, (200, 195, 100), (20, 20, 760, 760))
    pygame.draw.rect(screen, (BLUE), (30, 30, 740, 740), 10)
    intro = font.render("For instructions, press Space bar", False, BLUE)
    title1 = font.render("For entertainment only", False, GREEN)
    title2 = L_font.render("Personality", False, BLUE)
    title3 = L_font.render("Test", False, BLUE)
    author = font.render("Nellie Tobey, 2014", False, BLACK)
    clock.tick(30)
   
     # the first screen you want them to see. #
     # the function draws it,   the loop will change it# 
    screen.blit(intro, (40, 40))
    screen.blit(title1, (60, 60))
    screen.blit(title2, (290, 300))
    screen.blit(title3, (340, 340))
    screen.blit(author, (40, 640))
    pygame.display.flip()
   
    # these three variables will help us change the
    # state of the function
      
    done = False   
    instruction_screen = 1
    display_instructions = 1
    # main LOOP. This changes the screen as the state of function changes #
 
  while done==False:       
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True
                exit()

        if instruction_screen ==1:
            screen.blit(intro, (40, 40))
            screen.blit(title1, (60, 60))
            screen.blit(title2, (290, 300))
            screen.blit(title3, (340, 340))
            screen.blit(author, (40, 640))
            pygame.display.flip()
        keys = pygame.key.get_pressed()

           # if you push space, things change

        if (keys[K_SPACE]): 
            instruction_screen = 2  #we could create more, and more screens
            display_instructions = 0
        if display_instructions == 0:           
            text = L_font.render("INSTRUCTIONS", False, (0, 35, 0))
            pygame.draw.rect(screen, (200, 255, 255), (20, 20, 760, 760))
            screen.blit(text, (H/2 - 170, W/2 - 200))
            pygame.display.flip()
       
   
       
display_box()


def instructions():
    pass
def test():
    pass
def answers():                         #these are where my app will go. 
    pass
def questions():
    pass
def results():
    pass

Saturday, March 8, 2014

Class and inheritance in Python

# my example to show a shell for class's for a game in Python
# I'm sure there is a better way, but this will kinda show you how to make the inheritance, and class thing
# work together. You can switch the modules around to any class, as long as the class is defined before you #use them. 

##  Meggamuffins is my name!  Eating mushrooms is NOT my game!

#When you run this in terminal, it should show you how the order is written backwards,

# but the computer processes it bottom up.  Same as the other shell, but in all classes.

## ALL props to Zed Shaw and his amazing learning tools.  http://learnpythonthehardway.org

class State(object):
    #could store global variables here, instead of making them global
    def __init__(self, name):
        self.name = name
       
       
class Wingame(object):
    def __init__(self, chicken_dinner):
        self.chicken_dinner = chicken_dinner
    def winner(self):
        print "winner winner , chicken dinner! %s"

class Endgame(Wingame):
    def __init__(self, wawa):
        self.wawa = wawa
    def end_credits(self):  #game over
        print " You LOSE - Nellie Tobey - shell designer"
        Wingame(object).winner()
       
class Battle(Endgame):
    def __init__(self, charge):
        self.charge = charge
    def fight(self):
        print "Lets get ready to wooooop azz."
        Endgame(object).end_credits()

class Level_up(Battle):
    def __init__(self, woot):
        self.woot = woot
    def stats(self):
        print " congrats, you are level -impotent-"
        Battle(object).fight()
       
class Enemy_pos(Level_up):
    def __init__(self, pos_badguy):
        self.pos_badguy = pos_badguy
    def point_enemy(self):
        print "There he goes!  Gettim!"
        Level_up(object).stats()
       
class Enemy(Enemy_pos):
    def __init__(self, badguy):
        self.badguy = badguy
    def Rawwwr(self):
        print " we need more chose to be the badguy games!"
        Enemy_pos(object).point_enemy()
       
class Hero_pos(Enemy):
    def __init__(self, sexy_pos):
        self.sexy_pos = sexy_pos
    def point_hero(self):
        print " There he goes with that swaaard again!"
        Enemy(object).Rawwwr()
       
class Hero(Hero_pos):
    def __init__(self, sexy):
        self.sexy = sexy
    def mighty(self):
        print "FF is better at female lead roles then American cinema"
        Hero_pos(object).point_hero()
       
class Npc_pos(Hero):
    def __init__(self, poser):
        self.poser = poser
    def point_npc(self):
        print "character is at point (2, 3)"
        Hero(object).mighty()

class Npc(Npc_pos):
    def __init__(self, cpu):
        self.cpu = cpu
    def char_dialog(self):
        print "You have a quest, let me give you arbitrary things to do!"
        Npc_pos(object).point_npc()
       
class Inventory(Npc):
    def __init__(self, stuffing):
        self.stuffing = stuffing
    def stuff(self):
        print "inventory = this, that, the other thing"
        Npc(object).char_dialog()

class Images(Inventory):
    def __init__(self, image):
        self.image = image
    def show_image(self):
        print " ******************* "
        print " **     INTRO     ** "
        print " ******************* "
       
        Inventory(object).stuff()

class Storyline(Images):
    def __init__(self, story):
        self.story = story
    def storyintro(self):
        print "story introduction"
        Images(object).show_image()

class Start(Storyline):
    def __init__(self, heregoes):
        self.heregoes = heregoes
    def game_instructions(self):
        print "start, game instructions"
        Storyline(object).storyintro()


Gamestart = Start(object).game_instructions()

       

Thursday, March 6, 2014

simple bounce pygame python code

## I'm always looking for examples, so if I can provide one that I had a hard time
## finding ,  I will.    Remember to put your own images in.  mine only work for me.

##  diagonal bounce from chapter 5, Beggining Game Development with Pygame,
## python: Will McGugan


background_image = ("UnicornPoop/CRSwall.jpg")
sprite_image_Right = ("UnicornPoop/brushRight.png")  #your images here  ###################("filename/imagename.png") or just('filename.ext')
sprite_image_Left = ("UnicornPoop/brushLeft.png")

import pygame
from pygame.locals import*
from sys import exit
from vectorMagnitude import Vector2

pygame.init()

screen = pygame.display.set_mode((600, 800), 0, 32)

background= pygame.image.load(background_image).convert()

sprite = pygame.image.load(sprite_image_Right)
sprite_left = pygame.image.load(sprite_image_Left)

clock = pygame.time.Clock()

x, y = 100., 100.
speed_x, speed_y = 133., 170.


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

       

    screen.blit(background, (-5, -5))
    screen.blit(sprite, (x, y))
   
    time_passed = clock.tick(30)
    time_passed_seconds = time_passed / 1000.

    x += speed_x * time_passed_seconds
    y += speed_y * time_passed_seconds

    if x > 600 - sprite.get_width():
        speed_x = -speed_x
        x = 600 - sprite.get_width()
    elif x < 0:
        speed_x = -speed_x
        x = 0

    if y > 800 - sprite.get_height():
        speed_y = -speed_y
        y = 800 - sprite.get_height()
    elif y < 0:
        speed_y = -speed_y
        y = 0

    pygame.display.update()

   

pygame vector image bounce with Vector class

##### mostly this code is re-written, but if I had to look for it, maybe you did too
##  if you cut and paste (which you should type yourself to learn)  just the
## import  math, and vector class , and the stuff that's
## edited out at the bottom, you can see what the vector class does
## I looked forever for a code to make the object bounce, or follow.
## nobody really says, "you have to have a vector class, or module to do it"
## apparently you do.   If not, let me know in comments, I'd love to learn how


import pygame
from pygame.locals import*
from sys import exit
import math
#from book by Will McGugan Beginning Game Development with Python and Pygame
#and modification from stackoverflow.com,  Dominic Kexel
class Vector2(tuple):
   
    def __new__(typ, x = 1.0, y = 1.0):
        n = tuple.__new__(typ, (int(x), int(y)))
        n.x = x
        n.y = y
        return n
    def __str__(self):
        return "(%s, %s)"%(self.x, self.y)
    def __add__(self, other): 
        return self.__new__(type(self), self.x + other.x, self.y + other.y)
   
    def __sub__(self, other):
        return self.__new__(type(self), self.x - other.x, self.y - other.y)
   
    def __neg__(self, other):
        return self.__new__(type(self), -self.x, -self.y)

    def __mul__(self, scalar):
        return self.__new__(type(self), self.x * scalar, self.y * scalar)
   
    def __div__(self, scalar):
        return self.__new__(type(self), self.x / scalar, self.y / scalar)

    @staticmethod
    def from_points(P1, P2):
        return Vector2( P2[0] - P1[0], P2[1] - P1[1] )

    def get_magnitude(self):
        return math.sqrt( self.x**2 + self.y**2 )

    def normalize(self):
        magnitude = self.get_magnitude()
        self.x /= magnitude
        self.y /= magnitude

   
   
class main(Vector2):

    background_image = ("UnicornPoop/CRSwall.jpg") # for the images, you'll have to put your own in
    sprite_image_Right = ("UnicornPoop/brushRight.png") #filename / your image
    sprite_image_Left = ("UnicornPoop/brushLeft.png")   #filename / your image

    pygame.init()

    screen = pygame.display.set_mode((600, 800), 0, 32)

    background= pygame.image.load(background_image).convert()

    sprite = pygame.image.load(sprite_image_Right)
    spriteLeft = pygame.image.load(sprite_image_Left)

    clock = pygame.time.Clock()

    position = Vector2(100.0, 100.0)
    speed = 250
    heading = Vector2()
    sprite_pos = pygame.mouse.get_pos()
    while True:

       


        for event in pygame.event.get():
            if event.type == QUIT:
                exit()
        if event.type == MOUSEBUTTONDOWN:
           
           
            destination = Vector2(*event.pos) - Vector2(*sprite.get_size())/2
            heading = Vector2.from_points(position, destination)
            heading.normalize()
           
           
        screen.blit(background, (-5, -5))
        screen.blit(sprite, position)

   
        time_passed = clock.tick()
        time_passed_seconds = time_passed / 1000.

        distance_moved = time_passed_seconds * speed
        position += heading * distance_moved
   
        pygame.display.update()

main()

#A = (10.0, 20.0)
#B = (30.0, 35.0)
#AB = Vector2.from_points(A, B)
#step = AB * .1
#position = Vector2(10, 20)  #the book says to put (A.x, A.y) but I get error, has no attribute x
#for n in range(10):
    #position += step
    #print position
#A = (10.0, 20.0)
#B = (30.0, 35.0)
#C = (15.0, 45.0)
#AB = Vector2.from_points(A, B)
#BC = Vector2.from_points(B, C)

#AC = Vector2.from_points(A, C)
#print "Vector AC is",AC

#AC = AB + BC

#print "AB + BC is", AC


#print "Vector AB is", AB
#print "Magnitude of Vector AB is ", AB.get_magnitude()
#AB.normalize()
#print "Vector AB normalized is", AB
#print AB.get_magnitude()