All pastes #472790 Raw Edit

Miscellany

public text v1 · immutable
·published 2007-05-05 20:27 UTC
rendered paste body
# -*- coding: utf-8 -*-

import pygame
from pygame import *
import sys, os
import threading
import random
import time #import de time
pygame.init() #init de pygame


        
class Moteur:
    """cette classe gère les fonctions vitales concernant l'affichage ..."""

    def __init__(self):
        
        self.sizex = 640
        self.sizey = 480 #640, 480 #taille de l'écran   
        self.screen = pygame.display.set_mode((self.sizex, self.sizey)) #création de la fenêtre graphique
        
        pygame.display.set_caption('Civilis')#titre pour la fenêtre
        pygame.key.set_repeat(100, 200)#on active le "set repeat"
        
        return
        
    def generation(self,casesx,casesy):
        self.casesx = casesx
        self.casesy = casesy
        self.listcases = []
        listbackground = ['herbe','desert','glace']
        listelement = ['ville','rien','foret']

        x=0
        y=0
        self.b = True
        while x < casesx*50:
            while y < casesy*50:
                case = Cases(x,y,listbackground[random.randint(0,2)],listelement[random.randint(0,2)])
                self.listcases.append(case)
                y+=50
                
            if self.b == True:
                y=25
                self.b = False
                
            else:
                y=0
                self.b = True
                
            x+=40
        return
    
    def affichage(self):
        global xmap
        global ymap
        black = 0, 0, 0
        self.screen.fill(black)
        
        for cas in self.listcases:
            if cas.x >= xmap-50 and cas.x <= xmap+self.sizex and cas.y >= ymap-50 and cas.y <= ymap+self.sizey:
                self.screen.blit(cas.imageload,(cas.x-xmap,cas.y-ymap))               
                self.screen.blit(cas.elementload,(cas.x-xmap,cas.y-ymap))
        self.affichemenu()
        
        pygame.display.update()

    def printtext(self,text,x,y):
        
        """cette fonction sert à afficher du texte.
        Il suffit l'invoquer avec comme argument le message à afficher"""
        font = pygame.font.Font("font.ttf", 17 )
        font.set_underline(1)
        t = font.render(text, 1, (0,0,0))
        tpos = t.get_rect()
        tpos.center = (x,y)
        self.screen.blit(t, tpos)
        pygame.display.update()
        return
                    
    def affichemenu(self):
        
        """affiche le menu"""
        xmouse, ymouse = pygame.mouse.get_pos()
        self.screen.blit(pygame.image.load(os.path.join("data","menu.png")).convert(),(self.sizex-140,0))
        for cas in self.listcases:
            if xmouse <= cas.x-xmap+50 and xmouse >= cas.x-xmap and ymouse <= cas.y-ymap+50 and ymouse >= cas.y-ymap:
                self.printtext(cas.background,self.sizex-100,self.sizey-240)
                self.printtext(cas.element,self.sizex-100,self.sizey-220)
                
                return
        return
        
class Cases:

    def __init__(self,x,y,background,element):
        self.x = x
        self.y = y
        self.background = background
        self.element = element
        self.imageload = pygame.image.load(os.path.join("decors",self.background+".png")).convert_alpha()
        self.elementload = pygame.image.load(os.path.join("elements",self.element+".png")).convert_alpha()
        return

    def changeback(self,newback):
        self.background = newback
        return
    def changeelement(self,newelement):
        self.element = newelement
        return
    
class Gest:
    
    def __init__(self):
        self.xmouse = 0
        self.ymouse = 0
        return
    
    def evenement(self):
        """ici, on gere les entrées de l'utilisateur !"""
        
        global xmap
        global ymap
        continu = True
        while continu == True:
            self.xmouse, self.ymouse = pygame.mouse.get_pos()
            if self.xmouse <= 10:
                xmap -=10
            if self.xmouse >= a.sizex-10:
                xmap += 10
            if self.ymouse <= 10:
                ymap -= 10
            if self.ymouse >=a.sizey-10:
                ymap += 10
                
            for event in pygame.event.get():
                
                if event.type == QUIT:
                    print"fini"
                    continu = False
                    pygame.quit()
                    sys.exit()
                    return
                elif event.type == KEYDOWN:
                    if event.key == K_UP:        
                        ymap -=10
                    if event.key == K_DOWN:
                        ymap +=10
                    if event.key == K_LEFT:
                        xmap -= 10
                    if event.key == K_RIGHT:
                        xmap += 10
            if xmap < 0:
                xmap = 0
            if ymap < 0:
                ymap = 0
            
            a.affichage()







if __name__ == '__main__':        
    xmap = 0
    ymap = 0
    a = Moteur()
    a.generation(10,10)
    a.affichage()
    even = Gest()
    t = threading.Thread(target=even.evenement)#,args=())
    t.start()