Hello there, sorry for a little bit late today because I am busy setting up my old website which is now ready for me to add in more articles into it, if you are interested in more programming articles then do visit this site because I am going to create a brand new laptop application project with python and if you are a python lover then go ahead and bookmark this site and visit it starting from tomorrow!. Gaming Directional, which is the site we both are in now will only concentrate on writing game code so if you want to read other programming articles besides the game one then do visit the above mentioned site instead. OK so much for that lets look at another of the new class I have created for the previous pygame project, which is the Overlap class.
We will call the Overlap class object on every game loop to determine the following:-
1) Does the player ship gets hit by the enemy missile? If so then we will remove that enemy missile.
2) Does the player ship hits one of the enemy ship? If so then we will remove that enemy ship.
3) Does the enemy ship gets hit by a player missile? If so then remove both of them.
We will not going to do anything on the player ship yet even if it gets hit by the enemy ship or missile because we will deal with this issue later on.
Here is the entire Overlap class.
from pygame.locals import * class Overlap(object): def __init__(self): pass # nothing here # is player and enemy, player missile, enemy missile overlap def iseOverlap(self, player, em, pm): self.player_rect = Rect(player.pos.x, player.pos.y, player.width, player.height) for i in range(len(em.enemy_list)): # is player collides with enemy self.em_rect = Rect(em.enemy_list[i].x, em.enemy_list[i].y, em.width, em.height) if (self.player_rect.colliderect(self.em_rect)): em.enemy_list[i].on = False for i in range(len(em.enemy_missile_manager.missile_list)): # is enemy missile hits player self.em_rect = Rect(em.enemy_missile_manager.missile_list[i].x, em.enemy_missile_manager.missile_list[i].y, em.enemy_missile_manager.width, em.enemy_missile_manager.height) if (self.player_rect.colliderect(self.em_rect)): em.enemy_missile_manager.missile_list[i].on = False for i in range(len(em.enemy_list)): # is player missile hits enemy self.em_rect = Rect(em.enemy_list[i].x, em.enemy_list[i].y, em.width, em.height) for j in range(len(pm.missile_list)): self.pm_rect = Rect(pm.missile_list[j].x, pm.missile_list[j].y, pm.width, pm.height) if (self.em_rect.colliderect(self.pm_rect)): em.enemy_list[i].on = False pm.missile_list[j].on = False
We also need to edit the Game Manager class like this.
from Player import Player from Background import Background from MissileManager import MissileManager from EnemyManager import EnemyManager from Overlap import Overlap import pygame class GameManager(object): def __init__(self, scene): self.scene = scene self.player = Player(self.scene) self.background = Background(self.scene) self.missile_manager = MissileManager(self.scene, self.player) self.enemy_manager = EnemyManager(self.scene, self.player) self.overlap_manager = Overlap() self.load_music() self.play_music() def load_music(self): pygame.mixer_music.load('Music/winternight.ogg') def play_music(self): pygame.mixer_music.play(-1) #play the music infinite time def set_player_x(self, _x): self.player.setX(_x) def set_player_y(self, _y): self.player.setY(_y) def set_missile_strike(self, strike): self. missile_manager.setStrike(True) def update(self): self.player.update() self.missile_manager.update() self.enemy_manager.update() self.isOverlap() # check for player, enemy, missiles overlap def isOverlap(self): self.overlap_manager.iseOverlap(self.player, self.enemy_manager, self. missile_manager) def draw(self): self.background.draw() self.player.draw() self.missile_manager.draw() self.enemy_manager.draw() pygame.display.flip()
And here is the outcome
Alright then, after this class we will continue to create the recycle class tomorrow which we will use to recycle and then reuse back the game object so we do not need to create a new one which will increase the burden of the computer’s processor.