Commit 04211679 authored by christopher.spray's avatar christopher.spray

push2

parent 5d762c5f
File added
bandit.jpg

4.66 KB

...@@ -6,12 +6,12 @@ from random import randint ...@@ -6,12 +6,12 @@ from random import randint
#just do randint x - width, same for y #just do randint x - width, same for y
class Bandit(pygame.sprite.Sprite): class Bandit(pygame.sprite.Sprite):
def __init__(self, player, window_height, window_width, speedAI, observer, spriteGroupAI): def __init__(self, player, window_height, window_width, speedAI, observer, spriteGroupAI,spriteGroupWall, enemiesOnScreen):
WHITE = (255, 255, 255) WHITE = (255, 255, 255)
enemy = pygame.image.load("troll.png").convert() enemy = pygame.image.load("bandit.jpg").convert()
enemyImg = pygame.transform.scale(enemy, (40,40)) enemyImg = pygame.transform.scale(enemy, (60,60))
pygame.sprite.Sprite.__init__(self) #runs class initializer pygame.sprite.Sprite.__init__(self) #runs class initializer
self.image = pygame.Surface((40,40)) #40,40px self.image = pygame.Surface((60,60)) #40,40px
self.image = enemyImg #load image to object self.image = enemyImg #load image to object
self.image.set_colorkey(WHITE) #set transparency to white self.image.set_colorkey(WHITE) #set transparency to white
self.rect = self.image.get_rect() #get the rect for the img self.rect = self.image.get_rect() #get the rect for the img
...@@ -23,31 +23,28 @@ class Bandit(pygame.sprite.Sprite): ...@@ -23,31 +23,28 @@ class Bandit(pygame.sprite.Sprite):
self.observer = observer self.observer = observer
self.spriteGroupAI = spriteGroupAI self.spriteGroupAI = spriteGroupAI
self.spawnZone = randint(1,4) self.spawnZone = randint(1,4)
self.spriteGroupWall = spriteGroupWall
self.enemiesOnScreen = enemiesOnScreen
if(self.spawnZone == 1): if(self.spawnZone == 1):
#1 top = (300,0) - (500,50) #1 top = (300,0) - (500,50)
self.rect.x = randint(300,500) self.rect.x = randint(300+self.rect.width + 5,500-self.rect.width - 5)
self.rect.y = randint(0, 50) self.rect.y = randint(0, 50)
elif(self.spawnZone == 2): elif(self.spawnZone == 2):
#2 bot = (300,750) - (500,800) #2 bot = (300,750) - (500,800)
self.rect.x = randint(300,500) self.rect.x = randint(300+self.rect.width + 5,500-self.rect.width - 5)
self.rect.y = randint(750, 800) self.rect.y = randint(750, 800)
elif(self.spawnZone == 3): elif(self.spawnZone == 3):
#3 left = (0, 300) - (50,500) #3 left = (0, 300) - (50,500)
self.rect.x = randint(0,50) self.rect.x = randint(0,50)
self.rect.y = randint(300, 500) self.rect.y = randint(300+self.rect.height + 5, 500-self.rect.height - 5)
elif(self.spawnZone == 4): elif(self.spawnZone == 4):
#4 right = (750, 300) - (800, 500) #4 right = (750, 300) - (800, 500)
self.rect.x = randint(750,800) self.rect.x = randint(750,800)
self.rect.y = randint(300, 500) self.rect.y = randint(300+self.rect.height + 5, 500-self.rect.height + 5)
def update(self): def update(self):
self.followPlayer() self.followPlayer()
#self.collidedWith()
def die(self):
print("DED")
# self.observer.notify_enemy_died()
def followPlayer(self): def followPlayer(self):
#https://stackoverflow.com/questions/20044791/how-to-make-an-enemy-follow-the-player-in-pygame #https://stackoverflow.com/questions/20044791/how-to-make-an-enemy-follow-the-player-in-pygame
...@@ -60,6 +57,10 @@ class Bandit(pygame.sprite.Sprite): ...@@ -60,6 +57,10 @@ class Bandit(pygame.sprite.Sprite):
yDiff = yDiff/xyDistance #normalize the y-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.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 self.rect.y -= yDiff * self.speed #move in the y-axis towards the player at its own speed
WalkInWall = pygame.sprite.groupcollide(self.spriteGroupAI, self.spriteGroupWall, True,False) #if bandit collides with any wall
if WalkInWall:
#self.kill()
self.enemiesOnScreen -=1
except ZeroDivisionError: #if it throws a ZeroDivisionError.... except ZeroDivisionError: #if it throws a ZeroDivisionError....
#print("BIG OL ERROR") #Do shizz #print("BIG OL ERROR") #Do shizz
pass pass
...@@ -2,8 +2,9 @@ import pygame ...@@ -2,8 +2,9 @@ import pygame
class Bullet(pygame.sprite.Sprite): #create a sprite object class Bullet(pygame.sprite.Sprite): #create a sprite object
def __init__(self, x, y, angle, speed, dir): def __init__(self, x, y, angle, speed, dir, window_width, window_height):
bullet= pygame.image.load("wall.png").convert() WHITE = (255, 255, 255)
bullet= pygame.image.load("bullet.png").convert()
bulletImg= pygame.transform.scale(bullet, (20, 10)) bulletImg= pygame.transform.scale(bullet, (20, 10))
pygame.sprite.Sprite.__init__(self) #runs class initializer pygame.sprite.Sprite.__init__(self) #runs class initializer
self.image = bulletImg #load image to object self.image = bulletImg #load image to object
...@@ -13,4 +14,30 @@ class Bullet(pygame.sprite.Sprite): #create a sprite object ...@@ -13,4 +14,30 @@ class Bullet(pygame.sprite.Sprite): #create a sprite object
self.speed = speed self.speed = speed
self.angle = angle self.angle = angle
self.image = pygame.transform.rotate(self.image, self.angle) self.image = pygame.transform.rotate(self.image, self.angle)
self.image.set_colorkey(WHITE) #set transparency to white
self.direction = dir self.direction = dir
self.window_width = window_width
self.window_height = window_height
def update(self):
if self.rect.right > self.window_width:
self.kill()
pass
if self.rect.left < 0:
#kill
pass
if self.rect.bottom > self.window_height:
#kill
pass
if self.rect.top < 0:
#kill
pass
if self.direction == "up":
self.rect.y -= self.speed #do all 4 directions
elif self.direction == "down":
self.rect.y += self.speed #do all 4 directions
elif self.direction == "left":
self.rect.x -= self.speed #do all 4 directions
elif self.direction == "right":
self.rect.x += self.speed #do all 4 directions
...@@ -4,28 +4,46 @@ class Cowboy(pygame.sprite.Sprite): #create a sprite object ...@@ -4,28 +4,46 @@ class Cowboy(pygame.sprite.Sprite): #create a sprite object
def __init__(self, window_width, window_height, speed): def __init__(self, window_width, window_height, speed):
WHITE = (255, 255, 255) WHITE = (255, 255, 255)
player= pygame.image.load("player.png").convert() player= pygame.image.load("player.jpg").convert()
playerImg= pygame.transform.scale(player, (40,40)) self.playerImg = pygame.transform.scale(player, (50,50))
self.playerImgRight = pygame.transform.rotate(self.playerImg, 90)
self.playerImgUp = pygame.transform.rotate(self.playerImg, 180)
self.playerImgLeft = pygame.transform.rotate(self.playerImg, 270)
pygame.sprite.Sprite.__init__(self) #runs class initializer pygame.sprite.Sprite.__init__(self) #runs class initializer
self.image = pygame.Surface((40,40)) #40,40px self.image = pygame.Surface((50,50)) #40,40px
self.image = playerImg #load image to object self.image = self.playerImg #load image to object
self.image.set_colorkey(WHITE) #set transparency to white self.image.set_colorkey(WHITE) #set transparency to white
self.rect = self.image.get_rect() #get the rect for the img 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.center = (window_height / 2, window_width / 2) #place in center of screen
self.rect.x = window_width / 2 self.rect.x = window_width / 2
self.rect.y = window_height / 2 self.rect.y = window_height / 2
self.speed = speed self.speed = speed
self.health = 100 self.health = 3
def update(self, window_width, window_height): #update method (called every loop) def update(self, window_width, window_height): #update method (called every loop)
#check if char is off screen, then force it back to edge #check if char is off screen, then force it back to edge
if self.rect.right > window_width: if self.rect.right > window_width - 50:
self.rect.right = window_width self.rect.right = window_width - 50
if self.rect.left < 0: if self.rect.left < 50:
self.rect.left = 0 self.rect.left = 50
if self.rect.bottom > window_height: if self.rect.bottom > window_height - 50:
self.rect.bottom = window_height self.rect.bottom = window_height - 50
if self.rect.top < 0: if self.rect.top < 50:
self.rect.top = 0 self.rect.top = 50
def rotate(self, direction):
WHITE = (255, 255, 255)
if direction == "right":
self.image = self.playerImgRight
self.image.set_colorkey(WHITE)
elif direction == "left":
self.image = self.playerImgLeft
self.image.set_colorkey(WHITE)
elif direction == "up":
self.image = self.playerImgUp
self.image.set_colorkey(WHITE)
elif direction == "down":
self.image = self.playerImg
self.image.set_colorkey(WHITE)
...@@ -3,9 +3,13 @@ from bandit import Bandit ...@@ -3,9 +3,13 @@ from bandit import Bandit
from cowboy import Cowboy from cowboy import Cowboy
from observer import ai_observer from observer import ai_observer
from wall import Wall from wall import Wall
from bullet import Bullet
import time
pygame.init() pygame.init()
clock = pygame.time.Clock() clock = pygame.time.Clock()
font = pygame.font.Font("Pixeled.ttf", 15)
fps = 60 fps = 60
window_height = 800 window_height = 800
window_width = 800 window_width = 800
...@@ -21,9 +25,12 @@ upPressed = False ...@@ -21,9 +25,12 @@ upPressed = False
downPressed = False downPressed = False
leftPressed = False leftPressed = False
rightPressed = False rightPressed = False
bulletFired = False
bulletFiredTime = 0
bulletFireRate = 0.333
#pygame.mixer.music.load('D:/NEWGAME/music.mp3') pygame.mixer.music.load('music.mp3')
#pygame.mixer.music.play(-1) pygame.mixer.music.play(-1)
pygame.display.set_caption("Cowboy Shooter") pygame.display.set_caption("Cowboy Shooter")
screenSize = (window_width, window_height) screenSize = (window_width, window_height)
...@@ -87,6 +94,11 @@ spriteGroup.add(player) ...@@ -87,6 +94,11 @@ spriteGroup.add(player)
#------------------------------------------------------------- #-------------------------------------------------------------
upCollide = False
downCollide = False
leftCollide = False
rightCollide = False
while run is True: while run is True:
clock.tick(fps) clock.tick(fps)
screen.blit(background, [0,0]) screen.blit(background, [0,0])
...@@ -141,30 +153,43 @@ while run is True: ...@@ -141,30 +153,43 @@ while run is True:
#------------------------------------------------------------- #-------------------------------------------------------------
#EVENTS GO ABOVE LINE, CODE GOES BELOW LINE #EVENTS GO ABOVE LINE, CODE GOES BELOW LINE
if wPressed: if wPressed: #and not upCollide:
player.rect.y -= speed player.rect.y -= speed
if aPressed: player.rotate("up")
if aPressed: #and not leftCollide:
player.rect.x -= speed player.rect.x -= speed
if sPressed: player.rotate("left")
if sPressed: #and not downCollide:
player.rect.y += speed player.rect.y += speed
if dPressed: player.rotate("down")
if dPressed: #and not rightCollide:
player.rect.x += speed player.rect.x += speed
player.rotate("right")
if not bulletFired:
if upPressed: if upPressed:
#spawnBullet bulletFired = True
pass bulletFiredTime = time.time()
if downPressed: spriteGroupBullet.add(Bullet(player.rect.x+(player.rect.width/2-5), player.rect.y+(player.rect.height/2-3), 270, speedBullet, "up", window_width, window_height))
#spawnBullet elif downPressed:
pass bulletFired = True
if leftPressed: bulletFiredTime = time.time()
#spawnBullet spriteGroupBullet.add(Bullet(player.rect.x+(player.rect.width/2-5), player.rect.y+(player.rect.height/2-3), 90, speedBullet, "down", window_width, window_height))
pass elif leftPressed:
if rightPressed: bulletFired = True
#spawnBullet bulletFiredTime = time.time()
spriteGroupBullet.add(Bullet(player.rect.x+(player.rect.width/2-5), player.rect.y+(player.rect.height/2-3), 180, speedBullet, "left", window_width, window_height))
elif rightPressed:
bulletFired = True
bulletFiredTime = time.time()
spriteGroupBullet.add(Bullet(player.rect.x+(player.rect.width/2-5), player.rect.y+(player.rect.height/2-3), 0, speedBullet, "right", window_width, window_height))
pass pass
if bulletFired:
if time.time() > bulletFiredTime + bulletFireRate:
bulletFired = False
if(enemiesOnScreen <5): if(enemiesOnScreen <5):
enemiesOnScreen += 1 enemiesOnScreen += 1
b = Bandit(player, window_height, window_width, speedAI, observer,spriteGroupAI) b = Bandit(player, window_height, window_width, speedAI, observer,spriteGroupAI, spriteGroupWall, enemiesOnScreen)
spriteGroupAI.add(b) spriteGroupAI.add(b)
else: else:
pass; pass;
...@@ -182,14 +207,38 @@ while run is True: ...@@ -182,14 +207,38 @@ while run is True:
run = False run = False
#Checking if player hits wall #Checking if player hits wall
playerHitWall = pygame.sprite.collide_rect(player, wallTopLeftHor) #if player collides with any wall # playerHitWall = pygame.sprite.spritecollide(player, spriteGroupWall, False) #if player collides with any wall
if playerHitWall: # if playerHitWall:
#print("wallTopLeftHor") # if wPressed:
if player.rect.bottom == wallTopLeftHor.rect.bottom: # upCollide = True
print("TOP HIT") # elif aPressed:
player.rect == wallTopLeftHor.rect.bottom - 30 # leftCollide = True
# elif sPressed:
# downCollide = True
# elif dPressed:
# rightCollide = True
# else:
# upCollide = False
# leftCollide = False
# downCollide = False
# rightCollide = False
#checking if bullets hit the bandits
bulletHitBandit = pygame.sprite.groupcollide(spriteGroupBullet, spriteGroupAI, True, True)
if bulletHitBandit:
observer.notify_enemy_died()
enemiesOnScreen -=1
#checking if bullet hits any walls
bulletHitWall = pygame.sprite.groupcollide(spriteGroupBullet, spriteGroupWall, True, False)
#-------------------------------------------------------------
#FONTS
#render text, typecast ints as string, print to screen, in black
enemiesLeftText = font.render("Enemies Remaining: " + str(observer.enemiesRemaining), True, (0,0,0))
healthText = font.render("Health: x" + str(player.health), True, (0,0,0))
#------------------------------------------------------------- #-------------------------------------------------------------
...@@ -198,7 +247,11 @@ while run is True: ...@@ -198,7 +247,11 @@ while run is True:
spriteGroup.update(window_width, window_height) #calls all the update functions in all sprites spriteGroup.update(window_width, window_height) #calls all the update functions in all sprites
spriteGroupAI.update() spriteGroupAI.update()
spriteGroupWall.update() spriteGroupWall.update()
spriteGroupBullet.update()
spriteGroup.draw(screen) spriteGroup.draw(screen)
spriteGroupAI.draw(screen) spriteGroupAI.draw(screen)
spriteGroupWall.draw(screen) spriteGroupWall.draw(screen)
spriteGroupBullet.draw(screen)
screen.blit(enemiesLeftText, (5,5))
screen.blit(healthText, (575,3))
pygame.display.flip() pygame.display.flip()
...@@ -9,5 +9,5 @@ class ai_observer(): ...@@ -9,5 +9,5 @@ class ai_observer():
self.enemiesRemaining -=1 self.enemiesRemaining -=1
print("enemies remaining:", self.enemiesRemaining) print("enemies remaining:", self.enemiesRemaining)
if self.enemiesRemaining <= 3: if self.enemiesRemaining <= 0:
print("Game over") game.run == False
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