Commit b0adc32b authored by Ash's avatar Ash

Test

parents
import os
import pygame
#Decides the height width and title of the window
Heightofwindow = 800
Widthofwindow = 1000
Title = "Galaga"
#used by the clock
FpsGame = 60
#global variable for score
Score = 0
#loads sprites for use later in the program
currentPath = os.path.dirname(__file__)
imagePath = os.path.join(currentPath, "img")
playership = pygame.image.load(os.path.join(imagePath, "player.png"))
lazer = pygame.image.load(os.path.join(imagePath, "lazer.png"))
enemyship1 = pygame.image.load(os.path.join(imagePath, "enemy1.png"))
enemyship2 = pygame.image.load(os.path.join(imagePath, "enemy2.png"))
import pygame
import time
import random
import os
import GlobalVer as Univer
from upgrades import damageincrease, enemyspeedincrease, playerspeedincrease
from entities import player, enemy1,enemy2 ,entitylist, lazerlist, playerlazer, playerlist
backgroundblack = (0,0,0)
white = (255,255,255)
pygame.init()
gamedisplay = pygame.display.set_mode((Univer.Widthofwindow, Univer.Heightofwindow))
pygame.display.set_caption(Univer.Title)
clock = pygame.time.Clock();
gameon = True
#setting up instances and creating variables
p = player(500, 750, 3)
e = enemy1(500, 100)
upd = damageincrease()
hinc = enemyspeedincrease()
psinc = playerspeedincrease()
def gameloop():
upgradecounter = 0
#e2 = enemy1(500, 200) You can add this code back put upon having more than one enemy the game seems to lag. I'm unsure as to why.
#Main gameloop starts
while True:
gamedisplay.fill(backgroundblack)
display_score()
displayplayerhp()
#checks for all entitys in the corrasponding list and runs each entity's update method on the gamedisplay and also checks if upgrades happen
for entity in playerlist:
entity.update(gamedisplay)
if upgradecounter >= 3:
hinc.upgrading(entity)
#print(entity.speed)
for entity in entitylist:
entity.update(gamedisplay)
if upgradecounter >= 2:
psinc.upgrading(entity)
#print(entity.speed)
for entity in lazerlist:
entity.update(gamedisplay)
if upgradecounter >= 1:
upd.upgrading(entity)
#print(entity.playerdamage)
pygame.display.update()
#print(e.speed)
#if list is empty it checks the players score and decides it's next course of action
if len(entitylist) ==0:
if Univer.Score < 500:
e = enemy1(500, 100)
elif Univer.Score >= 500 and Univer.Score < 1000:
upgradecounter = 1
e1 = enemy2(500, 100)
elif Univer.Score >= 1000 and Univer.Score < 1500:
upgradecounter = 2
e2 = enemy1(500, 100)
elif Univer.Score >= 1500:
upgradecounter = 3
e3 = enemy2(500, 100)
clock.tick(Univer.FpsGame)
#print(Univer.Score)
#print(playerlazer.playerdamage)
#if the players health is equal or less than 0 the game ends
if p.hp <= 0:
gameover()
break
#if player exits screen the game ends
if p.x > Univer.Widthofwindow or p.x < 0 or p.y < 0 or p.y > Univer.Heightofwindow:
gameover()
break
#displays score
def display_score():
font = pygame.font.SysFont(None, 25)
text = font.render("Score: " + str(Univer.Score), True, white)
gamedisplay.blit(text, (10, 10))
#displays player lives/health
def displayplayerhp():
font = pygame.font.SysFont(None, 25)
text = font.render("Lives: " + str(p.hp), True, white)
gamedisplay.blit(text, (500, 10))
#displays a game over message
def gameover():
print('Game Lost')
font = pygame.font.SysFont(None, 25)
text = font.render("You Lose", True, white)
gamedisplay.blit(text, (500, 500))
pygame.display.update()
#runs the gameloop
gameloop()
import os
import pygame
import GlobalVer as Univer
import math
import random
import time
#setting up needed variables
clock = pygame.time.Clock()
timer = 2 # Decrease this to count down.
dt = 0 # Delta time (time since last tick)
i = 0
#lists to store corrasponding entitiys
lazerlist = []
entitylist = []
playerlist = []
#sets up a base which activates self and can use pygame sprites
class base(pygame.sprite.Sprite):
def __init__(self):
#print("test")
self.active = True
def update(self):
pass
#sets up the player class
class player(base):# inherites from base
#inalitzation method
def __init__(self, x, y, hp):
base.__init__(self)#runs the perants inalization method
#needed variables
self.x = x
self.y = y
self.speed = 6
self.hp = hp
self.x_change = 0
self.y_change = 0
self.playership = Univer.playership
self.rect = self.playership.get_rect() # creates a rect hitbox based on the sprite
playerlist.append(self)#add's the player to the playerlist
#update method for the player
def update(self, gameDisplay):
realX = self.x - 32
realY = self.y - 32
gameDisplay.blit(self.playership, (realX, realY))#displays the player
for event in pygame.event.get():
if event.type == pygame.QUIT:#exit
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:#handles player movement and shooting
if event.key == pygame.K_LEFT:
self.x_change -= self.speed
if event.key == pygame.K_RIGHT:
self.x_change = self.speed
if event.key == pygame.K_UP:
self.y_change -= self.speed
if event.key == pygame.K_DOWN:
self.y_change = self.speed
if event.key == pygame.K_SPACE:
p = playerlazer(self.x, self.y, 1)#creates new instance of lazer
#stops movement based on which keys are not being help down
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
self.x_change = 0
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
self.y_change = 0
self.x += self.x_change
self.y += self.y_change
#sets up enemy class
class enemy1(base):#inherites from base
#inalization method
def __init__(self, x, y):
base.__init__(self)#runs base inilization
self.x = x
self.y = y
self.hp = 1
self.speed = 6
self.enemyfighter= Univer.enemyship1
self.rect = self.enemyfighter.get_rect()
entitylist.append(self)#addes enemy to the entity list
#method for movement
def movement(self):
global i
global timer
if i == 1:
if self.x+self.speed >= Univer.Widthofwindow:
self.x -= self.speed
self.x_change = 0
else:
self.x += self.speed
self.x_change = 0
elif i == 2:
if self.x-self.speed <= 0:
self.x += self.speed
self.x_change = 0
else:
self.x -= self.speed
self.x_change = 0
elif i == 3:
self.shoot()#runs shoot method
timer = 2
self.random()# runs random method
def shoot(self):#creates a new instance of lazer
b = playerlazer(self.x, self.y, 2)
print("Fire")
#update method
def update(self, gameDisplay):
global i
global timer
global dt
global clock
timer -= dt
if self.active:
gameDisplay.blit(self.enemyfighter, (self.x - 30, self.y - 30))
if timer <= 1:
self.movement()
if timer <=0:
timer = 2
self.random()
#print(i)
if self.hp <= 0:
#dies is health is 0 or less
self.active = False
entitylist.remove(self)# removes entity from list
Univer.Score += 100 #adds 100 to the score
dt = clock.tick(30) / 1000
#print(dt)
#print(timer)
def random(self):# method to handle the random options in movement
global i
i = random.randint(1, 3)
#print("test")
#harder stronger enemy creation
class enemy2(enemy1):#inherite from enemy 1
#inalization method
def __init__(self, x, y):
enemy1.__init__(self,x,y)#runs enemy1 inalization
self.hp = 3
self.speed = 10
self.enemyfighter= Univer.enemyship2
self.rect = self.enemyfighter.get_rect()
#update movement
def update(self, gameDisplay):
global timer
global dt
global clock
timer -= dt
if self.active:
gameDisplay.blit(self.enemyfighter, (self.x - 30, self.y - 30))
if timer <= 1:
#uses movement method provided by parent of enemy1
self.movement()
if timer <=0:
timer = 2
self.random()
#print(i)
if self.hp <= 0:
#if dead removes it from list and adds 200 to score
self.active = False
entitylist.remove(self)
Univer.Score += 200
dt = clock.tick(30) / 1000
#print(dt)
#print(timer)
#creates the lazer
class playerlazer(base):#inherites from base
def __init__(self, x, y, dir):#initalization method
base.__init__(self)#runs base initalization method
self.speed = 10
self.playerdamage = 1
self.enemydamage = 1
self.x = x
self.y = y
self.dir = dir
self.plazer = Univer.lazer
self.rect = self.plazer.get_rect()
lazerlist.append(self)#adds to lazer list
#method to handle collisons and what to do if the lazer exits the screen
def update(self, gameDisplay):
if self.active:
gameDisplay.blit(self.plazer, (self.x - 5, self.y - 5))
if self.dir == 1:
self.y -= self.speed
if len(entitylist) ==0:
print("list is empty")
else:
for entity in entitylist:
distance = math.hypot(self.x - entity.x, self.y - entity.y)
if distance < 40:
print("Collide")
lazerlist.remove(self)
entity.hp -= self.playerdamage
print(len(entitylist))
elif (self.dir == 2):
self.y += self.speed
target = playerlist[0]
distance = math.hypot(self.x - target.x, self.y - target.y)
if distance < 35:
self.active = False;
lazerlist.remove(self)
target.hp -= self.enemydamage
if self.x > Univer.Widthofwindow or self.x < 0 or self.y < 0 or self.y > Univer.Heightofwindow:
print("Outside boundaries, should be deleted.")
self.active = False;
lazerlist.remove(self)
img/Lazer.png

238 Bytes

img/enemy1.png

905 Bytes

img/enemy2.png

903 Bytes

img/player.png

440 Bytes

import pygame
#sets up the base class
class upgrades:
def init(self):
self.name = "Upgrade"
def upgrading(self):
print(self.name)
print("upgrading")
#increases player damage to 2
class damageincrease(upgrades):
def init(self):
self.name = "Damage Upgrade"
def upgrading(self, plazer):
plazer.playerdamage = 2
#increases enemy speed to 10
class enemyspeedincrease(upgrades):
def init(self):
self.name = "EnemySpeed Upgrade"
def upgrading(self, enemy):
enemy.speed = 10
#increases player speed to 10
class playerspeedincrease(upgrades):
def init(self):
self.name = "PlayerSpeed Upgrade"
def upgrading(self, player):
player.speed = 10
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment