Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import random
- import os
- import sys
- import the_sprites
- import settings
- pygame.init()
- if getattr(sys, 'frozen', False):
- os.chdir(sys._MEIPASS)
- class Game(pygame.sprite.Sprite):
- def __init__(self, level):
- super().__init__()
- self.v = self.returnEnemyL()
- self.level = level
- self.landingCords = 0
- self.hero = the_sprites.thePlayer(45, 625, 40, 40)
- #self.theGround = Ground_lvl1(0, 625)
- self.font = pygame.font.SysFont('comicsans', 30, True, True)
- self.score = 0
- self.enemy = [the_sprites.enemy(400, 625, 40, 40, 850), the_sprites.enemy_2(1450, 625, 40, 40, 1900), the_sprites.enemy_3(1000, 625, 40, 40, 1500),
- the_sprites.enemy(800, 625, 40, 40, 1300), the_sprites.enemy(1900, 625, 40, 40, 2400), the_sprites.enemy(2300, 625, 40, 40, 2900),]
- self.enemy_sky = [the_sprites.enemy(400, 625, 40, 40, 850), the_sprites.enemy_2(1450, 625, 40, 40, 1900), the_sprites.enemy(1000, 625, 40, 40, 150)]
- self.enemy_forest = [the_sprites.enemy_2(400, 625, 40, 40, 850), the_sprites.enemy_3(1450, 625, 40, 40, 1900),
- the_sprites.enemy(1000, 625, 40, 40, 1500),
- the_sprites.enemy(800, 625, 40, 40, 1300), the_sprites.enemy_3(1900, 625, 40, 40, 2400)]
- self.enemy_rushR = [the_sprites.enemy(1100, 625, 40, 40, 700), the_sprites.enemy(1050, 625, 40, 40, 700),
- the_sprites.enemy(1200, 625, 40, 40, 700), the_sprites.enemy(1010, 625, 40, 40, 700),
- the_sprites.enemy(1150, 625, 40, 40, 700), the_sprites.enemy(1150, 625, 40, 40, 700),
- the_sprites.enemy(1080, 625, 40, 40, 700), the_sprites.enemy(1100, 625, 40, 40, 700),
- the_sprites.enemy(1000, 625, 40, 40, 700), the_sprites.enemy(1250, 625, 40, 40, 700),
- the_sprites.enemy(1200, 625, 40, 40, 700), the_sprites.enemy(1050, 625, 40, 40, 700),
- the_sprites.enemy(1100, 625, 40, 40, 700), the_sprites.enemy(1250, 625, 40, 40, 700),
- the_sprites.enemy(1200, 625, 40, 40, 700), the_sprites.enemy(1150, 625, 40, 40, 700),
- the_sprites.enemy(1260, 625, 40, 40, 700), the_sprites.enemy(1000, 625, 40, 40, 700),
- the_sprites.enemy(1280, 625, 40, 40, 700), the_sprites.enemy(1100, 625, 40, 40, 700),
- the_sprites.enemy(1000, 625, 40, 40, 700), the_sprites.enemy(1250, 625, 40, 40, 700),
- the_sprites.enemy(1200, 625, 40, 40, 700), the_sprites.enemy(1155, 625, 40, 40, 700)]
- self.enemy_rushR2 = [the_sprites.enemy(1100, 625, 40, 40, 700), the_sprites.enemy(1050, 625, 40, 40, 700),
- the_sprites.enemy(1200, 625, 40, 40, 700), the_sprites.enemy(1010, 625, 40, 40, 700),
- the_sprites.enemy(1150, 625, 40, 40, 700), the_sprites.enemy(1150, 625, 40, 40, 700),
- the_sprites.enemy(1080, 625, 40, 40, 700), the_sprites.enemy(1100, 625, 40, 40, 700),
- the_sprites.enemy(1000, 625, 40, 40, 700), the_sprites.enemy(1250, 625, 40, 40, 700),
- the_sprites.enemy(1200, 625, 40, 40, 700), the_sprites.enemy(1050, 625, 40, 40, 700),
- the_sprites.enemy(1100, 625, 40, 40, 700), the_sprites.enemy(1250, 625, 40, 40, 700),
- the_sprites.enemy(1200, 625, 40, 40, 700), the_sprites.enemy(1150, 625, 40, 40, 700),
- the_sprites.enemy(1260, 625, 40, 40, 700), the_sprites.enemy(1000, 625, 40, 40, 700),
- the_sprites.enemy(1280, 625, 40, 40, 700), the_sprites.enemy(1100, 625, 40, 40, 700),
- the_sprites.enemy(1000, 625, 40, 40, 700), the_sprites.enemy(1250, 625, 40, 40, 700),
- the_sprites.enemy(1200, 625, 40, 40, 700), the_sprites.enemy(1155, 625, 40, 40, 700)]
- self.enemyRight = the_sprites.enemy(1200, 625, 40, 40, 10)
- self.enemyLeft = the_sprites.enemy(10, 625, 40, 40, 1000)
- self.boss = the_sprites.WolfBoss(-10, 625, 40, 40, 1000)
- self.platform = [the_sprites.Platform(200, 550, 460), the_sprites.Platform(600, 549, 459), the_sprites.Platform(990, 450, 360),
- the_sprites.Platform(1500, 548, 458), the_sprites.Platform(1900, 449, 359), the_sprites.Platform(2500, 550, 461)]
- self.platform_sky = [the_sprites.Platform_sky(200, 550, 460), the_sprites.Platform_sky(600, 549, 459), the_sprites.Platform_sky(990, 450, 360),
- the_sprites.Platform_sky(1500, 548, 458)]
- self.platform_forest = [the_sprites.Platform_sky(200, 550, 460), the_sprites.Platform_sky(600, 549, 459), the_sprites.Platform_sky(990, 450, 360),
- the_sprites.Platform_sky(1500, 548, 458)]
- self.coin = [the_sprites.Coins(250, 525), the_sprites.Coins(285, 525), the_sprites.Coins(320, 525), the_sprites.Coins(355, 525), the_sprites.Coins(390, 525),
- the_sprites.Coins(1550, 525), the_sprites.Coins(1585, 525), the_sprites.Coins(1620, 525), the_sprites.Coins(1655, 525), the_sprites.Coins(1690, 525),
- the_sprites.Coins(1690, 525), the_sprites.Coins(2550, 525), the_sprites.Coins(2585, 525), the_sprites.Coins(2620, 525), the_sprites.Coins(2655, 525),
- the_sprites.Coins(2690, 525)]
- # Coins for Platform_sky
- self.coin_sky = [the_sprites.Coins(250, 525), the_sprites.Coins(285, 525), the_sprites.Coins(320, 525), the_sprites.Coins(355, 525), the_sprites.Coins(390, 525),
- the_sprites.Coins(1550, 525), the_sprites.Coins(1585, 525), the_sprites.Coins(1620, 525), the_sprites.Coins(1655, 525), the_sprites.Coins(1690, 525),
- the_sprites.Coins(1690, 525)]
- self.coin_forest = [the_sprites.Coins(250, 525), the_sprites.Coins(285, 525), the_sprites.Coins(320, 525), the_sprites.Coins(355, 525), the_sprites.Coins(390, 525),
- the_sprites.Coins(1550, 525), the_sprites.Coins(1585, 525), the_sprites.Coins(1620, 525), the_sprites.Coins(1655, 525), the_sprites.Coins(1690, 525),
- the_sprites.Coins(1690, 525), the_sprites.Coins(2550, 490), the_sprites.Coins(2585, 500), the_sprites.Coins(2620, 510), the_sprites.Coins(2655, 500),
- the_sprites.Coins(2690, 490)]
- self.coordinates = 0
- self.theGround = the_sprites.Ground_lvl1(0, 725)
- self.theGround2 = the_sprites.Ground_lvl2(0, 725)
- self.theGround3 = the_sprites.Ground_lvl3(0, 725)
- self.clock = pygame.time.Clock()
- self.CameraX = 0
- self.CameraY = 0
- self.noBorderCross = True
- self.noCameraCross = True
- self.bullets = []
- self.running = True
- self.tunnel = the_sprites.Tunnel(4390, 600)
- self.tunnel_sky = the_sprites.Tunnel(2200, 600)
- self.facing = 0
- self.landed = True
- self.enterGame = pygame.image.load('startorclose.png')
- # Coins for Platform
- self.showMenu = pygame.image.load('MainMenu.png')
- self.stopScreen = False
- self.gameOver = False
- self.r = 0
- self.d = 0
- self.g = 0
- self.i = 0
- self.j = 0
- self.k = 0
- self.v = self.returnEnemyL()
- # ----------------------------------------------------------------------------------------------------------------------
- def events(self):
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- self.running = False
- pygame.quit()
- if event.type == pygame.USEREVENT + 1:
- if self.CameraX == 5500:
- self.r = random.randrange(0, 2)
- if self.i != 24:
- if self.r == 0:
- settings.objects.append(self.enemy_rushR2[self.i])
- self.i += 1
- print("INSIDE R0")
- if self.r == 1:
- self.v = self.returnEnemyL()
- settings.objectsL.append(self.v)
- self.i += 1
- print("INSIDE R1")
- else:
- self.CameraX = 5510
- # ----------------------------------------------------------------------------------------------------------------------
- def hit_or_not(self):
- if self.level == 1:
- # Checking Collision of Enemy and Hero
- self.enemyCollide(self.hero, self.enemy)
- # Checking Collision of Platform and Hero
- self.obstacleHit_or_not(self.hero, self.platform)
- if self.groundHit_or_not(self.hero, self.theGround):
- self.hero.y = 625
- # Checking Collision of Coins and Hero
- self.coinsHit_or_not(self.hero, self.coin)
- if self.tunnelCollision(self.hero, self.tunnel):
- self.level = 2
- # Statements for colliding bullets with enemies
- self.bulletCollision(self.enemy)
- elif self.level == 2:
- self.enemyCollide(self.hero, self.enemy_sky)
- # Checking Collision of Platform_sky and Hero
- self.obstacleHit_or_not(self.hero, self.platform_sky)
- if self.groundHit_or_not(self.hero, self.theGround2):
- self.hero.y = 625
- # Checking Collision of Coin_Sky and Hero
- self.coinsHit_or_not(self.hero, self.coin_sky)
- if self.tunnelCollision(self.hero, self.tunnel_sky):
- self.level = 3
- # Statements for colliding bullets with enemies on lvl 2
- self.bulletCollision(self.enemy_sky)
- elif self.level == 3:
- self.enemyCollide(self.hero, self.enemy_forest)
- self.enemyCollide(self.hero, self.enemy_rushR)
- self.enemyCollide_2(self.hero, self.boss)
- self.enemyCollide(self.hero, self.enemy_rushR2)
- if self.groundHit_or_not(self.hero, self.theGround3):
- self.hero.y = 625
- # Checking Collision of Platform_sky and Hero
- self.obstacleHit_or_not(self.hero, self.platform_forest)
- # Checking Collision of Coin_Sky and Hero
- self.coinsHit_or_not(self.hero, self.coin_forest)
- # Statements for colliding bullets with enemies on lvl 2
- self.bulletCollision(self.enemy_forest)
- self.bulletCollision(self.enemy_rushR)
- self.bulletCollision(self.enemy_rushR2)
- #self.bulletCollision_2(self.enemyLeft)
- self.bulletCollision_2(self.v)
- self.bulletCollision_2(self.boss)
- # ----------------------------------------------------------------------------------------------------------------------
- def movements(self):
- keys = pygame.key.get_pressed()
- if keys[pygame.K_r]:
- if self.hero.left:
- self.facing = -1
- else:
- self.facing = 1
- if self.level != 3:
- if len(self.bullets) < 1:
- self.bullets.append(the_sprites.projectile(round(self.hero.x + self.hero.width), round(self.hero.y + self.hero.height), 6, (0, 0, 0), self.facing, 5))
- else:
- if len(self.bullets) < 1:
- self.bullets.append(
- the_sprites.projectile(round(self.hero.x + self.hero.width), round(self.hero.y + self.hero.height), 6,
- (0, 0, 0), self.facing, 1))
- if keys[pygame.K_LEFT] and self.hero.x > self.hero.velocity:
- self.hero.standing = False
- self.hero.x -= self.hero.velocity
- self.hero.left = True
- self.hero.right = False
- self.noBorderCross = True
- # ----------------------------------------------------------------------------------------------------------------------
- self.hero.rect.x = self.hero.x
- self.hero.rect.y = self.hero.y
- # ----------------------------------------------------------------------------------------------------------------------
- if self.level == 1:
- self.updatePlatformRect(self.platform)
- elif self.level == 2:
- self.updatePlatformRect(self.platform_sky)
- elif self.level == 3:
- self.updatePlatformRect(self.platform_forest)
- # ----------------------------------------------------------------------------------------------------------------------
- elif keys[pygame.K_RIGHT] and self.hero.x < settings.BG_WIDTH - self.hero.velocity - self.hero.width:
- self.hero.standing = False
- if self.noBorderCross:
- self.hero.x += self.hero.velocity
- if self.noCameraCross:
- if self.level == 1:
- # Moving Coins
- self.updateCoinPos(self.coin)
- # Moving Platforms
- self.updatePlatformPos(self.platform)
- self.updateEnemyPos(self.enemy)
- self.updateTunnelPos(self.tunnel)
- self.updateGroundPos(self.theGround)
- # For moving enemies dead bodies
- for e in self.enemy:
- if e.enemyDead:
- e.x += self.hero.velocity
- self.updateCoinPos(self.coin_sky)
- if self.hero.x >= settings.startScrollingPos:
- self.noBorderCross = False
- self.coordinates = 3700
- # Updating rect of Platform
- self.updatePlatformRect(self.platform)
- # Updating rect of Tunnel
- self.updateTunnelRect(self.tunnel)
- # Updating rect of Coin
- self.updateCoinRect(self.coin)
- self.updateGroundRect(self.theGround)
- # ----------------------------------------------------------------------------------------------------------------------
- elif self.level == 2:
- # Moving Coins
- self.updateCoinPos(self.coin_sky)
- # Moving Platform_sky
- self.updatePlatformPos(self.platform_sky)
- self.updateEnemyPos(self.enemy_sky)
- self.updateTunnelPos(self.tunnel_sky)
- self.updateGroundPos(self.theGround2)
- # For moving enemies dead bodies in lvl 2
- for e in self.enemy_sky:
- if e.enemyDead:
- e.x += self.hero.velocity
- if self.hero.x >= settings.startScrollingPos_sky:
- self.noBorderCross = False
- self.coordinates = 2200
- # Updating rect of Platform_sky
- self.updatePlatformRect(self.platform_sky)
- self.updateTunnelRect(self.tunnel_sky)
- self.updateCoinRect(self.coin_sky)
- self.updateGroundRect(self.theGround2)
- # ----------------------------------------------------------------------------------------------------------------------
- elif self.level == 3:
- # Moving Coins
- self.updateCoinPos(self.coin_forest)
- # Moving Platform_sky
- self.updatePlatformPos(self.platform_forest)
- self.updateGroundPos(self.theGround3)
- self.updateEnemyPos(self.enemy_forest)
- self.updateEnemyPos(self.enemy_rushR)
- self.updateEnemyPos(self.enemy_rushR2)
- # For moving enemies dead bodies in lvl 2
- for e in self.enemy_forest:
- if e.enemyDead:
- e.x += self.hero.velocity
- if self.hero.x >= settings.startScrollingPos_forest:
- self.noBorderCross = False
- self.coordinates = 5500
- # Updating rect of Platform_sky
- self.updatePlatformRect(self.platform_forest)
- self.updateCoinRect(self.coin_forest)
- self.updateGroundRect(self.theGround3)
- # ----------------------------------------------------------------------------------------------------------------------
- self.CameraX += self.hero.velocity
- # ----------------------------------------------------------------------------------------------------------------------
- # 6500 is Camera Position Limit - If increased Camera will behave abnormally
- if self.CameraX >= self.coordinates:
- self.noCameraCross = False
- if self.hero.x < 890 - self.hero.velocity - self.hero.width:
- self.hero.x += self.hero.velocity
- #self.level = 2
- # for o in self.objects:
- # o.x -= 10
- # if o.x < o.width * -1:
- # self.objects.pop(self.objects.index(o))
- self.hero.left = False
- self.hero.right = True
- # Updating rect of Hero
- self.hero.rect.x = self.hero.x
- self.hero.rect.y = self.hero.y
- # ----------------------------------------------------------------------------------------------------------------------
- elif keys[pygame.K_r]:
- self.hero.shoot = True
- else:
- self.hero.standing = True
- self.hero.shoot = False
- self.hero.walkCount = 0
- if not self.hero.isJump:
- if keys[pygame.K_SPACE]:
- self.hero.isJump = True
- self.hero.walkCount = 0
- else:
- if self.hero.jumpCount >= -10:
- self.hero.y -= (self.hero.jumpCount * abs(self.hero.jumpCount)) * 1
- self.hero.jumpCount -= 1
- self.hero.rect.x = self.hero.x
- self.hero.rect.y = self.hero.y
- else:
- self.hero.jumpCount = 10
- self.hero.isJump = False
- self.hero.rect.x = self.hero.x
- self.hero.rect.y = self.hero.y
- # Method to draw Sprites
- # ----------------------------------------------------------------------------------------------------------------------
- def redrawGameWindow(self):
- # This statement continuously keep drawing background image according to the camera value
- settings.screen.blit(settings.bg, (0 - self.CameraX, 0 - self.CameraY))
- #print("1 - ", self.CameraX)
- text = self.font.render('Score: ' + str(self.score), 1, (0, 0, 0))
- settings.screen.blit(text, (390, 10))
- # Drawing hero and enemies on Screen
- self.hero.draw(settings.screen)
- self.tunnel.draw(settings.screen)
- self.theGround.draw(settings.screen)
- # Drawing Enemies
- for e in self.enemy:
- e.draw(settings.screen)
- # Drawing Platforms(Tiles)
- for p in self.platform:
- p.draw(settings.screen)
- # Drawing Coins for Platform
- for c in self.coin:
- c.draw(settings.screen)
- for bullet in self.bullets:
- bullet.draw(settings.screen)
- pygame.display.flip()
- def redrawGameWindow2(self):
- # This statement continuously keep drawing background image according to the camera value
- settings.screen.blit(settings.bg_2, (0 - self.CameraX, 0 - self.CameraY))
- #print("2 - ", self.CameraX)
- text = self.font.render('Score: ' + str(self.score), 1, (0, 0, 0))
- settings.screen.blit(text, (390, 10))
- # Drawing hero and enemies on Screen
- self.hero.draw(screen)
- self.tunnel_sky.draw(screen)
- self.theGround2.draw(screen)
- # Drawing Enemies
- for e in self.enemy_sky:
- e.draw(settings.screen)
- # Drawing Platform_sky(Tiles)
- for p in self.platform_sky:
- p.draw(settings.screen)
- # Drawing Coins for Platform
- for c in self.coin_sky:
- c.draw(settings.screen)
- for bullet in self.bullets:
- bullet.draw(settings.screen)
- pygame.display.flip()
- def redrawGameWindow3(self):
- settings.screen.blit(settings.bg_3, (0 - self.CameraX, 0 - self.CameraY))
- print("3 - ", self.CameraX)
- text = self.font.render('Score: ' + str(self.score), 1, (0, 0, 0))
- settings.screen.blit(text, (390, 10))
- # Drawing hero and enemies on Screen
- self.hero.draw(settings.screen)
- # Drawing Enemies
- for e in self.enemy_forest:
- e.draw(settings.screen)
- if self.CameraX >= 5510:
- self.boss.draw(settings.screen)
- pygame.display.flip()
- # Drawing Platform_sky(Tiles)
- for p in self.platform_forest:
- p.draw(settings.screen)
- if self.CameraX >= 5500:
- for o in objects:
- o.draw(settings.screen)
- for o in objectsL:
- o.draw(settings.screen)
- # Drawing Coins for Platform
- for c in self.coin_forest:
- c.draw(settings.screen)
- for bullet in self.bullets:
- bullet.draw(settings.screen)
- pygame.display.flip()
- def redrawEnemies(self):
- for o in self.objects:
- o.draw(settings.screen)
- # def returnEnemyR(self):
- # coords = random.randrange(943, 1050)
- #
- # self.enemyRight = enemy(coords, 625, 40, 40, 10)
- #
- # self.enemyCollide(self.hero, self.enemyRight)
- # self.bulletCollision_2(self.enemyRight)
- # self.updateEnemyPos_2(self.enemyRight)
- #
- # return self.enemyRight
- def returnEnemyL(self):
- coords = random.randrange(943, 1050)
- self.enemyLeft = the_sprites.enemy(10, 625, 40, 40, coords)
- #self.enemyCollide_2(self.hero, self.enemyLeft)
- #self.bulletCollision_2(self.enemyLeft)
- #self.updateEnemyPos_2(self.enemyLeft)
- return self.enemyLeft
- # Method to update Rect of Platform and Coins
- # ----------------------------------------------------------------------------------------------------------------------
- def updatePlatformRect(self, platform):
- for p in platform:
- p.rect.x = p.x
- p.rect.y = p.y
- def updateCoinRect(self, coin):
- for c in coin:
- c.rect.x = c.x
- c.rect.y = c.y
- def updateTunnelRect(self, tunnel):
- tunnel.rect.x = tunnel.x
- tunnel.rect.y = tunnel.y
- def updateGroundRect(self, ground):
- ground.rect.x = ground.x
- ground.rect.y = ground.y
- #Methods to update position
- # ----------------------------------------------------------------------------------------------------------------------
- def updatePlatformPos(self, platform):
- for p in platform:
- p.x += -self.hero.velocity
- def updateCoinPos(self, coin):
- for c in coin:
- c.x += -self.hero.velocity
- def updateEnemyPos(self, enemy):
- for e in enemy:
- e.path[0] -= self.hero.velocity
- e.path[1] -= self.hero.velocity
- def updateEnemyPos_2(self, enemy):
- enemy.path[0] -= self.hero.velocity
- enemy.path[1] -= self.hero.velocity
- def updateTunnelPos(self, tunnel):
- tunnel.x += -self.hero.velocity
- def updateGroundPos(self, ground):
- ground.x += -self.hero.velocity
- # ----------------------------------------------------------------------------------------------------------------------
- # Methods for Checking Collision
- # ----------------------------------------------------------------------------------------------------------------------
- def enemyCollide(self, hero, enemy):
- for e in enemy:
- # This statement will start reacting when player touches enemy
- if hero.hitbox[1] < e.hitbox[1] + e.hitbox[3] and hero.hitbox[1] + hero.hitbox[3] > e.hitbox[1]:
- if hero.hitbox[0] + hero.hitbox[2] > e.hitbox[0] and hero.hitbox[0] < e.hitbox[0] + e.hitbox[2]:
- mainMenu(True)
- def enemyCollide_2(self, hero, enemy):
- # This statement will start reacting when player touches enemy
- if hero.hitbox[1] < enemy.hitbox[1] + enemy.hitbox[3] and hero.hitbox[1] + hero.hitbox[3] > enemy.hitbox[1]:
- if hero.hitbox[0] + hero.hitbox[2] > enemy.hitbox[0] and hero.hitbox[0] < enemy.hitbox[0] + enemy.hitbox[2]:
- mainMenu(True)
- def groundHit_or_not(self, hero, ground):
- hit = pygame.sprite.collide_rect(hero, ground)
- return hit
- # This function works for Tunnel Only
- def tunnelCollision(self, hero, tunnel):
- hit = pygame.sprite.collide_rect(hero, tunnel)
- return hit
- # This function works for Blocks only
- def obstacleHit_or_not(self, hero, platform):
- for p in platform:
- hit = pygame.sprite.collide_rect(hero, p)
- if hit:
- #print("ITS HITTING")
- hero.y = p.pos
- print("hero.y - ", hero.y)
- elif hero.y == p.pos:
- #print("HITTING INSIDE OF 450")
- if self.level == 1:
- self.landingCords = 625
- elif self.level == 2:
- self.landingCords = 650
- elif self.level == 3:
- self.landingCords = 625
- while True:
- hero.y += 5
- if hero.y >= self.landingCords:
- break
- # This function works for Blocks only
- def obstacleHit_or_not_2(self, hero, platform):
- hit = pygame.sprite.collide_rect(hero, platform)
- if hit:
- #print("ITS HITTING")
- hero.y = p.pos
- print("hero.y - ", hero.y)
- elif hero.y == p.pos:
- #print("HITTING INSIDE OF 450")
- if self.level == 1:
- self.landingCords = 625
- elif self.level == 2:
- self.landingCords = 650
- elif self.level == 3:
- self.landingCords = 625
- while True:
- hero.y += 5
- if hero.y >= self.landingCords:
- break
- # This function works for Coins Only
- def coinsHit_or_not(self, hero, thecoin):
- for c in thecoin:
- if not c.coinTouched:
- hit = pygame.sprite.collide_rect(hero, c)
- if hit:
- c.hit()
- self.score += 2
- # This is the function used for detecting bullets that will hit enemy
- def bulletCollision(self, enemy):
- for e in enemy:
- for bullet in self.bullets:
- if bullet.y - bullet.radius < e.hitbox[1] + e.hitbox[3] and bullet.y + bullet.radius > e.hitbox[1]:
- if bullet.x + bullet.radius > e.hitbox[0] and bullet.x - bullet.radius < e.hitbox[0] + e.hitbox[2]:
- e.hit()
- self.score += 1
- self.bullets.pop(self.bullets.index(bullet))
- # This won't let bullets travel more than screen size i.e 928
- if bullet.x < 928 and bullet.x > 0:
- bullet.x += bullet.vel
- else:
- try:
- self.bullets.pop(self.bullets.index(bullet))
- except ValueError:
- print("OUT OF BOUND")
- # This is the function used for detecting bullets that will hit enemy
- def bulletCollision_2(self, enemy):
- for bullet in self.bullets:
- if bullet.y - bullet.radius < enemy.hitbox[1] + enemy.hitbox[3] and bullet.y + bullet.radius > enemy.hitbox[1]:
- if bullet.x + bullet.radius > enemy.hitbox[0] and bullet.x - bullet.radius < enemy.hitbox[0] + enemy.hitbox[2]:
- enemy.hit()
- self.score += 1
- self.bullets.pop(self.bullets.index(bullet))
- # This won't let bullets travel more than screen size i.e 928
- if bullet.x < 928 and bullet.x > 0:
- bullet.x += bullet.vel
- else:
- try:
- self.bullets.pop(self.bullets.index(bullet))
- except ValueError:
- print("OUT OF BOUND")
- # ----------------------------------------------------------------------------------------------------------------------
- gameOver = False
- onTouch = True
- run = True
- def runGame(theGame):
- theGame.clock.tick(settings.FPS)
- # This function consists code for Events
- theGame.events()
- # This function consists code from enemy hit events
- theGame.hit_or_not()
- # This function consists code for player movements
- theGame.movements()
- if theGame.level == 1:
- # This function consists code for drawing the sprites over the screen
- theGame.redrawGameWindow()
- elif theGame.level == 2:
- theGame.redrawGameWindow2()
- elif theGame.level == 3:
- for o in settings.objects:
- o.x -= 1.4
- if o.x < o.width * -1:
- settings.objects.pop(settings.objects.index(o))
- for o in settings.objectsL:
- if o.x >= 300:
- settings.objectsL.pop(settings.objectsL.index(o))
- theGame.redrawGameWindow3()
- def readyGame(run, gameOver):
- game = Game(1)
- game2 = Game(2)
- game3 = Game(3)
- while run:
- if game.level == 1:
- runGame(game)
- keys = pygame.key.get_pressed()
- if keys[pygame.K_x]:
- game = Game(1)
- elif keys[pygame.K_ESCAPE]:
- run = False
- if gameOver:
- runGame(game)
- elif game.level == 2:
- runGame(game2)
- keys = pygame.key.get_pressed()
- if keys[pygame.K_x]:
- game2 = Game(2)
- elif keys[pygame.K_ESCAPE]:
- run = False
- if gameOver:
- runGame(game)
- elif game.level == 3:
- runGame(game3)
- keys = pygame.key.get_pressed()
- if keys[pygame.K_x]:
- game3 = Game(3)
- elif keys[pygame.K_ESCAPE]:
- run = False
- if gameOver:
- runGame(game)
- run2 = True
- con = True
- def mainMenu(con):
- if con:
- while run2:
- settings.screen.blit(settings.menu, (0, 0))
- pygame.display.flip()
- keys = pygame.key.get_pressed()
- if keys[pygame.K_SPACE]:
- con = False
- readyGame(True, False)
- elif keys[pygame.K_ESCAPE]:
- pygame.quit()
- pygame.event.pump()
- mainMenu(True)
- pygame.quit()
Add Comment
Please, Sign In to add comment