Skip to content
Snippets Groups Projects
Commit 1a4dd51f authored by Erkki Kaila's avatar Erkki Kaila
Browse files

initial commit

parents
Branches
No related tags found
No related merge requests found
import pygame, random
def init():
pygame.init()
scr = pygame.display.set_mode((640,480))
scr.fill((0,0,0))
return scr
class StarShip:
def __init__(self, x, y, color: tuple):
self.x = x
self.y = y
self.color = color
# static so far
self.size = 1
self.going_up = False
self.going_down = False
def draw(self, screen):
color2 = tuple([x * 1.3 for x in self.color])
size = self.size
x1 = self.x
y1 = self.y
x2 = int(x1 + 20 * size)
y2 = int(y1 + 10 * size)
x3 = int(x2 + 40 * size)
y3 = int(y2 + 20 * size)
y4 = int(y3 + 20 * size)
y5 = int(y4 + 10 * size)
if self.going_up:
y3 -= 5
if self.going_down:
y3 += 5
pygame.draw.polygon(screen, color2, ((x1,y2), (x2, y1), (x3, y3), (x1, y3), (x1, y2)))
pygame.draw.polygon(screen, self.color, ((x1,y3), (x3, y3), (x2, y5), (x1, y4), (x1, y3)))
def up(self):
self.y -= 1
def down(self):
self.y += 1
def left(self) :
self.x -= 1
def right(self):
self.x += 1
class Bullet:
def __init__(self, x, y):
self.x = x
self.y = y
self.is_out = False
def draw(self, screen):
x,y = self.x, self.y
pygame.draw.line(screen, (255,255,255), (x,y), (x+20, y))
def move(self):
self.x += 2
if self.x > 640:
is_out = True
class Star:
def __init__(self, x, y, level: int):
self.x = x
self.y = y
basecol = int((10 - level) / 10 * 255)
self.rgb = (basecol, basecol, basecol)
self.tick = 0
self.level = level
def draw(self, screen):
pygame.draw.circle(screen, self.rgb, (self.x, self.y), 1)
def move(self):
self.tick += 1
if self.tick == self.level:
self.tick = 0
self.x -= 1
if self.x == 0:
self.x = 640
self.y = random.randint(5,475)
class Pyramid:
def __init__(self, y, rgb: tuple):
self.x = 640
self.y = y
self.rgb = rgb
self.phase = 0
self.ticker = 0
self.color1 = self.rgb
self.color2 = tuple([x * 1.7 for x in self.color1])
self.is_out = False
self.exploding = False
self.exp_phase = 1
def draw(self, screen):
if self.exp_phase == 1:
top_x = self.x + 20
top_y = self.y
left_x = self.x
right_x = self.x + 40
bottom_y = self.y + 35
if self.phase < 20:
mid_bottom = self.y + 35 + (self.phase // 4)
else:
mid_bottom = self.y + 35 + ((40 - self.phase) // 4)
mid_x = self.x + self.phase
pygame.draw.polygon(screen, self.color1, ((top_x, top_y), (right_x, bottom_y), (mid_x, mid_bottom), (top_x, top_y)))
pygame.draw.polygon(screen, self.color2, ((top_x, top_y), (left_x, bottom_y), (mid_x, mid_bottom), (top_x, top_y)))
self.ticker += 1
if self.exploding:
pygame.draw.circle(screen, (255,255,0), (self.x + 20, self.y + 20), self.exp_radius)
if self.ticker == 3:
self.ticker = 0
self.phase += 1
if self.phase == 40:
self.color1, self.color2 = self.color2, self.color1
self.phase = 0
if self.exploding:
self.exp_radius += 1
if self.exp_radius == 40:
self.exp_radius = 0
self.exp_phase = 2
else:
pygame.draw.circle(screen, (255,255,0), (self.x + 20, self.y + 20), 40)
pygame.draw.circle(screen, (0,0,0), (self.x + 20, self.y + 20), self.exp_radius)
self.ticker += 1
if self.ticker == 3:
self.ticker = 0
self.exp_radius += 1
if self.exp_radius == 40:
self.is_out = True
def move(self):
self.x -= 1
if self.x == -20:
self.is_out = True
def start_explode(self):
self.exploding = True
self.exp_radius = 0
class Tile:
def __init__(self, color: int):
self.color = color
self.highlight = tuple([c * 1.3 for c in self.color])
self.shadow = tuple([c * 0.7 for c in self.color])
self.drop = tuple([c * 0.9 for c in self.color])
# static size
self.size = 31
def draw(self, x: int, y: int, screen, drop_top = False, drop_left = False):
size = self.size
pygame.draw.rect(screen, self.color, pygame.Rect(x, y, self.size, self.size))
if drop_top:
pygame.draw.rect(screen, self.drop, pygame.Rect(x, y, self.size, self.size // 2))
if drop_left:
pygame.draw.rect(screen, self.drop, pygame.Rect(x, y, self.size // 2, self.size))
pygame.draw.line(screen, self.highlight, (x,y), (x+size, y))
pygame.draw.line(screen, self.highlight, (x,y), (x, y+size))
pygame.draw.line(screen, self.shadow, (x + size,y + 1), (x+size, y+size))
pygame.draw.line(screen, self.shadow, (x,y + size), (x+size, y+size))
class Floor:
def __init__(self, color1: tuple, color2: tuple, y = 448):
self.color1 = color1
self.color2 = color2
self.offset = 0
self.t1 = Tile(color1)
self.t2 = Tile(color2)
self.y = y
def draw(self, screen):
for i in range(0,22):
if i % 2 == 0:
self.t1.draw(i * 32 - self.offset, self.y, screen)
else:
self.t2.draw(i * 32 - self.offset, self.y, screen)
def move(self):
self.offset += 1
if self.offset == 64:
self.offset = 0
def check_bullet_hits(bullets: list, enemies: list):
global score
for bullet in bullets:
for enemy in enemies:
if bullet.x > enemy.x and bullet.x < enemy.x + 40 and bullet.y > enemy.y and bullet.y < enemy.y + 40 and not enemy.exploding:
enemy.start_explode()
bullet.is_out = True
score += 100
def check_enemy_hit(starship, enemies: list):
right = starship.x + 60
bottom = starship.y + 80
for enemy in enemies:
if right > enemy.x and right < enemy.x + 100 and bottom > enemy.y and bottom < enemy.y + 120:
return True
scr = init()
timer = pygame.time.Clock()
ball_img = pygame.image.load("pallo.png")
high = 0
lives = 3
score = 0
while True:
stars = []
ri = random.randint
rc = random.choice
direct = (-1,1)
for i in range(1, 9):
for j in range(20):
stars.append(Star(ri(0,640), ri(5,475), i))
bullets = []
enemies = []
font = pygame.font.SysFont("Arial", 24)
textx = 0
texta = 1
ship = StarShip(250, 200, (128, 128, 0))
left = False
right = False
up = False
down = False
floor = Floor((0,128,0), (0,64,0))
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
elif event.type == pygame.KEYDOWN:
value = True
if event.key == pygame.K_LEFT:
left = value
elif event.key == pygame.K_RIGHT:
right = value
elif event.key == pygame.K_UP:
up = value
ship.going_up = True
elif event.key == pygame.K_DOWN:
down = value
ship.going_down = True
elif event.key == pygame.K_SPACE:
bullets.append(Bullet(ship.x + 60, ship.y + 30))
elif event.type == pygame.KEYUP:
value = False
if event.key == pygame.K_LEFT:
left = value
elif event.key == pygame.K_RIGHT:
right = value
elif event.key == pygame.K_UP:
up = value
ship.going_up = False
elif event.key == pygame.K_DOWN:
down = value
ship.going_down = False
if left:
ship.left()
if right:
ship.right()
if up:
ship.up()
if down:
ship.down()
if random.randint(1,150) == 1:
enemies.append(Pyramid(ri(5,420), (ri(0,128), ri(0,128), ri(0,128))))
scr.fill((0,0,0))
for star in stars:
star.draw(scr)
floor.draw(scr)
ship.draw(scr)
index = 0
while index < len(bullets):
bullets[index].draw(scr)
bullets[index].move()
if bullets[index].is_out:
bullets.pop(index)
else:
index += 1
index = 0
while index < len(enemies):
enemies[index].draw(scr)
enemies[index].move()
if enemies[index].is_out:
enemies.pop(index)
else:
index += 1
check_bullet_hits(bullets, enemies)
if check_enemy_hit(ship, enemies):
break
text1 = font.render("Score: " + (str(score).zfill(7)), True, (255,255,255))
text2 = font.render("Score: " + (str(score).zfill(7)), True, (128,128,128))
scr.blit(text2, (5,5))
scr.blit(text1, (2,2))
text1 = font.render("High: " + (str(high).zfill(7)), True, (255,255,255))
text2 = font.render("High: " + (str(high).zfill(7)), True, (128,128,128))
scr.blit(text2, (250,5))
scr.blit(text1, (247,2))
for live in range(lives):
sh = StarShip(450 + live*30, 10, (128,128,0))
sh.size = 0.3
sh.draw(scr)
if score > high:
high = score
pygame.display.flip()
for star in stars:
star.move()
floor.move()
#textx += texta
#if textx == 0 or textx == 335:
# texta = -texta
timer.tick(200)
for i in range(222, 1, -1):
scr.fill((i,i,i))
pygame.display.flip()
timer.tick(300)
lives -= 1
if lives == 0:
for i in range(0, 255):
text1 = font.render("Game Over ", True, (i,i,i))
text_rect = text1.get_rect(center=(320, 240))
scr.blit(text1, (text_rect))
pygame.display.flip()
timer.tick(200)
while True:
done = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
elif event.type == pygame.KEYDOWN:
done = True
if done:
break
lives = 3
score = 0
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment