Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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.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
- def hit_or_not(self):
- '''
- # This statement will start reacting when player touches enemy
- if self.hero.hitbox[1] < self.enemy.hitbox[1] + self.enemy.hitbox[3] and self.hero.hitbox[1] + self.hero.hitbox[3] > self.enemy.hitbox[1]:
- if self.hero.hitbox[0] + self.hero.hitbox[2] > self.enemy.hitbox[0] and self.hero.hitbox[0] < self.enemy.hitbox[0] + self.enemy.hitbox[2]:
- # hero.x = bricks_block.x
- # hero.y = bricks_block.y
- print("GOT HIT")
- '''
- if self.enemyHit_or_not(self.hero, self.enemy):
- self.gameOver = True
- # This statement will start reacting when player touches enemy
- if self.hero.hitbox[1] < self.enemy2.hitbox[1] + self.enemy2.hitbox[3] and self.hero.hitbox[1] + self.hero.hitbox[3] > self.enemy2.hitbox[1]:
- if self.hero.hitbox[0] + self.hero.hitbox[2] > self.enemy2.hitbox[0] and self.hero.hitbox[0] < self.enemy2.hitbox[0] + self.enemy2.hitbox[2]:
- print("GOT HIT!")
- # self.hero.hit(win)
- self.obstacleHit_or_not(self.hero, self.platform, 460)
- 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)
- # 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
- elif self.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
- def enemyHit_or_not(self, hero, enemy):
- hit = pygame.sprite.collide_rect(hero, enemy)
- '''
- if hit:
- screen.blit(self.showMenu, (0, 0))
- pygame.display.flip()
- print("Do something")
- elif not hit:
- print("Do some more work")
- '''
- return hit
- # This loop is used for detecting bullets that will hit enemy
- # One statement for enemy and other one for enemy2
- def bulletHit_or_not(self):
- for bullet in self.bullets:
- if bullet.y - bullet.radius < self.enemy.hitbox[1] + self.enemy.hitbox[3] and bullet.y + bullet.radius > self.enemy.hitbox[1]:
- if bullet.x + bullet.radius > self.enemy.hitbox[0] and bullet.x - bullet.radius < self.enemy.hitbox[0] + self.enemy.hitbox[2]:
- self.enemy.hit()
- self.score += 1
- self.bullets.pop(self.bullets.index(bullet))
- if bullet.y - bullet.radius < self.enemy2.hitbox[1] + self.enemy2.hitbox[3] and bullet.y + bullet.radius > self.enemy2.hitbox[1]:
- if bullet.x + bullet.radius > self.enemy2.hitbox[0] and bullet.x - bullet.radius < self.enemy2.hitbox[0] + self.enemy2.hitbox[2]:
- self.enemy2.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))
- 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
- 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
- # 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 >= 6500:
- 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
- # 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
- 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.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()
- # ----------------------------------------------------------------------------------------------------------------------
- run = True
- def runGame(theGame):
- while run:
- 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 bullet hit events
- theGame.bulletHit_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 startGame(run):
- game = Game()
- while run:
- runGame(game)
- keys = pygame.key.get_pressed()
- if keys[pygame.K_ESCAPE]:
- run = False
- elif keys[pygame.K_c]:
- game = Game()
- startGame(True)
- pygame.quit()
Add Comment
Please, Sign In to add comment