def moveUP(self, screen, hero_group, stairGroup): collision = pygame.sprite.groupcollide(hero_group, level.getStairGroup(), False, True) if len(collision) > 0: self.setPosition("U", 20) self.setBarNo()
def moveDown(self, screen,hero_group,stairGroup) : barList = level.Level1bars() barList.reverse() collision = pygame.sprite.groupcollide(hero_group, level.getStairGroup(),False,True) if len(collision) > 0: if self.getPosition("D") + 20 <= barList[self.__myPlayer.barNo-1] : self.setPosition("D",20) elif self.getDown() : self.setPosition("D",20) self.setBarNo()
def moveDown(self, screen, hero_group, stairGroup): barList = level.Level1bars() barList.reverse() collision = pygame.sprite.groupcollide(hero_group, level.getStairGroup(), False, True) if len(collision) > 0: if self.getPosition("D") + 20 <= barList[self.__myPlayer.barNo - 1]: self.setPosition("D", 20) elif self.getDown(): self.setPosition("D", 20) self.setBarNo()
def moveUP(self, screen,hero_group,stairGroup) : collision = pygame.sprite.groupcollide(hero_group, level.getStairGroup(),False,True) if len(collision) > 0: self.setPosition("U",20) self.setBarNo()
def main(): pygame.init() screen = pygame.display.set_mode((WIDTH,HEIGHT)) pygame.display.set_caption("DonkeyKong") # Initialize level lifes = 3 score = 0 COINS = 20 times = 0 done = False donkey = Donkey() donkey_group = pygame.sprite.GroupSingle(donkey.makeDonkey()) hero = Player() hero_group = pygame.sprite.GroupSingle(hero.makePlayer(lifes)) fireball_group = pygame.sprite.OrderedUpdates() Fireballs = [] fireballS = Fireball() fireSource = pygame.sprite.GroupSingle(fireballS.makeFireSource()) coinGroup = comLevels.genCoins(COINS) pygame.key.set_repeat(8,10) FIRETIME = pygame.USEREVENT + 1 pygame.time.set_timer(pygame.USEREVENT, 150) pygame.time.set_timer(FIRETIME, 2000) while done == False : for event in pygame.event.get() : if event.type == pygame.QUIT : done = True if event.type == pygame.KEYDOWN : if event.key == pygame.K_q or event.key == pygame.K_ESCAPE: done = True elif event.key == pygame.K_SPACE : hero.setState("JUMPING") elif event.key == pygame.K_w or event.key == pygame.K_UP : hero.moveUP(screen,hero_group,level.getStairGroup()) elif event.key == pygame.K_s or event.key == pygame.K_DOWN : hero.moveDown(screen,hero_group,level.getStairGroup()) elif event.key == pygame.K_a or event.key == pygame.K_LEFT : if hero.getState == ("JUMPING") : hero.allowLeft(True) hero.moveLeft(screen) elif event.key == pygame.K_d or event.key == pygame.K_RIGHT : if hero.getState() == "JUMPING" : hero.allowRight(True) hero.moveRigth(screen) elif event.key == pygame.K_f : for ball in Fireballs : ball.setSpeedP() elif event.key == pygame.K_b : for ball in Fireballs : ball.setSpeedN() if event.type == pygame.USEREVENT : donkey.move() donkey.chgSpeed() if event.type == FIRETIME : fireball = Fireball() fireball_group.add(fireball.makeFireball()) Fireballs.append(fireball) for ball in Fireballs : ball.moveBalls() ball.gravity() ball.onBarless() ball.killFireball() collDon = pygame.sprite.groupcollide(donkey_group,hero_group,False,True) collFire = pygame.sprite.groupcollide(fireball_group,hero_group,False,True) if len(collDon) > 0 or len(collFire) > 0 : if score >= 25 : score -= 25 else : score = 0 print "player: D " + str(hero.getPosition("D")) print "player: L " + str(hero.getPosition("L")) print "donkey: D " + str(donkey.getPosition("D")) print "donkey: L " + str(donkey.getPosition("L")) lifes = hero.playerDied(screen) pygame.display.update() hero_group = pygame.sprite.GroupSingle(hero.makePlayer(lifes)) try: time.sleep(3) except : print "key" collCoin = pygame.sprite.groupcollide(coinGroup,hero_group,True,False) if len(collCoin) > 0 : score += 20 if hero.getState() == "JUMPING": hero.printPos() print "--------" times = hero.jump(times) if times == 4: hero.setState("STANDING") hero.allowLeft(False) hero.allowRight(False) times = 0 level.selectLevel(screen,1) coinGroup.draw(screen) hero_group.draw(screen) fireSource.draw(screen) fireball_group.draw(screen) donkey_group.draw(screen) comLevels.updateLife(lifes,screen) comLevels.updateScore(score,screen) pygame.display.update() if lifes == 0: break pygame.quit() sys.exit()
def main(): pygame.init() screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("DonkeyKong") # Initialize level lifes = 3 score = 0 COINS = 20 times = 0 done = False donkey = Donkey() donkey_group = pygame.sprite.GroupSingle(donkey.makeDonkey()) hero = Player() hero_group = pygame.sprite.GroupSingle(hero.makePlayer(lifes)) fireball_group = pygame.sprite.OrderedUpdates() Fireballs = [] fireballS = Fireball() fireSource = pygame.sprite.GroupSingle(fireballS.makeFireSource()) coinGroup = comLevels.genCoins(COINS) pygame.key.set_repeat(8, 10) FIRETIME = pygame.USEREVENT + 1 pygame.time.set_timer(pygame.USEREVENT, 150) pygame.time.set_timer(FIRETIME, 2000) while done == False: for event in pygame.event.get(): if event.type == pygame.QUIT: done = True if event.type == pygame.KEYDOWN: if event.key == pygame.K_q or event.key == pygame.K_ESCAPE: done = True elif event.key == pygame.K_SPACE: hero.setState("JUMPING") elif event.key == pygame.K_w or event.key == pygame.K_UP: hero.moveUP(screen, hero_group, level.getStairGroup()) elif event.key == pygame.K_s or event.key == pygame.K_DOWN: hero.moveDown(screen, hero_group, level.getStairGroup()) elif event.key == pygame.K_a or event.key == pygame.K_LEFT: if hero.getState == ("JUMPING"): hero.allowLeft(True) hero.moveLeft(screen) elif event.key == pygame.K_d or event.key == pygame.K_RIGHT: if hero.getState() == "JUMPING": hero.allowRight(True) hero.moveRigth(screen) elif event.key == pygame.K_f: for ball in Fireballs: ball.setSpeedP() elif event.key == pygame.K_b: for ball in Fireballs: ball.setSpeedN() if event.type == pygame.USEREVENT: donkey.move() donkey.chgSpeed() if event.type == FIRETIME: fireball = Fireball() fireball_group.add(fireball.makeFireball()) Fireballs.append(fireball) for ball in Fireballs: ball.moveBalls() ball.gravity() ball.onBarless() ball.killFireball() collDon = pygame.sprite.groupcollide(donkey_group, hero_group, False, True) collFire = pygame.sprite.groupcollide(fireball_group, hero_group, False, True) if len(collDon) > 0 or len(collFire) > 0: if score >= 25: score -= 25 else: score = 0 print "player: D " + str(hero.getPosition("D")) print "player: L " + str(hero.getPosition("L")) print "donkey: D " + str(donkey.getPosition("D")) print "donkey: L " + str(donkey.getPosition("L")) lifes = hero.playerDied(screen) pygame.display.update() hero_group = pygame.sprite.GroupSingle(hero.makePlayer(lifes)) try: time.sleep(3) except: print "key" collCoin = pygame.sprite.groupcollide(coinGroup, hero_group, True, False) if len(collCoin) > 0: score += 20 if hero.getState() == "JUMPING": hero.printPos() print "--------" times = hero.jump(times) if times == 4: hero.setState("STANDING") hero.allowLeft(False) hero.allowRight(False) times = 0 level.selectLevel(screen, 1) coinGroup.draw(screen) hero_group.draw(screen) fireSource.draw(screen) fireball_group.draw(screen) donkey_group.draw(screen) comLevels.updateLife(lifes, screen) comLevels.updateScore(score, screen) pygame.display.update() if lifes == 0: break pygame.quit() sys.exit()