1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
# modules
import pygame, sys
from pygame.locals import *
# constantes
WIDTH = 1000
HEIGHT = 700
TITLE = "Ultra Super Mega Secret Project"
BACKGROUND_COLOR = (111, 207, 0)
IMAGES = ("P-left.png", "P-right.png", "P-top.png", "P-bottom.png", "plt-1.png", "plt-2.png")
P_SPEED = 1
FPS = 120
# clock et commandes de demarrage
pygame.init()
fpsClock = pygame.time.Clock()
pygame.key.set_repeat(10, 30)
# classes
class Screen(): # Classe du Canvas
def __init__(self, Width, Height, Title):
self.Width = Width
self.Height = Height
self.Title = Title
self.DISPLAY = pygame.display.set_mode((self.Width, self.Height), 0, 32)
pygame.display.set_caption(Title)
self.EntityList = []
# fonction d'affichage des entites
def draw(self, player):
self.DISPLAY.fill(BACKGROUND_COLOR)
for Entity in self.EntityList:
self.DISPLAY.blit(Entity.IMG[Entity.nb], (Entity.pos.left, Entity.pos.top))
# analyse des inputs
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN or event.type == KEYUP:
player.eventScan(event)
player.collisionScan(self.EntityList)
player.moveDraw()
self.DISPLAY.blit(player.IMG[player.nb], [player.pos.left, player.pos.top])
class Player():
# initialisation des variables de l'entite du joueur
def __init__(self, spawnX, spawnY, images):
self.IMG = (pygame.image.load(images[0]), pygame.image.load(images[1]), \
pygame.image.load(images[2]), pygame.image.load(images[3]))
self.pos = self.IMG[3].get_rect()
self.pos.left = spawnX
self.pos.top = spawnY
self.nb = 3
self.speed = P_SPEED
# bools de collision
self.contactR = False
self.contactL = False
self.contactT = False
self.contactB = False
# bools de mouvement souhaite
self.goR = False
self.goL = False
self.goT = False
self.goB = False
# fonction de scan des inputs de clavier
def eventScan(self, event):
if event.type == KEYDOWN:
if event.key in (K_DOWN, K_s):
self.goB = True
self.goT = False
self.nb = 3
elif event.key in (K_UP, K_z):
self.goT = True
self.goB = False
self.nb = 2
elif event.key in (K_RIGHT, K_d):
self.goR = True
self.goL = False
self.nb = 1
elif event.key in (K_LEFT, K_q):
self.goL = True
self.goR = False
self.nb = 0
elif event.type == KEYUP:
if event.key in (K_DOWN, K_s):
self.goB = False
elif event.key in (K_UP, K_z):
self.goT = False
elif event.key in (K_RIGHT, K_d):
self.goR = False
elif event.key in (K_LEFT, K_q):
self.goL = False
# fonction de scans des collisions
def collisionScan(self, Entities):
# reset des bools de collision
self.contactR, self.contactL, self.contactB, self.contactT = False, False, False, False
# scan des collisions
for TargetObj in Entities:
# contact R
if ((self.pos.top >= TargetObj.pos.top and self.pos.top <= TargetObj.pos.bottom) or
(self.pos.bottom >= TargetObj.pos.top and self.pos.bottom <= TargetObj.pos.bottom) or
(self.pos.top <= TargetObj.pos.top and self.pos.bottom >= TargetObj.pos.bottom)) and \
self.pos.right >= TargetObj.pos.left and self.pos.left < TargetObj.pos.left:
self.contactR = True
# contact L
elif ((self.pos.top >= TargetObj.pos.top and self.pos.top <= TargetObj.pos.bottom) or
(self.pos.bottom >= TargetObj.pos.top and self.pos.bottom <= TargetObj.pos.bottom) or
(self.pos.top <= TargetObj.pos.top and self.pos.bottom >= TargetObj.pos.bottom)) and \
self.pos.left <= TargetObj.pos.right and self.pos.right > TargetObj.pos.right:
self.contactL = True
# contact T
elif ((self.pos.right >= TargetObj.pos.left and self.pos.right <= TargetObj.pos.right) or
(self.pos.right >= TargetObj.pos.left and self.pos.left <= TargetObj.pos.right) or
(self.pos.right >= TargetObj.pos.left and self.pos.left <= TargetObj.pos.right)) and \
self.pos.top <= TargetObj.pos.bottom and self.pos.bottom > TargetObj.pos.bottom:
self.contactT = True
# contact B
elif ((self.pos.right >= TargetObj.pos.left and self.pos.right <= TargetObj.pos.right) or
(self.pos.left >= TargetObj.pos.left and self.pos.left <= TargetObj.pos.right) or
(self.pos.right >= TargetObj.pos.left and self.pos.left <= TargetObj.pos.right)) and \
self.pos.bottom >= TargetObj.pos.top and self.pos.top < TargetObj.pos.top:
self.contactB = True
# fonction d'application des deplacements
def moveDraw(self):
# conditions haut-bas
if self.goB and not self.contactB:
self.pos.top += self.speed
elif self.goT and not self.contactT:
self.pos.top -= self.speed
# conditions droite-gauche
if self.goR and not self.contactR:
self.pos.left += self.speed
elif self.goL and not self.contactL:
self.pos.left -= self.speed
class Platform():
def __init__(self, images, nb, spawnX, spawnY, screen):
self.IMG = (pygame.image.load(images[4]), pygame.image.load(images[5]))
self.nb = nb
self.pos = self.IMG[self.nb].get_rect()
self.pos.centerx = spawnX
self.pos.centery = spawnY
# fonctions principales
def MainLoop():
MainScreen = Screen(WIDTH, HEIGHT, TITLE)
P_Entity = Player(200, 100, IMAGES)
Plt1 = Platform(IMAGES, 0, WIDTH/2, HEIGHT/2, MainScreen)
MainScreen.EntityList.append(Plt1)
# boucle principale
while True:
MainScreen.draw(P_Entity)
pygame.display.update()
fpsClock.tick(FPS)
# lancement
MainLoop() |
Partager