Page 1 of 1

Simple pygame example

PostPosted: Tue Dec 04, 2012 4:08 pm
by BenChang
Code: Select all
import pygame
import random

from pygame.locals import *

class Apple:
    def __init__(self):
        self.img=pygame.image.load("apple2.png").convert_alpha()
        self.rect = self.img.get_rect()
       
        print "apple:",self.rect
       
        self.x=random.uniform(20,780)
        self.y=20
       
    def draw(self,screen):
        # draw onto the screen
       
        screen.blit(self.img,(self.rect.x,self.rect.y))
       
    def update(self):
        # make it fall down
       
        self.y += 0.5
       
        self.rect.x = self.x
        self.rect.y = self.y
       
class Basket:
    def __init__(self):
        self.img=pygame.image.load("basket.png").convert_alpha()
        self.rect = self.img.get_rect()
       
        self.font=pygame.font.Font("C64.ttf", 20)
       
        #check if basket collides with apple
        #if basket.colliderect(Apple):
        #    self.score = self.score+10
       
        self.score = 0
       
        print "basket", self.rect
       
        self.rect.x=400
        self.rect.y=510
       
        #self.score_value = 0
        #self.score_text = Text(screen = self.screen, x = 550, y = 20,
            #text = "Score: 0", size = 25, colour = colour.blue)
           
       
    def draw(self,screen):
        # draw onto the screen
       
        scoretext=self.font.render("Score: ", True, (25,25,50))
        screen.blit(scoretext,(550,20))
       
        screen.blit(self.img,(self.rect.x,self.rect.y))
   
    def update(self):
        # move basket to mouse position
       
        m = pygame.mouse.get_pos()
       
        self.rect.x = m[0]
       
       
       
def main ():
    pygame.init()
    pygame.font.init()
   
    screen_width=800
    screen_height=600
   
    screen=pygame.display.set_mode ([screen_width,screen_height],DOUBLEBUF)
   
    done = False
   
    clock = pygame.time.Clock()
   
    background=pygame.image.load("treeline1.jpg").convert()
   
    apple1=Apple()
    apple2=Apple()
   
    basket=Basket()
   
    pygame.mouse.set_visible(0)
     
    #if basket.colliderect(apple(1,2)):
    #    self.score = slef.score + 10
   
    while done==False:
        # check events
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                done=True
           
        # update positions of things
       
        apple1.update()
        apple2.update()
       
        basket.update()
   
   
        # check for collisions
       
        # is the basket intersecting the apple?
       
        if basket.rect.colliderect (apple1.rect):
            print "Let the bodies hit the floor!"
       
       
        # draw some stuff
   
        screen.blit (background,(0,0))
       
        # draw some apples
       
        apple1.draw(screen)
        apple2.draw(screen)
       
        # draw the basket
       
        basket.draw(screen)
        pygame.display.flip()
       
       
        # make mouse invisible
       
       
       
        # make apples disappear after collision
       
       
       
main ()

pygame.quit()

Re: Simple pygame example

PostPosted: Tue Dec 04, 2012 4:52 pm
by BenChang
Code: Select all
import pygame
import random

from pygame.locals import *

class Apple:
    def __init__(self):
        self.img=pygame.image.load("apple2.png").convert_alpha()
        self.rect = self.img.get_rect()
       
        print "apple:",self.rect
       
        self.x=random.uniform(20,780)
        self.y=random.uniform(-50,20)
       
        self.dy = random.uniform(1,10)
       
    def draw(self,screen):
        # draw onto the screen
       
        screen.blit(self.img,(self.rect.x,self.rect.y))
       
    def update(self):
        # make it fall down
       
        self.y += self.dy
               
        self.rect.x = self.x
        self.rect.y = self.y
       
class Basket:
    def __init__(self):
        self.img=pygame.image.load("basket.png").convert_alpha()
        self.rect = self.img.get_rect()
     
        print "basket", self.rect
       
        self.rect.x=400
        self.rect.y=510

    def draw(self,screen):
        # draw onto the screen
       
        screen.blit(self.img,(self.rect.x,self.rect.y))
   
    def update(self):
        # move basket to mouse position
       
        m = pygame.mouse.get_pos()
       
        self.rect.x = m[0]
       
       
       
def main ():
    pygame.init()
    pygame.font.init()
   
    screen_width=800
    screen_height=600
   
    screen=pygame.display.set_mode ([screen_width,screen_height],DOUBLEBUF)

    font=pygame.font.Font("C64.ttf", 20)
           
    done = False
   
    clock = pygame.time.Clock()
   
    background=pygame.image.load("treeline1.jpg").convert()
   
    # make list of apples
    apples=[]
   
    # set a timer to launch an apple
   
    pygame.time.set_timer(pygame.USEREVENT, random.uniform(1000,5000))
   
    basket=Basket()
   
    pygame.mouse.set_visible(0)

    score_value = 0
    #self.score_text = Text(screen = self.screen, x = 550, y = 20,text = "Score: 0", size = 25, colour = colour.blue)
               

    while done==False:
        # check events
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                done=True
            if event.type==pygame.USEREVENT:
               apples.append(Apple())
               pygame.time.set_timer(pygame.USEREVENT, random.uniform(1000,5000))

           
        # update positions of things
       
        for a in apples:
            a.update() 
       
        basket.update()
   
        # check for collisions
       
        # is the basket intersecting any of the apples?
       
        for a in apples:
            if basket.rect.colliderect (a.rect):
                print "Let the bodies hit the floor!"
                # yes!  increase score, get rid of apple
                score_value = score_value + 1
                # remove it from the list
                apples.remove(a)
                # delete the object to free up memory
                del(a)
       
        # draw some stuff
   
        screen.blit (background,(0,0))
       
        # draw some apples
       
        for a in apples:
            a.draw(screen)      
       
        # draw the basket
       
        basket.draw(screen)
       
        # draw the score and other GUI
         
        # format the output string
        statustext = "Score: {0}".format(score_value)
               
        # blit text into a temporary bitmap surface using the font
        scoretext=font.render(statustext, True, (25,25,50))
       
        # draw it onto the screen
        screen.blit(scoretext,(550,20))
       
        # flip the buffers
        pygame.display.flip()
       
       
main ()

pygame.quit()