Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 32 additions & 18 deletions asteroids/main.py → asteroids/asteroids.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

pygame.init()

# screen dimensions
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 800

Expand All @@ -16,10 +17,11 @@
asteroid2 = pygame.image.load('asteroidpics/asteroid2.png')
asteroid3 = pygame.image.load('asteroidpics/asteroid3.png')

# game window set up
pygame.display.set_caption('Asteroids')
window = pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT))


# game state vars
run = True
clock = pygame.time.Clock()
game_over = False
Expand All @@ -28,6 +30,7 @@
rapid_fire = False
rapid_fire_start = -1

# Player class + attributes and methods
class Player(object):
def __init__(self):
self.img = playerShip
Expand All @@ -36,16 +39,18 @@ def __init__(self):
self.x = SCREEN_WIDTH//2
self.y = SCREEN_HEIGHT//2
self.angle = 0
# rotate the player ship based on the curr angle
self.rotated_surface = pygame.transform.rotate(self.img, self.angle)
self.rotated_rect = self.rotated_surface.get_rect()
self.rotated_rect.center = (self.x,self.y)
self.cosine = math.cos(math.radians(self.angle + 90))
self.sine = math.sin(math.radians(self.angle + 90))
# calculates the front point of the ship for shooting the bullets
self.head = (self.x + self.cosine * self.width//2, self.y - self.sine * self.height//2)

# draw the rotated player ship on the window
def draw(self,window):
window.blit(self.rotated_surface,self.rotated_rect)

# turn the ship left by increasing the angle
def turn_left(self):
self.angle +=5
self.rotated_surface = pygame.transform.rotate(self.img, self.angle)
Expand All @@ -54,7 +59,7 @@ def turn_left(self):
self.cosine = math.cos(math.radians(self.angle + 90))
self.sine = math.sin(math.radians(self.angle +90))
self.head = (self.x + self.cosine * self.width//2, self.y - self.sine * self.height/2)

# turn the ship right by decreasing the angle
def turn_right(self):
self.angle -=5
self.rotated_surface = pygame.transform.rotate(self.img, self.angle)
Expand All @@ -63,7 +68,8 @@ def turn_right(self):
self.cosine = math.cos(math.radians(self.angle + 90))
self.sine = math.sin(math.radians(self.angle +90))
self.head = (self.x + self.cosine * self.width//2, self.y - self.sine * self.height/2)


# move the ship forward in the direction it is facing
def move_forward(self):
self.x += self.cosine * 6
self.y -= self.sine * 6
Expand All @@ -74,6 +80,7 @@ def move_forward(self):
self.sine = math.sin(math.radians(self.angle + 90))
self.head = (self.x + self.cosine * self.width // 2, self.y - self.sine * self.height // 2)

# update the location of the ship when it goes off the screen
def update_location(self):
if self.x > SCREEN_WIDTH+ 50:
self.x = 0
Expand All @@ -84,7 +91,7 @@ def update_location(self):
elif self.y > SCREEN_HEIGHT + 50:
self.y = 0


# Bullet class + attributes and methods
class Bullet(object):
def __init__(self):
self.point = player.head
Expand All @@ -106,6 +113,7 @@ def check_off_screen(self):
if self.x < -50 or self.x > SCREEN_WIDTH or self.y > SCREEN_HEIGHT or self.y < -50:
return True

# Asteroid class + attributes and methods
class Asteroid(object):
def __init__(self, rank):
self.rank = rank
Expand All @@ -132,7 +140,7 @@ def __init__(self, rank):

def draw(self, window):
window.blit(self.image, (self.x, self.y))

# Star class + attributes and methods
class Star(object):
def __init__(self):
self.img = star
Expand All @@ -153,6 +161,7 @@ def __init__(self):
def draw(self, window):
window.blit(self.img, (self.x,self.y))

# Alien class + attributes and methods
class Alien(object):
def __init__(self):
self.img = alien
Expand All @@ -174,6 +183,8 @@ def __init__(self):
self.yv = self.ydir * 2
def draw(self, window):
window.blit(self.img, (self.x, self.y))

# Alien Bullets class + attributes and methods
class AlienBullet(object):
def __init__(self, x ,y):
self.x = x
Expand All @@ -189,12 +200,7 @@ def draw(self,window):
pygame.draw.rect(window, (255,255,255), [self.x,self.y,self.w,self.h])








# updates game window with the current game state
def redraw_window():
window.blit(bg,(0,0))
font = pygame.font.SysFont('arial',20)
Expand Down Expand Up @@ -222,6 +228,7 @@ def redraw_window():
window.blit(livesText,(25,25))
pygame.display.update()

# game entities
player = Player()
player_bullets = []
asteroids = []
Expand All @@ -234,6 +241,7 @@ def redraw_window():
while run:
clock.tick(60)
count +=1

if not game_over:
if count%50 == 0:
ran = random.choice([1,1,1,2,2,3])
Expand All @@ -254,6 +262,7 @@ def redraw_window():
if (b.y >= a.y and b.y <= a.y + a.h) or b.y + b.h >= a.y and b.y + b.h <= a.y + a.h:
aliens.pop(i)
score += 50
break
for i, b in enumerate(alien_bullets):
b.x += b.xv
b.y += b.yv
Expand Down Expand Up @@ -289,6 +298,7 @@ def redraw_window():
for b in player_bullets:
if (b.x >= a.x and b.x <= a.x + a.w) or b.x + b.w >= a.x and b.x + b.w <= a.x + a.w:
if (b.y >= a.y and b.y <= a.y + a.h) or b.y + b.h >= a.y and b.y + b.h <= a.y + a.h:
# breaks asteroids and adds to score based on asteroids type
if a.rank ==3:
score += 10
na1 = Asteroid(2)
Expand All @@ -314,6 +324,7 @@ def redraw_window():
asteroids.pop(asteroids.index(a))
player_bullets.pop(player_bullets.index(b))
break

for s in stars:
s.x += s.xv
s.y += s.yv
Expand All @@ -336,7 +347,7 @@ def redraw_window():
rapid_fire = False
rapid_fire_start = -1


# keys for movement/rotation
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
player.turn_left()
Expand All @@ -348,9 +359,11 @@ def redraw_window():
if rapid_fire:
player_bullets.append(Bullet())


for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
run = False

if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
if not game_over:
Expand All @@ -363,7 +376,7 @@ def redraw_window():
asteroids.clear()
alien_bullets.clear()
stars.clear()

# pause key
elif event.key == pygame.K_p:
paused = True
while paused:
Expand All @@ -376,12 +389,13 @@ def redraw_window():
paused = False
elif pause_event.key == pygame.K_q:
pygame.quit()
exit() #
exit()

pygame.time.wait(100)
# quit key
elif event.key == pygame.K_q:
pygame.quit()
exit()

# redraws the window with the game's current state
redraw_window()
pygame.quit()