Commit 5d762c5f authored by christopher.spray's avatar christopher.spray

init

parent 7c9e9164
File added
import pygame
import math
from random import randint
#Might need to change spawning area when implementing wall collision
#just do randint x - width, same for y
class Bandit(pygame.sprite.Sprite):
def __init__(self, player, window_height, window_width, speedAI, observer, spriteGroupAI):
WHITE = (255, 255, 255)
enemy = pygame.image.load("troll.png").convert()
enemyImg = pygame.transform.scale(enemy, (40,40))
pygame.sprite.Sprite.__init__(self) #runs class initializer
self.image = pygame.Surface((40,40)) #40,40px
self.image = enemyImg #load image to object
self.image.set_colorkey(WHITE) #set transparency to white
self.rect = self.image.get_rect() #get the rect for the img
self.rect.center = (window_height / 2, window_width / 2) #place in center of screen
#self.rect.y = randint(0, window_height-self.rect.width)
#self.rect.x = randint(0, window_height-self.rect.width)
self.speed = speedAI
self.playerRef = player
self.observer = observer
self.spriteGroupAI = spriteGroupAI
self.spawnZone = randint(1,4)
if(self.spawnZone == 1):
#1 top = (300,0) - (500,50)
self.rect.x = randint(300,500)
self.rect.y = randint(0, 50)
elif(self.spawnZone == 2):
#2 bot = (300,750) - (500,800)
self.rect.x = randint(300,500)
self.rect.y = randint(750, 800)
elif(self.spawnZone == 3):
#3 left = (0, 300) - (50,500)
self.rect.x = randint(0,50)
self.rect.y = randint(300, 500)
elif(self.spawnZone == 4):
#4 right = (750, 300) - (800, 500)
self.rect.x = randint(750,800)
self.rect.y = randint(300, 500)
def update(self):
self.followPlayer()
#self.collidedWith()
def die(self):
print("DED")
# self.observer.notify_enemy_died()
def followPlayer(self):
#https://stackoverflow.com/questions/20044791/how-to-make-an-enemy-follow-the-player-in-pygame
#Code adapted from there ^
try: #try this block
xDiff = self.rect.x - self.playerRef.rect.x #get x-axis difference
yDiff = self.rect.y - self.playerRef.rect.y #get y-axis difference
xyDistance = math.hypot(xDiff, yDiff) #get Euclidian diff in coords
xDiff = xDiff/xyDistance #normalize the x-axis difference to get direction
yDiff = yDiff/xyDistance #normalize the y-axis difference to get direction
self.rect.x -= xDiff * self.speed #move in the y-axis towards the player at its own speed
self.rect.y -= yDiff * self.speed #move in the y-axis towards the player at its own speed
except ZeroDivisionError: #if it throws a ZeroDivisionError....
#print("BIG OL ERROR") #Do shizz
pass
bullet.png

331 Bytes

