Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- from the_sprites import *
- from settings import *
- pygame.init()
- class Game(pygame.sprite.Sprite):
- def __init__(self):
- super().__init__()
- self.clock = pygame.time.Clock()
- self.CameraX = 0
- self.CameraY = 0
- self.font = pygame.font.SysFont('comicsans', 30, True, True)
- self.score = 0
- self.noBorderCross = True
- self.noCameraCross = True
- self.bullets = []
- self.running = True
- self.hero = thePlayer(45, 625, 40, 40)
- self.enemy = [enemy(400, 625, 40, 40, 850), enemy(1450, 625, 40, 40, 1900), enemy(1000, 625, 40, 40, 1500),
- enemy(800, 625, 40, 40, 1300), enemy(1900, 625, 40, 40, 2400), enemy(2300, 625, 40, 40, 2900),
- enemy(2700, 625, 40, 40, 3400), enemy(3200, 625, 40, 40, 3900), enemy(3900, 625, 40, 40, 4200),
- enemy(3500, 625, 40, 40, 4290)]
- self.platform = [Platform(200, 550, 460), Platform(600, 549, 459), Platform(990, 450, 360),
- Platform(1500, 548, 458), Platform(1900, 449, 359), Platform(2500, 550, 457)]
- self.facing = 0
- self.landed = True
- self.enterGame = pygame.image.load('startorclose.png')
- # Coins for Platform
- self.coin = [Coins(250, 525), Coins(285, 525), Coins(320, 525), Coins(355, 525), Coins(390, 525),
- Coins(1550, 525), Coins(1585, 525), Coins(1620, 525), Coins(1655, 525), Coins(1690, 525),
- Coins(1690, 525), Coins(2550, 525), Coins(2585, 525), Coins(2620, 525), Coins(2655, 525),
- Coins(2690, 525)]
- self.showMenu = pygame.image.load('MainMenu.png')
- self.stopScreen = False
- self.gameOver = False
- # ----------------------------------------------------------------------------------------------------------------------
- def events(self):
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- self.running = False
- pygame.quit()
- # ----------------------------------------------------------------------------------------------------------------------
- def hit_or_not(self):
- # 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)
- # ----------------------------------------------------------------------------------------------------------------------
- # Checking Collision of Coins and Hero
- self.coinsHit_or_not(self.hero, self.coin)
- # ----------------------------------------------------------------------------------------------------------------------
- # Statements for colliding bullets with enemies
- self.bulletCollision(self.enemy)
- # ----------------------------------------------------------------------------------------------------------------------
- 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 len(self.bullets) < 1:
- self.bullets.append(projectile(round(self.hero.x + self.hero.width), round(self.hero.y + self.hero.height), 6, (0, 0, 0), self.facing))
- 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
- # ----------------------------------------------------------------------------------------------------------------------
- self.updatePlatformRect(self.platform)
- # ----------------------------------------------------------------------------------------------------------------------
- elif keys[pygame.K_RIGHT] and self.hero.x < BG_WIDTH - self.hero.velocity - self.hero.width:
- self.hero.standing = False
- if self.noBorderCross:
- self.hero.x += self.hero.velocity
- if self.noCameraCross:
- # Moving Coins
- self.updateCoinPos(self.coin)
- # ----------------------------------------------------------------------------------------------------------------------
- # Moving Platforms
- self.updatePlatformPos(self.platform)
- # ----------------------------------------------------------------------------------------------------------------------
- # Moving Camera
- self.CameraX += self.hero.velocity
- # ----------------------------------------------------------------------------------------------------------------------
- self.updateEnemyPos(self.enemy)
- # ----------------------------------------------------------------------------------------------------------------------
- # For moving enemies dead bodies
- for e in self.enemy:
- if e.enemyDead:
- e.x += self.hero.velocity
- if self.hero.x >= startScrollingPos:
- self.noBorderCross = False
- # 6500 is Camera Position Limit - If increased Camera will behave abnormally
- if self.CameraX >= 3700:
- self.noCameraCross = False
- if self.hero.x < 890 - self.hero.velocity - self.hero.width:
- self.hero.x += self.hero.velocity
- 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
- # ----------------------------------------------------------------------------------------------------------------------
- # Updating rect of Platform
- self.updatePlatformRect(self.platform)
- # ----------------------------------------------------------------------------------------------------------------------
- # Updating rect of Coin
- self.updateCoinRect(self.coin)
- # ----------------------------------------------------------------------------------------------------------------------
- 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)) * 0.5
- 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
- screen.blit(bg, (0 - self.CameraX, 0 - self.CameraY))
- text = self.font.render('Score: ' + str(self.score), 1, (0, 0, 0))
- screen.blit(text, (390, 10))
- # Drawing hero and enemies on Screen
- self.hero.draw(screen)
- # Drawing Enemies
- for e in self.enemy:
- e.draw(screen)
- # Drawing Platforms(Tiles)
- for p in self.platform:
- p.draw(screen)
- # Drawing Coins for Platform
- for c in self.coin:
- c.draw(screen)
- for bullet in self.bullets:
- bullet.draw(screen)
- pygame.display.flip()
- # 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
- #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
- # ----------------------------------------------------------------------------------------------------------------------
- # 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]:
- readyGame(True, True)
- # 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")
- while True:
- hero.y += 5
- if hero.y >= 625:
- 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")
- # ----------------------------------------------------------------------------------------------------------------------
- gameOver = False
- onTouch = True
- run = True
- def runGame(theGame):
- theGame.clock.tick(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()
- # This function consists code for drawing the sprites over the screen
- theGame.redrawGameWindow()
- def readyGame(run, gameOver):
- game = Game()
- while run:
- runGame(game)
- keys = pygame.key.get_pressed()
- if keys[pygame.K_x]:
- game = Game()
- elif keys[pygame.K_ESCAPE]:
- run = False
- pygame.event.pump()
- if gameOver:
- runGame(game)
- readyGame(True, False)
- pygame.quit()
Add Comment
Please, Sign In to add comment