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)
- self.enemy2 = enemy(1450, 625, 40, 40, 1900)
- self.platform = Platform(200, 550)
- self.platform1 = Platform(600, 550)
- self.platform2 = Platform(1000, 550)
- self.facing = 0
- self.landed = True
- self.enterGame = pygame.image.load('startorclose.png')
- self.coin = Coins(250, 525)
- self.coin1 = Coins(285, 525)
- self.coin2 = Coins(320, 525)
- self.coin3 = Coins(355, 525)
- self.coin4 = Coins(390, 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)
- self.enemyCollide(self.hero, self.enemy2)
- # Checking Collision of Platform and Hero
- self.obstacleHit_or_not(self.hero, self.platform, 460)
- self.obstacleHit_or_not(self.hero, self.platform1, 460)
- self.obstacleHit_or_not(self.hero, self.platform2, 460)
- # Checking Collision of Coins and Hero
- self.coinsHit_or_not(self.hero, self.coin)
- self.coinsHit_or_not(self.hero, self.coin1)
- self.coinsHit_or_not(self.hero, self.coin2)
- self.coinsHit_or_not(self.hero, self.coin3)
- self.coinsHit_or_not(self.hero, self.coin4)
- # One statement for enemy and other one for enemy2
- self.bulletCollision(self.enemy)
- self.bulletCollision(self.enemy2)
- 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.platform.rect.x = self.platform.x
- self.platform.rect.y = self.platform.y
- self.platform1.rect.x = self.platform1.x
- self.platform1.rect.y = self.platform1.y
- self.platform2.rect.x = self.platform2.x
- self.platform2.rect.y = self.platform2.y
- 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.coin.x += -self.hero.velocity
- self.coin1.x += -self.hero.velocity
- self.coin2.x += -self.hero.velocity
- self.coin3.x += -self.hero.velocity
- self.coin4.x += -self.hero.velocity
- # Moving Platforms
- self.platform.x += -self.hero.velocity
- self.platform1.x += -self.hero.velocity
- self.platform2.x += -self.hero.velocity
- # Moving Camera
- self.CameraX += self.hero.velocity
- self.enemy.path[0] -= self.hero.velocity
- self.enemy.path[1] -= self.hero.velocity
- self.enemy2.path[0] -= self.hero.velocity
- self.enemy2.path[1] -= self.hero.velocity
- if self.enemy.enemyDead:
- self.enemy.x += self.hero.velocity
- if self.enemy2.enemyDead:
- self.enemy2.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.platform.rect.x = self.platform.x
- self.platform.rect.y = self.platform.y
- self.platform1.rect.x = self.platform1.x
- self.platform1.rect.y = self.platform1.y
- self.platform2.rect.x = self.platform2.x
- self.platform2.rect.y = self.platform2.y
- # Updating rect of Coin
- self.coin.rect.x = self.coin.x
- self.coin.rect.y = self.coin.y
- self.coin1.rect.x = self.coin1.x
- self.coin1.rect.y = self.coin1.y
- self.coin2.rect.x = self.coin2.x
- self.coin2.rect.y = self.coin2.y
- self.coin3.rect.x = self.coin3.x
- self.coin3.rect.y = self.coin3.y
- self.coin4.rect.x = self.coin4.x
- self.coin4.rect.y = self.coin4.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)) * 0.7
- 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)
- self.enemy.draw(screen)
- self.enemy2.draw(screen)
- self.platform.draw(screen)
- self.platform1.draw(screen)
- self.platform2.draw(screen)
- self.coin.draw(screen)
- self.coin1.draw(screen)
- self.coin2.draw(screen)
- self.coin3.draw(screen)
- self.coin4.draw(screen)
- for bullet in self.bullets:
- bullet.draw(screen)
- pygame.display.flip()
- # Methods for Checking Collision
- # ----------------------------------------------------------------------------------------------------------------------
- def enemyCollide(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]:
- readyGame(True, True)
- # This function works for Blocks only
- def obstacleHit_or_not(self, hero, platform, position):
- hit = pygame.sprite.collide_rect(hero, platform)
- if hit:
- #print("ITS HITTING")
- hero.y = position
- print("hero.y - ", hero.y)
- elif hero.y == position:
- #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):
- if not thecoin.coinTouched:
- hit = pygame.sprite.collide_rect(hero, thecoin)
- if hit:
- thecoin.hit()
- self.score += 2
- # This is the function used for detecting bullets that will hit enemy
- def bulletCollision(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:
- self.bullets.pop(self.bullets.index(bullet))
- # ----------------------------------------------------------------------------------------------------------------------
- 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