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
|
#! /usr/bin/env python
# -*- coding: utf-8 -*-
#---Importation des modules
try:
import pygame as pg, math as mh, os
except ImportError as err:
print("Impossible de charger le module: ", err)
#---Variables
screenSize = (800,600)
title = "Zastro"
orientation = "center"
run = True
frame = 60
black = (0,0,0)
white = (255,255,255)
green = (0,255,0)
red = (255,0,0)
lLazer = []
#---Classes
class Ship():
def __init__(self, pos, rayon, color):
self.screen = pg.display.get_surface()
self.area = self.screen.get_rect()
self.color = color
self.rayon = rayon
if pos == "center":
self.x = self.area.w/2
self.y = self.area.h/2
else:
self.x = self.rayon
self.y = self.rayon
def display(self):
pg.draw.circle(self.screen, self.color, (int(self.x), int(self.y)), self.rayon)
self.rect = pg.Rect(self.x-self.rayon, self.y-self.rayon, self.rayon*2, self.rayon*2)
class Lazer():
def __init__(self, start, coord, color, life):
self.screen = pg.display.get_surface()
self.area = self.screen.get_rect()
self.color = color
self.start = start
self.coord = coord
self.life = life
self.calcul()
def calcul(self):
if self.start[0] > self.coord[0]:
x = self.start[0] - self.coord[0]
else:
x = self.coord[0] - self.start[0]
if self.start[1] > self.coord[0]:
y = self.start[1] - self.coord[1]
else:
y = self.coord[1] - self.start[1]
radian = mh.atan(x/y)
x2 = mh.tan(radian)*self.coord[1]
y2 = 0
self.end = (x2, y2)
def display(self):
pg.draw.line(self.screen, self.color, self.start, self.end, 1)
self.life -= 1
#---Initiation de la fenetre
os.environ['SDL_VIDEO_CENTERED'] = orientation
pg.init()
screen = pg.display.set_mode(screenSize)
pg.display.set_caption(title)
pg.key.set_repeat(100, 30)
#---Initiation des objets
ship = Ship("center", 10, white)
#---Boucle principal
while run:
pg.time.Clock().tick(frame)
mousePos = pg.mouse.get_pos()
for ev in pg.event.get():
if ev.type == pg.QUIT:
run = False
if ev.type == pg.KEYDOWN:
if ev.key == pg.K_ESCAPE:
run = False
if ev.type == pg.MOUSEBUTTONDOWN and ev.button == 1:
lLazer.append(Lazer((ship.x, ship.y), mousePos, red, frame/3))
#---Movement
#---Collision
#---Score
#---Affichage
screen.fill(black)
for element in lLazer:
element.display()
if element.life < 0:
lLazer.remove(element)
ship.display()
pg.display.flip()
#---Quitter
pg.quit()
os._exit(0) |
Partager