COllision detection with the lsit
Completion requirements
import pygame # may need to install: pip install pygameimport os # needed to get paths to imagesimport math#get path to our gamemyPath = os.path.dirname( os.path.realpath( __file__ ) )#Set up the screenscreenW = 800screenH = 600screen = pygame.display.set_mode( (screenW, screenH) )# Titlepygame.display.set_caption("Space Invaders")#backgroundbackground = pygame.image.load( os.path.join( myPath, "images\\background.png") )# playerplayerImg = pygame.image.load( os.path.join( myPath, "images\\player.png" ) )playerW = 64playerH = 64playerX = screenW / 2 - playerW / 2 # half screen width - half player widthplayerY = 480playerX_change = 0 # how far to move the playerplayer_state = "alive"num_enemies = 20 # how many enemies to spawnwave = 1score = 0# Enemy Classclass Enemy:def __init__(self, x, y):self.Img = pygame.image.load( os.path.join( myPath, "images\\enemy.png" ) )self.width = 64self.height = 64self.X = xself.Y = yself.X_change = 2 # how far to move the enemyself.Y_change = self.height#Create a list of enemies from the Enemy Class# enemies = [ Enemy(10, 30), Enemy(100, 30), Enemy(200, 30),# Enemy(10, 100), Enemy(100, 100), Enemy(200, 100)]# BulletbulletImg = pygame.image.load( os.path.join( myPath, "images\\bullet.png" ) )bulletX = 0bulletY = 0bulletY_change = -3bullet_state = "ready"def draw_sprite( sprite, x, y ):x = int(x)y = int(y)screen.blit( sprite, ( x, y ) ) # blit basically means drawdef fire_bullet( x, y ):global bullet_state, bulletX, bulletYbullet_state = "fire"bulletX = xbulletY = ydef isCollision(x1, y1, x2, y2):hypotenuse = math.sqrt( math.pow(x1 - x2, 2 ) + math.pow(y1 - y2, 2) ) # pythagoras theorumif hypotenuse < 64 :return Trueelse:return Falsedef init( enemies, amount ):for e in range( amount ): # loop for the number of times providedenemies.append( Enemy(0,0) ) # Add a new enemy to the list at 0,0# current enemy * width * a bit for a gap - max 10 enemies per rowenemies[e].X = e % 10 * enemies[e].width * 1.1enemies[e].Y = math.floor( e / 10 ) * ( enemies[e].height + enemies[e].Y_change )#return enemies# create enemies using the init methodenemies = []init( enemies, num_enemies )# game looprunning = Truewhile running:# need to clear the screen and set the background before drawing any spritesscreen.fill((0,0,0))screen.blit( background, (0,0) )for event in pygame.event.get(): # get all python events and loop through themif event.type == pygame.QUIT:running = Falseif event.type == pygame.KEYDOWN:if event.key == pygame.K_LEFT:playerX_change = -2if event.key == pygame.K_RIGHT:playerX_change = 2if event.key == pygame.K_SPACE:if bullet_state == "ready":fire_bullet( playerX + 64 / 2 - 32 / 2, playerY )if event.type == pygame.KEYUP:if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:playerX_change = 0playerX += playerX_changeif playerX <= 0: # if player tries to move too far leftplayerX = 0if playerX >= 800 - 64: # if the player moves too far rightplayerX = 800 - 64# Loop though all the enemy objects in the listfor e in enemies:e.X += e.X_change # Move the enemyif e.X <= 0: # if enemy tries to move too far lefte.X_change = e.X_change * -1e.Y += e.Y_changeif e.X >= 800 - e.width: # if the enemy moves too far righte.X_change = e.X_change * -1e.Y += e.Y_changedraw_sprite(e.Img, e.X, e.Y) # draw the enemy#test collisionscollision = isCollision(e.X + e.width / 2, e.Y + e.width / 2, bulletX + 32 / 2, bulletY + 32 /2 )if collision and bullet_state == "fire":enemies.remove( e )bullet_state = "ready"score += 1print( score )#bullet movementif bullet_state == "fire":bulletY += bulletY_changeif bulletY <= 0:bullet_state = "ready"draw_sprite(playerImg, playerX, playerY) # draw the player# if the players bullet is firing, draw itif bullet_state == "fire":draw_sprite(bulletImg, bulletX, bulletY)pygame.display.update() # needed to apply the change
Last modified: Thursday, 10 December 2020, 3:32 PM