Using a list for lots of enemies
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 screenscreen = pygame.display.set_mode( (800, 600) )# 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" ) )playerX = 368playerY = 480playerX_change = 0 # how far to move the playerscore = 0# Enemy Classclass Enemy:def __init__(self, x, y):self.Img = pygame.image.load( os.path.join( myPath, "images\\enemy.png" ) )self.X = xself.Y = yself.X_change = 2 # how far to move the enemyself.Y_change = 40self.width = 64self.height = 64#Create a list of enemies from the Enemy Classenemies = [ 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 False# 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: Tāite, 3 Tīhema 2020, 3:03 PM