EnstaEquipeYiatus: test.py

File test.py, 3.7 KB (added by ensta-yiatus, 14 years ago)
Line 
1#!/usr/bin/env python
2
3#Import Modules
4import os, pygame
5from pygame.locals import *
6
7if not pygame.font: print 'Warning, fonts disabled'
8if not pygame.mixer: print 'Warning, sound disabled'
9
10#functions to create our resources
11def load_image(name, colorkey=None):
12    fullname = os.path.join('data', name)
13    try:
14        image = pygame.image.load(fullname)
15    except pygame.error, message:
16        print 'Cannot load image:', fullname
17        raise SystemExit, message
18    image = image.convert()
19    if colorkey is not None:
20        if colorkey is -1:
21            colorkey = image.get_at((0,0))
22        image.set_colorkey(colorkey, RLEACCEL)
23    return image, image.get_rect()
24
25
26
27class Player(pygame.sprite.Sprite):
28    def __init__(self):
29        self.fond_player = pygame.Surface((80,80))
30        self.fond_player = self.fond_player.convert()
31        self.fond_player.fill((250, 0, 0))
32        self.orient_up = pygame.image.load('data/player_up.png')
33        self.orient_down = pygame.image.load('data/player_down.png')
34        self.orient_left = pygame.image.load('data/player_left.png')
35        self.orient_right = pygame.image.load('data/player_right.png')
36        self.posx = 400
37        self.posy = 400
38        self.orientation = 2
39        #a definir
40       
41    def update(self):
42        if self.orientation == 8:
43            self.fond_player.blit(self.orient_up,(0,0))
44        if self.orientation == 2:
45            self.fond_player.blit(self.orient_down,(0,0))
46        if self.orientation == 4:
47            self.fond_player.blit(self.orient_left,(0,0))
48        if self.orientation == 6:
49            self.fond_player.blit(self.orient_right,(0,0))
50
51           
52    def Up(self):
53        self.posy = self.posy - 1
54        self.orientation = 8
55    def Down(self):
56        self.posy = self.posy + 1
57        self.orientation = 2
58    def Left(self):
59        self.posx = self.posx - 1
60        self.orientation = 4
61    def Right(self):
62        self.posx = self.posx + 1
63        self.orientation = 6
64
65def main():
66    """this function is called when the program starts.
67       it initializes everything it needs, then runs in
68       a loop until the function returns."""
69
70#Initialize Everything
71    pygame.init()
72    screen = pygame.display.set_mode((800, 800))
73    pygame.display.set_caption('Bomb Fever')
74    pygame.mouse.set_visible(0)
75
76   
77   
78#Create The Background
79    BGpattern = pygame.image.load('data/background.png')
80    background = pygame.Surface(screen.get_size())
81    background = background.convert()
82   
83    for i in range(screen.get_width()/BGpattern.get_width()+1):
84        for j in range(screen.get_height()/BGpattern.get_height()+1):
85            background.blit(BGpattern,(i*BGpattern.get_width(),j*BGpattern.get_height()))
86
87
88#Prepare Game Objects
89    clock = pygame.time.Clock()
90    player = Player()
91   
92#Display The Background
93    screen.blit(background,(0,0))
94    pygame.display.flip()
95   
96
97   
98    while 1:
99        keystate = pygame.key.get_pressed()
100        clock.tick(60)
101        if keystate[K_LEFT]:
102            player.Left()
103        if keystate[K_RIGHT]:
104            player.Right()
105        if keystate[K_UP]:
106            player.Up()
107        if keystate[K_DOWN]:
108            player.Down()
109           
110       
111        for event in pygame.event.get():
112            if event.type == QUIT:
113                return
114            elif event.type == KEYDOWN and event.key == K_ESCAPE:
115                return
116
117
118
119        screen.blit(background,(0,0))
120        #les autres trucs a remettre a jour
121        screen.blit(player.fond_player, (player.posx,player.posy))
122        player.update()
123        pygame.display.flip()
124#this calls the 'main' function when this script is executed
125if __name__ == '__main__': main()