Browse Source

Trying to make a particles;

Bullets are Pygame.Rect objects now
master
Unbewohnte 4 years ago
parent
commit
762835c6df
  1. 12
      Main.py
  2. 36
      beings.py
  3. 15
      bullets.py
  4. 24
      particles.py
  5. 4
      surfaces.py

12
Main.py

@ -4,6 +4,7 @@ import sys
from bullets import Bullet
from beings import *
from surfaces import Surface
from particles import *
######## Set up things that will not change
pygame.init()
pygame.display.set_caption('Healthless')
@ -25,8 +26,6 @@ def play():
death_timer = 70
text = font.render(str(death_timer), True,(0,0,0)) #Declaring just for rect
text_rect = text.get_rect(center = (windowX//2,windowY//2 - 100))
# lower_surface = Surface('killsurf')
# main_surface = Surface('default')
while True:
for event in pygame.event.get():
@ -37,14 +36,12 @@ def play():
window.fill((41, 64, 59))
# lower_surface.place(window,0,0)
# main_surface.place(window,145,145)
player.teleportation()
player.shoot(window)
player.draw(window)
player.update()
player.collision()
player.collision(window)
if player.out_of_area():
death_timer -= 1
timertext_color = (255-death_timer-60,3.6*death_timer,10)
@ -69,6 +66,11 @@ def play():
print('Random is on our side')
break
partic = Particle(400,400)
particles_on_screen.append(partic)
for particle in particles_on_screen:
particle.draw(window)
particle.update()
clock.tick(FPS)
pygame.display.update()

36
beings.py

@ -2,6 +2,7 @@ import pygame
from random import randint
from time import time
from bullets import *
#from particles import * # !!!
import time
import sys
@ -59,9 +60,8 @@ class Player:
for bullet in bullets_on_screen:
bullet.draw(window,(0,0,0),bullet_image)
bullet.move()
if bullet.bullet_y <= -20:
if bullet.bullet_rect[1] <= -20:
bullets_on_screen.remove(bullet)
print('Bullets: ' + str(len(bullets_on_screen)))
def teleportation(self):
self.vel = 10
@ -86,23 +86,19 @@ class Player:
self.y += self.height*2
self.tp_cooldown = 100
# if keys[pygame.K_d] and keys[pygame.K_DOWN] and keys[pygame.K_RIGHT] and self.tp_cooldown == 0: #back-right-dash
# self.y += self.height*2
# self.x += self.height*2
# self.tp_cooldown = 100
def out_of_area(self):
if self.x + self.width > windowX or self.x < 0 or self.y+self.height > windowY or self.y < 0:
return True
else:
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(-100,100)
# self.y += randint(-100,100) #Old hardcoded version
def collision(self,window):
for bullet in enemy_bul_on_screen:
if self.player_rect.colliderect(bullet):
if self.player_rect.colliderect(bullet.bullet_rect):
# new_particle = Particle(bullet.x,bullet.y)
# particles_on_screen.append(new_particle)
# for particle in particles_on_screen:
# particle.draw(window)
# particle.update()
self.x += randint(-100,100)
self.y += randint(-100,100)
@ -135,8 +131,9 @@ class Enemy:
for bullet in enemy_bul_on_screen:
bullet.draw(window,(0,0,0),enemy_bul_img)
bullet.movedwn()
if bullet.bullet_y >= windowY +20:
enemy_bul_on_screen.remove(bullet)
if bullet.bullet_rect[1] >= windowY +20:
enemy_bul_on_screen.remove(bullet) # !!???
print('Bullets (Enemy,Player): ' + str(len(bullets_on_screen + enemy_bul_on_screen)))
def out_of_area(self):
if self.enemy_x > windowX+1 or self.enemy_x < -1 or self.enemy_y > windowY+1 or self.enemy_y < -1:
@ -145,13 +142,8 @@ class Enemy:
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(-60,60)
# self.enemy_y += randint(-60,60) # Old hardcoded version
for bullet in bullets_on_screen:
if self.enemy_rect.colliderect(bullet):
if self.enemy_rect.colliderect(bullet.bullet_rect):
#Particle effect here
self.enemy_x += randint(-60,60)
self.enemy_y += randint(-60,60)

15
bullets.py

@ -1,22 +1,23 @@
import pygame
import random
from math import pow
bullets_on_screen = []
enemy_bul_on_screen = []
class Bullet:
def __init__(self, bullet_x, bullet_y):
def __init__(self, bullet_x, bullet_y): # y = ax^2 + bx + c __maybe ?)
self.bullet_x = bullet_x + random.randint(-9,9) # -8 -- 8
self.bullet_y = bullet_y
self.bullet_width = 4
self.bullet_height = 12
self.bullet_vel = random.randint(16,28) #12 -- 22
self.bullet_rect = pygame.Rect(self.bullet_x,self.bullet_y, self.bullet_width,self.bullet_height)
self.bullet_rect = pygame.Rect(self.bullet_x,self.bullet_y,self.bullet_width,self.bullet_height)
def draw(self,window,start_color,bullet_image):
self.rect = pygame.draw.rect(window,start_color,(self.bullet_x,self.bullet_y,self.bullet_width,self.bullet_height))
window.blit(bullet_image,(self.bullet_x, self.bullet_y))
pygame.draw.rect(window,start_color,self.bullet_rect)
window.blit(bullet_image,(self.bullet_rect[0],self.bullet_rect[1]))
def move(self):
self.bullet_y -= self.bullet_vel
self.bullet_rect[1] -= self.bullet_vel
def movedwn(self):
self.bullet_y += self.bullet_vel
self.bullet_rect[1] += self.bullet_vel

24
particles.py

@ -0,0 +1,24 @@
import pygame
from random import randint
particles_on_screen = []
class Particle:
def __init__(self,x,y):
self.x = x
self.y = y
self.width = 4
self.height = self.width
self.vel = int(randint(0,3)/5)
self.timer = 200
def draw(self,window):
for i in range(10):
pygame.draw.rect(window,(255,255,255),(self.x + randint(-30,30), self.y + randint(-30,30), self.width, self.height))
def update(self):
for particle in particles_on_screen:
self.timer -= 1
#self.x += self.vel - randint(1,2)
self.y += self.vel
if particle.timer <= 0:
particles_on_screen.remove(particle)
#print('Particles :',str(len(particles_on_screen)))

4
surfaces.py

@ -7,10 +7,6 @@ class Surface:
self.width = windowX
self.height = windowY
self.color = (30,70,40)
if str(name) == 'default':
self.width = windowX/1.5
self.height = windowY/1.5
self.color = (50,20,60)
def place(self,window,surf_x,surf_y):
pygame.draw.rect(window,self.color,(surf_x,surf_y,self.width,self.height))

Loading…
Cancel
Save