Browse Source

Improved adn fixed myself. Enemy can now shoot.

master
Unbewohnte 4 years ago
parent
commit
55265dd76b
  1. 9
      Main.py
  2. 45
      beings.py
  3. 4
      bullets.py
  4. 44
      enemy.py

9
Main.py

@ -4,8 +4,9 @@ import sys
import random import random
import time import time
from bullets import Bullet from bullets import Bullet
from player import Player #from player import Player
from enemy import Enemy #from enemy import Enemy
from beings import *
from surfaces import Surface from surfaces import Surface
######## Set up things that will not change ######## Set up things that will not change
@ -49,12 +50,12 @@ def play():
player.shoot(window,start_color,bullet_image) player.shoot(window,start_color,bullet_image)
player.draw(window,start_color,player_image) player.draw(window,start_color,player_image)
player.update(window,start_color,bullet_image) player.update(window,start_color,bullet_image)
player.collision()
enemy.enemy_shoot(window,start_color,enemy_bul_img) enemy.enemy_shoot(window,start_color,enemy_bul_img)
enemy.draw(window,start_color,enemy_image) enemy.draw(window,start_color,enemy_image)
enemy.update() enemy.update()
enemy.collision(enemy) enemy.collision()
if player.out_of_area(): if player.out_of_area():
print(player.x, player.y) print(player.x, player.y)

45
player.py → beings.py

@ -9,6 +9,7 @@ import sys
windowX = 640 windowX = 640
windowY = 640 windowY = 640
bullets_on_screen = [] bullets_on_screen = []
enemy_bul_on_screen = []
class Player: class Player:
def __init__(self): def __init__(self):
@ -90,3 +91,47 @@ class Player:
return True return True
else: else:
return False return False
def collision(self):
for bullet in enemy_bul_on_screen:
if bullet.bullet_x + bullet.bullet_width/2 >= self.x and bullet.bullet_x + bullet.bullet_width/2 <= self.x + self.width and bullet.bullet_y <= self.y:
self.x += randint(-50,50)
self.y += randint(-50,50)
class Enemy:
def __init__(self):
self.enemy_x = 100
self.enemy_y = 100
self.en_width = 32
self.en_height = 64
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))
window.blit(en_image,(self.enemy_x, self.enemy_y))
def update(self):
self.enemy_x += 3
if self.enemy_x >= 600:
self.enemy_x = 1
def enemy_shoot(self,window,start_color,bul_image): #Have NO IDEA how it`ll work
new_bullet = Bullet(self.enemy_x + self.en_width/2, self.enemy_y + 10)
enemy_bul_on_screen.append(new_bullet)
if int(len(enemy_bul_on_screen)) > 0:
for bullet in enemy_bul_on_screen:
bullet.draw(window,start_color,bul_image)
bullet.moveb()
if bullet.bullet_y >= windowY +20:
enemy_bul_on_screen.remove(bullet)
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:
return True
else:
return False
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(-50,50)
self.enemy_y += randint(-50,50)

4
bullets.py

@ -1,6 +1,10 @@
import pygame import pygame
import random import random
bullets_on_screen = []
enemy_bul_on_screen = []
class Bullet: class Bullet:
def __init__(self, bullet_x, bullet_y): def __init__(self, bullet_x, bullet_y):
self.bullet_x = bullet_x + random.randint(-9,9) # -8 -- 8 self.bullet_x = bullet_x + random.randint(-9,9) # -8 -- 8

44
enemy.py

@ -1,44 +0,0 @@
import pygame
from player import *
from bullets import Bullet
from random import randint
windowX = 640
windowY = 640
enemy_bul_on_screen = []
class Enemy:
def __init__(self):
self.enemy_x = 100
self.enemy_y = 100
self.en_width = 32
self.en_height = 64
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))
window.blit(en_image,(self.enemy_x, self.enemy_y))
def update(self):
self.enemy_x += 3
if self.enemy_x >= 600:
self.enemy_x = 1
def enemy_shoot(self,window,start_color,bul_image): #Have NO IDEA how it`ll work
new_bullet = Bullet(self.enemy_x + self.en_width/2, self.enemy_y + 10)
enemy_bul_on_screen.append(new_bullet)
if int(len(enemy_bul_on_screen)) > 0:
for bullet in enemy_bul_on_screen:
bullet.draw(window,start_color,bul_image)
bullet.moveb()
if bullet.bullet_y >= windowY +20:
enemy_bul_on_screen.remove(bullet)
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:
return True
else:
return False
def collision(self,enemy):
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:
enemy.enemy_x += randint(-50,50)
enemy.enemy_y += randint(-50,50)
Loading…
Cancel
Save