Skip to content
Permalink
Browse files
player/enemy collision
Game over now appears and locks players movements when coliding with an enemy.
Player to restart by pressing SPACE
  • Loading branch information
dacostag committed Apr 27, 2020
1 parent 4c076a9 commit 3530d7abd6fedabefad2a75b80af34debd081c81
Showing 1 changed file with 19 additions and 10 deletions.
29 main.py
@@ -60,7 +60,7 @@ textX, textY = 10, 10

# Game Over text
over_font = pygame.font.Font('8-BIT WONDER.ttf', 36)

game_over = False #game_over remains False until player loses the game

def show_score(x, y):
score = font.render("Score " + str(score_value), True, (245, 194, 66))
@@ -90,6 +90,8 @@ def isCollision(x1, y1, x2, y2):
distance = math.sqrt((math.pow(x1 - x2, 2)) + (math.pow(y1 - y2, 2)))
if distance < 27:
return True
#if x1 == playerX

return False


@@ -104,7 +106,8 @@ while running: # leaves window opened
if event.type == pygame.QUIT:
running = False

if event.type == pygame.KEYDOWN: # when key is being pressed
# when key is being pressed, below are all the game keys
if event.type == pygame.KEYDOWN and game_over is False:
if event.key == pygame.K_LEFT:
playerX_change = -1
if event.key == pygame.K_RIGHT:
@@ -117,7 +120,7 @@ while running: # leaves window opened
if spray_state is "ready":
# spray_sound = mixer.Sound('quickfart.wav') #later change .wav to the spray sound
# spray_sound.play()
# get's current x coordinate of the player
# get's current x, y coordinate of the player
sprayX = playerX
sprayY = playerY
player_attack(sprayX, sprayY)
@@ -128,6 +131,10 @@ while running: # leaves window opened
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
playerY_change = 0

if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
game_over = False

playerX += playerX_change
playerY += playerY_change

@@ -145,11 +152,9 @@ while running: # leaves window opened
for i in range(num_of_enemies):

# Game over
if enemyX[i] < 50:
for j in range(num_of_enemies):
enemyY[j] = height + 40
game_over_text()
break
player_collision = isCollision(playerX, playerY, enemyX[i], enemyY[i])
if player_collision:
game_over = True

enemyX[i] += enemyX_change[i]
# set boundaries to define enemy movement
@@ -159,8 +164,8 @@ while running: # leaves window opened
enemyX_change[i] = -0.7

# Collision between attack and enemy
collision = isCollision(enemyX[i], enemyY[i], sprayX, sprayY)
if collision:
attack_collision = isCollision(enemyX[i], enemyY[i], sprayX, sprayY)
if attack_collision:
sprayY = height
spray_state = "ready"
score_value += 1
@@ -180,6 +185,10 @@ while running: # leaves window opened
sprayX += sprayX_change
player_attack(sprayX, sprayY)

# When player loses the game:
if game_over:
game_over_text()

player(playerX, playerY)
show_score(textX, textY)
# note: things appear on top of the things above it in the code

0 comments on commit 3530d7a

Please sign in to comment.