Fix standing enemies collision

This commit is contained in:
Nikolya Andreychik
2021-04-06 01:25:25 -07:00
parent c1c439b771
commit c40653ed23
4 changed files with 26 additions and 6 deletions
+3
View File
@@ -19,3 +19,6 @@ class CollisionBox(pygame.sprite.Sprite):
def setY(self, y):
self.rect.y = y + self.offset[1]
def setX(self, x):
self.rect.x = x + self.offset[0]
+8 -4
View File
@@ -178,10 +178,11 @@ class Drakora():
if self.drawBoxes:
for player in self.players:
for collision in self.player.getCollisionBoxes():
for collision in player.getCollisionBoxes():
pygame.draw.rect(self.screen, (255, 0, 0), collision.rect, 1)
for enemy in self.enemies:
pygame.draw.rect(self.screen, (255, 0, 0), enemy.rect, 1)
for collision in enemy.getCollisionBoxes():
pygame.draw.rect(self.screen, (255, 0, 0), collision.rect, 1)
for floor in self.floors:
pygame.draw.rect(self.screen, (255, 0, 0), floor.rect, 1)
@@ -241,8 +242,11 @@ class Drakora():
return 300
def collideCheck(self):
if pygame.sprite.groupcollide(self.player.getCollisionBoxes(), self.enemies, None, None):
if not self.isGodmode: self.isGameOver = True
for enemy in self.enemies:
if pygame.sprite.groupcollide(self.player.getCollisionBoxes(), enemy.getCollisionBoxes(), None, None):
if not self.isGodmode:
self.isGameOver = True
break
if self.player.isOnFloor:
self.player.rect.y += 1
+9
View File
@@ -16,6 +16,12 @@ class Enemy(pygame.sprite.Sprite):
self.height = (mainGameClass.getScreenHeight()
- mainGameClass.getFloorHeight())
self.collisionBoxes = pygame.sprite.Group()
def getCollisionBoxes(self):
return self.collisionBoxes
def update(self):
if (self.rect.x < -self.rect.width):
@@ -23,3 +29,6 @@ class Enemy(pygame.sprite.Sprite):
self.thisGame.addScore(1)
self.rect.x -= self.speed
for i in self.collisionBoxes:
i.setX(self.rect.x)
+6 -2
View File
@@ -8,6 +8,7 @@ import random
import os
from Enemy import Enemy
from CollisionBox import CollisionBox
class StandingEnemy(Enemy):
@@ -73,9 +74,9 @@ class StandingEnemy(Enemy):
image.set_colorkey((255,0,255))
self.subtype = random.randint(1, len(StandingEnemy.images))
self.subtype = random.randint(0, len(StandingEnemy.images) - 1)
self.image = random.choice(StandingEnemy.images[self.subtype-1])
self.image = random.choice(StandingEnemy.images[self.subtype])
# if self.subtype == 1: self.image = pygame.Surface((32, 96))
# elif self.subtype == 2: self.image = pygame.Surface((32, 64))
@@ -91,4 +92,7 @@ class StandingEnemy(Enemy):
self.rect.center = (mainGameClass.getScreenWidth() + self.rect.width,
self.height)
collision = CollisionBox(9, 3, self.rect.w - 18, self.rect.h - 6, self.rect.center)
self.collisionBoxes.add(collision)
self.speed = self.thisGame.getGameSpeed()