Browse Source

Clearer code, tried to make a surface

master
Unbewohnte 4 years ago
parent
commit
ef4de5258f
  1. 33
      Main.py
  2. 21
      beings.py
  3. 6
      bullets.py
  4. 9
      images.py
  5. 16
      settings.py
  6. 3
      surfaces.py

33
Main.py

@ -1,37 +1,33 @@
import pygame import pygame
import pygame_menu import pygame_menu
import sys import sys
from settings import windowX,windowY, DEBUG, window_caption,logo,gamefont
from bullets import Bullet from bullets import Bullet
from beings import * from beings import *
from surfaces import SlowTimeSurf from surfaces import SlowTimeSurf
from particles import * from particles import *
######## Set up things that will not change
pygame.init() pygame.init()
pygame.display.set_caption('Healthless') pygame.display.set_caption(window_caption)
windowX = 832
windowY = 832
window = pygame.display.set_mode((windowX,windowY)) window = pygame.display.set_mode((windowX,windowY))
icon = pygame.image.load('pics/logo.png') pygame.display.set_icon(logo)
pygame.display.set_icon(icon) font = pygame.font.Font(gamefont, 46)
font = pygame.font.Font('freesansbold.ttf', 46)
########
DEBUG = False
################################################################ ################################################################
def play(): def play():
FPS = 70 FPS = 70
clock = pygame.time.Clock() clock = pygame.time.Clock()
pygame.mouse.set_visible(False) pygame.mouse.set_visible(False)
slowsurf = SlowTimeSurf(400,20) slowsurf = SlowTimeSurf(384,64)
enemy = Enemy(100,100) enemy = Enemy(100,100)
enemy2 = Enemy(windowX,200) enemy2 = Enemy(windowX,200)
player = Player() player = Player()
death_timer = 70 death_timer = 70
SCORE = 0 SCORE = 0
########################## ########################## LAYER 0
while True: while True:
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == pygame.QUIT: if event.type == pygame.QUIT:
gamerun = False
pygame.quit(),sys.exit() pygame.quit(),sys.exit()
break break
@ -45,9 +41,9 @@ def play():
pygame.draw.rect(window,(184, 227, 179),(0,column,windowX,2)) pygame.draw.rect(window,(184, 227, 179),(0,column,windowX,2))
########################## ########################## LAYER 0 END
#PPPPPPPPPPPPPPPPPPPPPPPPP ########################## LAYER 1
slowsurf.place(window) slowsurf.place(window)
if slowsurf.collide(player.player_rect): if slowsurf.collide(player.player_rect):
enemy.vel -= 0.1 enemy.vel -= 0.1
@ -74,9 +70,9 @@ def play():
else: else:
death_timer = 70 death_timer = 70
#PPPPPPPPPPPPPPPPPPPPPPPPP ########################## LAYER 1 END
#EEEEEEEEEEEEEEEEEEEEEEEEE ########################## LAYER 2
if enemy.alive == True: if enemy.alive == True:
enemy.enemy_shoot(window) enemy.enemy_shoot(window)
enemy.draw(window) enemy.draw(window)
@ -103,11 +99,12 @@ def play():
if enemy.alive == False and enemy2.alive == False: if enemy.alive == False and enemy2.alive == False:
print('Win-win') print('Win-win')
break break
#EEEEEEEEEEEEEEEEEEEEEEEEE ########################## LAYER 2 END
########################## LAYER 3
score_font = font.render(str(SCORE), True, (114, 150, 47)) score_font = font.render(str(SCORE), True, (114, 150, 47))
window.blit(score_font,(100,100)) window.blit(score_font,(100,100))
########################## LAYER 3 END
clock.tick(FPS) clock.tick(FPS)
pygame.display.update() pygame.display.update()
pass pass

21
beings.py

