[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [pygame] Anti-aliasing problem



Dirk Krause <d_krause@pixelpark.com>:
>
> 
> Hi Magnus,
> 
> it would help if you posted the code, I guess.

Attached to this mail, warts and all <wink>

--
Magnus Lie Hetland                                  The Anygui Project
http://hetland.org                                  http://anygui.org

import pygame.event, pygame.sprite, pygame.font
from pygame.locals import *
import eggcatcher.images, eggcatcher.objects
from eggcatcher import config
import sys, os

def run():
    pygame.init()
    screen_size = config.screen_size
    if config.full_screen:
        screen = pygame.display.set_mode(screen_size, FULLSCREEN)
    else:
        screen = pygame.display.set_mode(screen_size)
    pygame.display.set_caption('Egg Catcher')
    pygame.mouse.set_visible(0)
    
    reptile = eggcatcher.images.load('reptile.gif')
    background = eggcatcher.images.tile(reptile, screen.get_size())

    screen.blit(background, (0, 0))

    catcher = eggcatcher.objects.Catcher()
    catcher_front = eggcatcher.objects.CatcherFront()

    egg = eggcatcher.objects.Egg()
    allsprites = pygame.sprite.RenderUpdates([catcher, egg, catcher_front])

    game_over = 0
    startup = 1
    startup_drawn = 0
    score = 0
    speed = config.drop_speed
    
    while 1:
        for event in pygame.event.get():
            if event.type is QUIT:
                return
            elif event.type is KEYDOWN and event.key is K_ESCAPE:
                return
            elif (game_over or startup) and event.type in [MOUSEBUTTONDOWN, KEYDOWN]:
                screen.blit(background, (0, 0))
                pygame.display.flip()
                speed = config.drop_speed
                egg.reset(speed=speed)
                score = 0
                game_over = startup = 0
        if startup:
            if not startup_drawn:
                image = eggcatcher.images.load(config.start_image)
                imagepos = image.get_rect()
                
                font = pygame.font.Font(None, 36)
                text = font.render("Welcome to Egg Catcher. Click to Start, Esc to Quit", 1, (10, 10, 10))
                textpos = text.get_rect()
                
                spacing = config.margin

                area = screen.get_rect()

                top = area.height - textpos.height - spacing - imagepos.height
                top //= 2

                centerx = area.centerx

                imagepos.midtop = centerx, top
                screen.blit(image, imagepos)

                top += imagepos.height + spacing
                
                textpos.top = top
                textpos.centerx = centerx
                screen.blit(text, textpos)
                
                pygame.display.flip()
                startup_drawn = 1
        elif game_over:
            font = pygame.font.Font(None, 36)
            text = font.render(("Game Over. Score: %s Eggs. Click to Restart, Esc to Quit" % score), 1, (10, 10, 10))
            textpos = text.get_rect()
            textpos.centerx = screen.get_rect().centerx
            textpos.centery = screen.get_rect().centery
            # Why doesn't the anti-aliasing work here?!
            screen.blit(text, textpos)
            pygame.display.flip()
        else:
            allsprites.update()
            if egg.rect.bottom >= catcher.rect.bottom - 10:
                if egg.rect.colliderect(catcher.rect):
                    score += 1
                    speed += config.speed_increase
                    egg.reset(speed=speed)
                else:
                    screen.blit(background, (0, 0))
                    pygame.display.flip()
                    game_over = 1
                    continue
            screen.blit(background, (0, 0))
            updates = allsprites.draw(screen)
            pygame.display.update(updates)
        
def main(*args):
    path = os.path.abspath(args[0])
    dir = os.path.split(path)[0]
    os.chdir(dir)
    sys.path.insert(0, '.')
    run()