import pygame
class Bullet(pygame.sprite.Sprite): #create a sprite object
def __init__(self, x, y, angle, speed, dir):
bullet= pygame.image.load("wall.png").convert()
bulletImg= pygame.transform.scale(bullet, (20, 10))
pygame.sprite.Sprite.__init__(self) #runs class initializer
self.image = bulletImg #load image to object
self.rect = self.image.get_rect() #get the rect for the img
self.rect.x = x
self.rect.y = y
self.speed = speed
self.angle = angle
self.image = pygame.transform.rotate(self.image, self.angle)
self.direction = dir
import pygame
class Cowboy(pygame.sprite.Sprite): #create a sprite object
def __init__(self, window_width, window_height, speed):
WHITE = (255, 255, 255)
player= pygame.image.load("player.png").convert()
playerImg= pygame.transform.scale(player, (40,40))
pygame.sprite.Sprite.__init__(self) #runs class initializer
self.image = pygame.Surface((40,40)) #40,40px
self.image = playerImg #load image to object
self.image.set_colorkey(WHITE) #set transparency to white
self.rect = self.image.get_rect() #get the rect for the img
self.rect.center = (window_height / 2, window_width / 2) #place in center of screen
self.rect.x = window_width / 2
self.rect.y = window_height / 2
self.speed = speed
self.health = 100
def update(self, window_width, window_height): #update method (called every loop)
#check if char is off screen, then force it back to edge
if self.rect.right > window_width:
self.rect.right = window_width
if self.rect.left < 0:
self.rect.left = 0
if self.rect.bottom > window_height:
self.rect.bottom = window_height
if self.rect.top < 0:
self.rect.top = 0
import pygame, sys
from bandit import Bandit
from cowboy import Cowboy
from observer import ai_observer
from wall import Wall
pygame.init()
clock = pygame.time.Clock()
fps = 60
window_height = 800
window_width = 800
run=True
speed = 3
speedAI = 2
speedBullet = 4
wPressed = False
aPressed = False
sPressed = False
dPressed = False
upPressed = False
downPressed = False
leftPressed = False
rightPressed = False
#pygame.mixer.music.load('D:/NEWGAME/music.mp3')
#pygame.mixer.music.play(-1)
pygame.display.set_caption("Cowboy Shooter")
screenSize = (window_width, window_height)
screen = pygame.display.set_mode(screenSize)
background = pygame.image.load("background.jpg").convert()
spriteGroup = pygame.sprite.Group()
spriteGroupAI = pygame.sprite.Group()
spriteGroupWall = pygame.sprite.Group()
spriteGroupBullet = pygame.sprite.Group()
maxBandits = 40
enemiesRemaining = maxBandits
#---------------------------------------------------
#placing Walls
wallTopLeftHor = Wall(0,0,0)
wallTopLeftVer = Wall(0,0,90)
wallTopRightHor = Wall(500,0,0)
wallTopRightVer = Wall(750,0,90)
wallBotLeftHor = Wall(0,750,0)
wallBotLeftVer = Wall(0,500,90)
wallBotRightHor = Wall(500,750,0)
wallBotRightVer = Wall(750,500,90)
spriteGroupWall.add(wallTopLeftHor)
spriteGroupWall.add(wallTopLeftVer)
spriteGroupWall.add(wallTopRightHor)
spriteGroupWall.add(wallTopRightVer)
spriteGroupWall.add(wallBotLeftHor)
spriteGroupWall.add(wallBotLeftVer)
spriteGroupWall.add(wallBotRightHor)
spriteGroupWall.add(wallBotRightVer)
#---------------------------------------------------
player = Cowboy(window_width, window_height, speed)
observer = ai_observer(maxBandits)
#bandit1 = Bandit(player, window_height, window_width, speedAI)
enemiesOnScreen = 0
#for i in range (maxBandits):
# if(enemiesOnScreen <5):
# enemiesOnScreen += 1
# b = Bandit(player, window_height, window_width, speedAI, observer,spriteGroupAI)
# spriteGroupAI.add(b)
# else:
spriteGroup.add(player)
#-------------------------------------------------------------
#BULLETS
#-------------------------------------------------------------
while run is True:
clock.tick(fps)
screen.blit(background, [0,0])
for event in pygame.event.get():
#https://gamedev.stackexchange.com/questions/54841/rpg-movement-holding-down-button
#player movement adapted from there ^
if event.type == pygame.KEYDOWN:
if event.key == ord('w'):
wPressed = True
player.speed = speed
if event.key == ord('a'):
aPressed = True
player.speed = speed
if event.key == ord('s'):
sPressed = True
player.speed = speed
if event.key == ord('d'):
dPressed = True
player.speed = speed
if event.key == pygame.K_UP:
upPressed = True
if event.key == pygame.K_LEFT:
leftPressed = True
if event.key == pygame.K_DOWN:
downPressed = True
if event.key == pygame.K_RIGHT:
rightPressed = True
if event.type ==pygame.KEYUP:
if event.key == ord('w'):
wPressed = False
if event.key == ord('a'):
aPressed = False
if event.key == ord('s'):
sPressed = False
if event.key == ord('d'):
dPressed = False
if event.key == pygame.K_UP:
upPressed = False
if event.key == pygame.K_LEFT:
leftPressed = False
if event.key == pygame.K_DOWN:
downPressed = False
if event.key == pygame.K_RIGHT:
rightPressed = False
if event.type == pygame.QUIT:
run = False
pygame.quit()
#-------------------------------------------------------------
#EVENTS GO ABOVE LINE, CODE GOES BELOW LINE
if wPressed:
player.rect.y -= speed
if aPressed:
player.rect.x -= speed
if sPressed:
player.rect.y += speed
if dPressed:
player.rect.x += speed
if upPressed:
#spawnBullet
pass
if downPressed:
#spawnBullet
pass
if leftPressed:
#spawnBullet
pass
if rightPressed:
#spawnBullet
pass
if(enemiesOnScreen <5):
enemiesOnScreen += 1
b = Bandit(player, window_height, window_width, speedAI, observer,spriteGroupAI)
spriteGroupAI.add(b)
else:
pass;
#-------------------------------------------------------------
#COLLISIONS
#Checking if player collides with bandits
banditHitPlayer = pygame.sprite.spritecollide(player,spriteGroupAI, True) #if player collides with any bandit, delete that bandit
if banditHitPlayer:
player.health -=1
observer.notify_enemy_died()
enemiesOnScreen -=1
if(player.health <=0):
run = False
#Checking if player hits wall
playerHitWall = pygame.sprite.collide_rect(player, wallTopLeftHor) #if player collides with any wall
if playerHitWall:
#print("wallTopLeftHor")
if player.rect.bottom == wallTopLeftHor.rect.bottom:
print("TOP HIT")
player.rect == wallTopLeftHor.rect.bottom - 30
#-------------------------------------------------------------
spriteGroup.update(window_width, window_height) #calls all the update functions in all sprites
spriteGroupAI.update()
spriteGroupWall.update()
spriteGroup.draw(screen)
spriteGroupAI.draw(screen)
spriteGroupWall.draw(screen)
pygame.display.flip()
File added
import pygame
class ai_observer():
def __init__(self, maxBandits):
self.enemiesRemaining = maxBandits
def notify_enemy_died(self):
self.enemiesRemaining -=1
print("enemies remaining:", self.enemiesRemaining)
if self.enemiesRemaining <= 3:
print("Game over")
player.png

1.61 KB

troll.png

15.5 KB

wall.png

8.9 KB

import pygame
class Wall(pygame.sprite.Sprite): #create a sprite object
def __init__(self, x, y, angle):
wall= pygame.image.load("wall.png").convert()
wallImg= pygame.transform.scale(wall, (300, 50))
pygame.sprite.Sprite.__init__(self) #runs class initializer
self.image = wallImg #load image to object
self.angle = angle
self.image = pygame.transform.rotate(self.image, self.angle)
self.rect = self.image.get_rect() #get the rect for the img
self.rect.x = x
self.rect.y = y
self.imageWidth = 300
self.imageHeight = 50
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