Page 1 of 1

Game Dev I - PyGame Examples

PostPosted: Wed Sep 05, 2012 8:40 pm
by BenChang
pygame_bear.jpg
pygame_bear.jpg (127.04 KiB) Viewed 696 times


Code: Select all
import pygame
import sys


'''
    PyGame Tutorial - movement, spritesheets, collision, using ASCII files for level maps
    code: Marc Destefano
    bear sprite: Silvia Ruzanka

'''

class Ball(object):
    """this is the enemy ball thing"""
    def __init__(self):
        super(Ball, self).__init__()
        self.img = pygame.image.load("ball.png").convert_alpha()
        self.rect = self.img.get_rect()
        self.rect.x = 10
        self.rect.y = 10
        self.vel_x = 3
        self.vel_y = 3
        self.score = 0
       
    def update(self, screen_rect):
        """updates ball's position"""
        future_rect = self.rect.move(self.vel_x, self.vel_y)
        if future_rect.left < screen_rect.left or future_rect.right > screen_rect.right:
            self.vel_x = -self.vel_x
            self.score += 1
        if future_rect.top < screen_rect.top or future_rect.bottom > screen_rect.bottom:
            self.vel_y = -self.vel_y
            self.score += 1
        self.rect.move_ip(self.vel_x, self.vel_y)
       
    def draw(self, screen):
        """draws ball to screen"""
        screen.blit(self.img, self.rect)
       
class Player(object):
    """Player 1"""
    def __init__(self):
        super(Player, self).__init__()
        self.img = pygame.image.load("bear.png").convert_alpha()
        self.rect = self.img.get_rect()
        self.direction = 0 #right = 0, left = 1
        #up, down, left, right movement flags
        self.moving = [False, False, False, False]
        self.speed = 5
        self.frame = 0
       
    def update(self):
        """updates player position"""
        if self.moving[0] == True:
            self.rect.move_ip(0, -self.speed)
        elif self.moving[1] == True:
            self.rect.move_ip(0, self.speed)
        if self.moving[2] == True:
            self.rect.move_ip(-self.speed,0)
            self.direction = 1
        elif self.moving[3] == True:
            self.rect.move_ip(self.speed,0)
            self.direction = 0
        if self.moving == [False, False, False, False]:
            self.frame = 0
        else:
            self.frame += 1
            if self.frame > 39:
                self.frame = 0
   
    def draw(self, screen):
        """draws player"""
        screen.blit(self.img, self.rect, pygame.Rect(64*(self.frame/10),self.direction*143,64,143))
       
class Game(object):
    """main gameworld object"""
    def __init__(self):
        super(Game, self).__init__()
        pygame.init()
        self.screen = pygame.display.set_mode((640, 448))
        self.screen_rect = self.screen.get_rect()
        self.clock = pygame.time.Clock()
        self.ball = Ball()
        self.player = Player()
        self.font = pygame.font.Font(None, 32)
        self.map = open("map.txt").readlines()
        self.map = [line.rstrip() for line in self.map]
        self.land = pygame.image.load("land.png").convert()
        self.water = pygame.image.load("water.png").convert()
       
    def process_events(self):
        """process all game events"""
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    sys.exit(0)
                if event.key == pygame.K_w:
                    self.player.moving[0] = True
                if event.key == pygame.K_s:
                    self.player.moving[1] = True
                if event.key == pygame.K_a:
                    self.player.moving[2] = True
                if event.key == pygame.K_d:
                    self.player.moving[3] = True
            if event.type == pygame.KEYUP:
                if event.key == pygame.K_w:
                    self.player.moving[0] = False
                if event.key == pygame.K_s:
                    self.player.moving[1] = False
                if event.key == pygame.K_a:
                    self.player.moving[2] = False
                if event.key == pygame.K_d:
                    self.player.moving[3] = False
                   
    def update(self):
        """updates the game"""
        self.ball.update(self.screen_rect)
        self.player.update()
       
    def draw(self):
        """draw the gameworld"""
        self.screen.fill((0,0,0))
        for i, row in enumerate(self.map):
            for j, column in enumerate(row):
                if column == "l":
                    #draw land
                    self.screen.blit(self.land, pygame.Rect(j*64, i*64, 64, 64))
                elif column == "w":
                    #draw water
                    self.screen.blit(self.water, pygame.Rect(j*64, i*64, 64, 64))
        self.ball.draw(self.screen)
        self.player.draw(self.screen)
        self.score_surf = self.font.render("Score: %d"%self.ball.score, 1, (255,255,255))
        self.score_rect = self.score_surf.get_rect()
        self.score_rect.top = 250
        self.screen.blit(self.score_surf, self.score_rect)
       
g = Game()
while True:
    g.clock.tick(30)
    g.process_events()
    g.update()
    g.draw()
    pygame.display.flip()