Browse Source

Improved enemy`s collision

master
Unbewohnte 4 years ago
parent
commit
85c2ae1a0b
  1. 4
      Main.py
  2. 14
      beings.py
  3. 2
      bullets.py

4
Main.py

@ -70,8 +70,8 @@ def play():
enemy.draw(window,start_color,enemy_image) enemy.draw(window,start_color,enemy_image)
enemy.move('right') enemy.move('right')
if enemy.enemy_x == windowX: #That returning thingy if enemy.enemy_x >= windowX-10: #That returning thingy
enemy.enemy_x = 1 enemy.enemy_x = 5
enemy.collision() enemy.collision()
if enemy.out_of_area(): if enemy.out_of_area():

14
beings.py

@ -111,6 +111,7 @@ class Enemy:
def draw(self,window,en_color,en_image): def draw(self,window,en_color,en_image):
pygame.draw.rect(window,en_color,(self.enemy_x, self.enemy_y, self.en_width, self.en_height)) pygame.draw.rect(window,en_color,(self.enemy_x, self.enemy_y, self.en_width, self.en_height))
window.blit(en_image,(self.enemy_x, self.enemy_y)) window.blit(en_image,(self.enemy_x, self.enemy_y))
self.enemy_rect = pygame.Rect(self.enemy_x, self.enemy_y, self.en_width, self.en_height) #Rect object for collision
def move(self,side): def move(self,side):
if str(side) == "right": if str(side) == "right":
self.enemy_x += self.vel self.enemy_x += self.vel
@ -126,19 +127,24 @@ class Enemy:
if int(len(enemy_bul_on_screen)) > 0: if int(len(enemy_bul_on_screen)) > 0:
for bullet in enemy_bul_on_screen: for bullet in enemy_bul_on_screen:
bullet.draw(window,start_color,bul_image) bullet.draw(window,start_color,bul_image)
bullet.moveb() bullet.movedwn()
if bullet.bullet_y >= windowY +20: if bullet.bullet_y >= windowY +20:
enemy_bul_on_screen.remove(bullet) enemy_bul_on_screen.remove(bullet)
def out_of_area(self): def out_of_area(self):
if self.enemy_x > windowX or self.enemy_x < 0 or self.enemy_y > windowY or self.enemy_y < 0: if self.enemy_x > windowX+1 or self.enemy_x < -1 or self.enemy_y > windowY+1 or self.enemy_y < -1:
return True return True
else: else:
return False return False
def collision(self): def collision(self):
# for bullet in bullets_on_screen:
# # if bullet.bullet_x + bullet.bullet_width/2 >= enemy.enemy_x and bullet.bullet_x + bullet.bullet_width/2 <= enemy.enemy_x + enemy.en_width and bullet.bullet_y <= enemy.enemy_y:
# if bullet.bullet_x + bullet.bullet_width/2 >= self.enemy_x and bullet.bullet_x + bullet.bullet_width/2 <= self.enemy_x + self.en_width and bullet.bullet_y <= self.enemy_y:
# self.enemy_x += randint(-60,60)
# self.enemy_y += randint(-60,60) # Old hardcoded version
for bullet in bullets_on_screen: for bullet in bullets_on_screen:
# if bullet.bullet_x + bullet.bullet_width/2 >= enemy.enemy_x and bullet.bullet_x + bullet.bullet_width/2 <= enemy.enemy_x + enemy.en_width and bullet.bullet_y <= enemy.enemy_y: if self.enemy_rect.colliderect(bullet):
if bullet.bullet_x + bullet.bullet_width/2 >= self.enemy_x and bullet.bullet_x + bullet.bullet_width/2 <= self.enemy_x + self.en_width and bullet.bullet_y <= self.enemy_y:
self.enemy_x += randint(-60,60) self.enemy_x += randint(-60,60)
self.enemy_y += randint(-60,60) self.enemy_y += randint(-60,60)

2
bullets.py

@ -17,5 +17,5 @@ class Bullet:
window.blit(bullet_image,(self.bullet_x, self.bullet_y)) window.blit(bullet_image,(self.bullet_x, self.bullet_y))
def move(self): def move(self):
self.bullet_y -= self.bullet_vel self.bullet_y -= self.bullet_vel
def moveb(self): def movedwn(self):
self.bullet_y += self.bullet_vel self.bullet_y += self.bullet_vel

Loading…
Cancel
Save