Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import time
- import sys
- import random
- import math
- WIDTH, HEIGHT = 800, 600
- WHITE = (255, 255, 255)
- BLACK = (0,0,0)
- PINK = (255, 0, 120)
- BAR = 60
- #### SETTINGS ###
- PLAYER_SPEED = pygame.math.Vector2(0, 0)
- ROTATION_SPEED = 3
- LASER_SPEED = 4
- OFFSET = pygame.math.Vector2(WIDTH / 2, HEIGHT / 2)
- ASTEROIDS_COUNT = 30
- MAX_LIFE = 3
- #### SETTINGS ###
- pygame.init()
- canvas = pygame.display.set_mode((WIDTH,HEIGHT))
- font = pygame.font.SysFont("Calibri", 30)
- pygame.display.set_caption('Asterodis')
- clock = pygame.time.Clock()
- def quitGame():
- pygame.quit()
- sys.exit(0)
- def drawScore():
- global score
- life_text = font.render("{} / {} ".format(ship.life, MAX_LIFE), 1, WHITE)
- rect_life = life_text.get_rect()
- rect_life.x = BAR/2
- rect_life.centery = HEIGHT - BAR/2
- canvas.blit(life_text, (rect_life.x, rect_life.y))
- score_text = font.render("{}".format(score), 1, WHITE)
- rect_score = score_text.get_rect()
- rect_score.right = WIDTH - BAR/2
- rect_score.centery = HEIGHT - BAR/2
- canvas.blit(score_text, (rect_score.x, rect_score.y))
- class Ship():
- def __init__(self, r):
- self.life = MAX_LIFE
- self.r = r
- self.apex = [pygame.math.Vector2(-self.r, self.r), pygame.math.Vector2(self.r, self.r), pygame.math.Vector2(0, -self.r)]
- self.newApex = [0, 0, 0]
- self.center = OFFSET
- self.speed = PLAYER_SPEED
- def rotate(self, angle):
- self.apex[0] = self.apex[0].rotate(angle)
- self.apex[1] = self.apex[1].rotate(angle)
- self.apex[2] = self.apex[2].rotate(angle)
- self.newApex = [self.apex[0] + self.center, self.apex[1] + self.center, self.apex[2] + self.center]
- return(self.apex[2], self.newApex[2] - self.apex[2])
- def move(self, direction):
- self.theta = self.apex[2].angle_to(pygame.math.Vector2(self.r, 0)) * math.pi / 180
- self.speed += direction * 0.3 #dzielimy bo zapierdala za bardzo
- self.speed *= 0.95
- self.center += self.speed
- def edge(self):
- if self.center.x > WIDTH + self.r:
- self.center.x = -self.r
- elif self.center.x < -self.r :
- self.center.x = WIDTH + self.r
- if self.center.y > HEIGHT + self.r:
- self.center.y = -self.r
- elif self.center.y < -self.r :
- self.center.y = HEIGHT + self.r
- def show(self):
- self.rocket = pygame.draw.polygon(canvas, PINK, self.newApex, 2)
- def die(self, asteroids):
- for a in asteroids:
- if self.rocket.colliderect(a.asteroid):
- self.life -= 1
- self.apex = [pygame.math.Vector2(-self.r, self.r), pygame.math.Vector2(self.r, self.r), pygame.math.Vector2(0, -self.r)]
- self.newApex = [0, 0, 0]
- self.center = pygame.math.Vector2(WIDTH / 2, HEIGHT / 2)
- asteroids.clear()
- class Asteroid():
- def __init__(self, x = 0, y = 0, r = 0, collapsCnt = 0):
- if x == 0 and y == 0 and r == 0:
- self.x = random.randint(-1000, 1000)
- self.y = random.randint(-1000, 1000)
- self.r = random.randint(40, 70)
- else:
- self.x = x
- self.y = y
- self.r = r
- self.collapsCnt = collapsCnt
- self.asteroid = pygame.Rect(self.x, self.y, self.r, self.r)
- speedX = random.randint(1 , 2)
- speedY = random.randint(1 , 2)
- dirX = random.choice([-1, 1])
- dirY = random.choice([-1, 1])
- self.speedX = speedX * dirX
- self.speedY = speedY * dirY
- def update(self):
- self.asteroid.x += self.speedX
- self.asteroid.y += self.speedY
- if 1000 <= self.asteroid.right <= 1000 + self.speedX:
- self.speedX *= -1
- elif self.asteroid.left <= -1000:
- self.speedX *= -1
- if 1000 <= self.asteroid.bottom <= 1000 + self.speedY:
- self.speedY *= -1
- elif self.asteroid.top <= -1000:
- self.speedY *= -1
- def collapse(self):
- x1, y1 = self.asteroid.topleft
- x2, y2 = self.asteroid.bottomright
- r = self.r * 0.6
- self.collapsCnt += 1
- return x1, y1, x2, y2, r, self.collapsCnt
- def show(self):
- pygame.draw.rect(canvas, WHITE, self.asteroid, 1)
- class Laser():
- def __init__(self, start, offset):
- self.startVector = start
- self.offset = offset
- self.x = int(start.x)
- self.y = int(start.y)
- self.alpha = self.startVector.angle_to(pygame.math.Vector2(30, 0)) * math.pi / 180
- def create(self):
- self.las = pygame.Rect(self.x, self.y, 6, 6)
- def shot(self):
- self.x += LASER_SPEED * math.cos(self.alpha)
- self.y += LASER_SPEED * -math.sin(self.alpha)
- self.create()
- def show(self):
- self.las.x += self.offset.x
- self.las.y += self.offset.y
- pygame.draw.rect(canvas, WHITE, self.las)
- def delate(self, asteroids):
- laserDelate = False
- if self.las.left <= 0 or self.las.right >= WIDTH or self.las.top <= 0 or self.las.bottom >= HEIGHT:
- laserDelate = True
- for a in asteroids:
- if self.las.colliderect(a.asteroid):
- laserDelate = True
- return(laserDelate)
- asteroids = []
- lasers = []
- ship = Ship(30)
- startTime = time.time()
- def theGame():
- global score
- global startTime
- current = time.time()
- score = int(current - startTime)
- canvas.fill((BLACK))
- drawScore()
- if len(asteroids) == 0:
- for i in range(ASTEROIDS_COUNT):
- asteroids.append(Asteroid())
- angle = 0
- direction = pygame.math.Vector2(0, 0)
- keys = pygame.key.get_pressed()
- if keys[pygame.K_w]:
- direction = pygame.math.Vector2(math.cos(ship.theta), -math.sin(ship.theta))
- if keys[pygame.K_d]:
- angle = 1 * ROTATION_SPEED
- elif keys[pygame.K_a]:
- angle = -1 * ROTATION_SPEED
- start, offset = ship.rotate(angle)
- ship.move(direction)
- ship.edge()
- ship.show()
- ship.die(asteroids)
- for laser in lasers:
- laser.shot()
- laser.show()
- if laser.delate(asteroids):
- lasers.remove(laser)
- for i in range(len(asteroids)):
- shotAsteroid = laser.las.colliderect(asteroids[i].asteroid)
- if shotAsteroid == 1:
- if asteroids[i].collapsCnt < 1:
- print(asteroids[i].collapsCnt)
- x1, y1, x2, y2, r, collapsCnt = asteroids[i].collapse()
- asteroids.append(Asteroid(x1, y1, r, collapsCnt))
- asteroids.append(Asteroid(x2, y2, r, collapsCnt))
- del asteroids[i]
- break
- elif shotAsteroid >= 1:
- del asteroids[i]
- asteroids.append(Asteroid())
- break
- for i in range(len(asteroids)):
- asteroids[i].update()
- asteroids[i].show()
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- quitGame()
- if event.type == pygame.KEYDOWN:
- if event.key == 32:
- lasers.append(Laser(start, offset))
- while ship.life <= 0:
- canvas.fill((0,0,0,))
- line1 = 'The End'
- line2 = 'Your score: ' + str(score)
- line3 = 'press P to play again'
- line4 = 'press Q to quit'
- textSet1 = font.render(line1, True, (PINK))
- textSet2 = font.render(line2, True, (PINK))
- textSet3 = font.render(line3, True, (PINK))
- textSet4 = font.render(line4, True, (PINK))
- canvas.blit(textSet1,(20,20))
- canvas.blit(textSet2,(20,80))
- canvas.blit(textSet3,(20,140))
- canvas.blit(textSet4,(20,200))
- pygame.display.flip()
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- quitGame()
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_p:
- ship.life = MAX_LIFE
- score = 0
- startTime = time.time()
- return
- elif event.key == pygame.K_q:
- quitGame()
- pygame.display.flip()
- clock.tick(60)
- while True:
- theGame()
Advertisement
Add Comment
Please, Sign In to add comment