@ -3,20 +3,11 @@ from random import randint
from time import time from time import time
from bullets import * from bullets import *
from particles import * from particles import *
from images import * from settings import windowX,windowY,bullet_image,enemy_bul_img,enemy_image,player_image,timesurf_image
import time import time
import sys import sys
############
windowX = 832
windowY = 832
# ############ imgs for player-enemy(self,bullet), surfaces
# bullet_image = pygame.image.load('pics/second_bullet.png')
# enemy_bul_img = pygame.image.load('pics/bullet.png')
# enemy_image = pygame.image.load('pics/32x64.png')
# player_image = pygame.image.load('pics/32x64.png')
# timesurf_image = pygame.image.load('pics/timesurf.png')
# ############
class Player: class Player:
def __init__(self): def __init__(self):
@ -60,7 +51,7 @@ class Player:
if int(len(player_bullets_on_screen)) > 0: if int(len(player_bullets_on_screen)) > 0:
for bullet in player_bullets_on_screen: for bullet in player_bullets_on_screen:
bullet.draw(window,(0,0,0),bullet_image) bullet.draw(window)
bullet.move("up") bullet.move("up")
if bullet.bullet_rect[1] <= -20: if bullet.bullet_rect[1] <= -20:
player_bullets_on_screen.remove(bullet) player_bullets_on_screen.remove(bullet)
@ -119,10 +110,12 @@ class Enemy:
self.vel = 3 self.vel = 3
self.bul_cooldown = 10 self.bul_cooldown = 10
self.alive = True self.alive = True
self.enemy_rect = pygame.Rect(self.enemy_x, self.enemy_y, self.en_width, self.en_height)
def draw(self,window): def draw(self,window):
pygame.draw.rect(window,(0,0,0),(self.enemy_x, self.enemy_y, self.en_width, self.en_height)) pygame.draw.rect(window,(0,0,0),(self.enemy_x, self.enemy_y, self.en_width, self.en_height))
window.blit(enemy_image,(self.enemy_x, self.enemy_y)) window.blit(enemy_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 self.enemy_rect = pygame.Rect(self.enemy_x, self.enemy_y, self.en_width, self.en_height)
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
@ -143,7 +136,7 @@ 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,(0,0,0),enemy_bul_img) bullet.draw(window)
bullet.move("down") bullet.move("down")
if bullet.bullet_rect[1] >= windowY +20: if bullet.bullet_rect[1] >= windowY +20:
enemy_bul_on_screen.remove(bullet) enemy_bul_on_screen.remove(bullet)

6
bullets.py

@ -1,7 +1,7 @@
import pygame import pygame
import random import random
from math import pow from math import pow
from settings import bullet_image
player_bullets_on_screen = [] player_bullets_on_screen = []
enemy_bul_on_screen = [] enemy_bul_on_screen = []
@ -14,8 +14,8 @@ class Bullet:
self.bullet_vel = random.randint(24,36) #16 -- 28 self.bullet_vel = random.randint(24,36) #16 -- 28
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): def draw(self,window):
pygame.draw.rect(window,start_color,self.bullet_rect) pygame.draw.rect(window,(0,0,0),self.bullet_rect)
window.blit(bullet_image,(self.bullet_rect[0],self.bullet_rect[1])) window.blit(bullet_image,(self.bullet_rect[0],self.bullet_rect[1]))
def move(self,side): def move(self,side):
if str(side) == "up": if str(side) == "up":

9
images.py

@ -1,9 +0,0 @@
import pygame
############ imgs for player-enemy(self,bullet), surfaces
bullet_image = pygame.image.load('pics/second_bullet.png')
enemy_bul_img = pygame.image.load('pics/bullet.png')
enemy_image = pygame.image.load('pics/32x64.png')
player_image = pygame.image.load('pics/32x64.png')
timesurf_image = pygame.image.load('pics/timesurf.png')
############

16
settings.py

@ -0,0 +1,16 @@
import pygame
############
bullet_image = pygame.image.load('pics/second_bullet.png')
enemy_bul_img = pygame.image.load('pics/bullet.png')
enemy_image = pygame.image.load('pics/32x64.png')
player_image = pygame.image.load('pics/32x64.png')
timesurf_image = pygame.image.load('pics/timesurf.png')
logo = pygame.image.load('pics/logo.png')
############
window_caption = 'Healthless'
gamefont = 'freesansbold.ttf'
############
windowX = 832
windowY = 832
############
DEBUG = False

3
surfaces.py

@ -1,6 +1,7 @@
import pygame import pygame
from beings import * from beings import *
from images import timesurf_image from settings import timesurf_image
class Surface: class Surface:
def __init__(self,surf_x,surf_y): def __init__(self,surf_x,surf_y):
self.x = surf_x self.x = surf_x

Loading…
Cancel
Save