def run(r, dt): player = getPlayer(r) for currentArrow in r["arrows"]: newX, newY = Arrow.live(currentArrow, dt) if isFree(r, newX, newY): Entity.setPosition(currentArrow, newX, newY) else: r["arrows"].remove(currentArrow) # Si une entité (Mob ou Joueur) a été touché, il faut lui enlever de la vie hurtedEntity = getEntityByPosition(r, newX, newY) if hurtedEntity != -1: health = Entity.getHealth(hurtedEntity) resistance = Entity.getResistance(hurtedEntity) newHealth = health - (Arrow.getDamage(currentArrow)/resistance) Entity.setHealth(hurtedEntity, newHealth) r["arrows"].remove(currentArrow) for currentEntity in r["entity"]: if Entity.getType(currentEntity) == "player": newX, newY = Player.live(currentEntity, dt) if Entity.getType(currentEntity) == "ghost": # Le ghost lance des flèches sur le joueur: if random.randint(0, 100) < 2: launchArrow(r, currentEntity) newX, newY = Mob.live(currentEntity, player, 3, dt) if Entity.getType(currentEntity) == "guardian": newX, newY = Mob.live(currentEntity, player, 6, dt) # Le guardian lance des flèches sur le joueur: if random.randint(0, 100) < 5: launchArrow(r, currentEntity) if Entity.getType(currentEntity) == "boss": newX, newY = Mob.live(currentEntity, player, 12, dt) # Le boss peut lancer des fleches de maniere random if random.randint(0, 100) < 20: launchArrow(r, currentEntity) # Déplacement autorisé s'il n'y pas d'obstacle ou de mob / joueur (ni de coffres) if isFree(r, newX, newY) and getEntityByPosition(r, newX, newY, currentEntity) == -1 and getChestByPosition(r, newX, newY) == -1: Entity.setPosition(currentEntity, newX, newY) # Despawn du monstre quand sa vie tombe à 0 if Entity.getHealth(currentEntity) <= 0 and Entity.getType(currentEntity) != "player": r["entity"].remove(currentEntity) # Si on a tué le Boss, alors on a gagné if Entity.getHealth(currentEntity) <= 0 and Entity.getType(currentEntity) == "boss": Entity.setMaxHealth(player, Entity.getMaxHealth(player) + 50) Entity.setHealth(player, Entity.getMaxHealth(player)) Entity.setStrength(player, Entity.getStrength(player) + 0.30) Entity.setResistance(player, Entity.getResistance(player) + 0.20) return "win" return ""
def get_game_objects(self): list_blocks = [] list_obstacles = [] count_blocks = 0 count_obstacles = 0 arr = Arrow(ARROW_DIR) start = Block(START_DIR, type_block=TYPE_5) end = Block(END_DIR, type_block=TYPE_5) for i in range(DIM): for j in range(DIM): if self.map_data[i][j] > 0 and self.map_data[i][j] < 100: list_blocks.append( Block(BLOCKS_DIR[self.map_data[i][j] - 1], type_block=self.map_data[i][j] - 1)) count_blocks += 1 self.map_data[i][j] = count_blocks elif self.map_data[i][j] < 0: list_obstacles.append( Block(OBSTACLES_DIR[-self.map_data[i][j] - 1])) count_obstacles += 1 self.map_data[i][j] = -count_obstacles elif self.map_data[i][j] == 100: arr.set_map_pos((i, j)) elif self.map_data[i][j] == 200: start.map_pos = (i, j) elif self.map_data[i][j] == 300: end.map_pos = (i, j) return list_blocks, list_obstacles, arr, start, end
def add_arrow(self, i, j, diag, up, left, selected): """ Arrow list is created as a list of Arrow objects, containing: selected cell co-ordinates, source co-ordinates and value in selected cell """ if diag == selected: self.arrows.append( Arrow((i, j), (i - 1, j - 1), self.matrix[i - 1, j - 1])) self.G.add_edge((i, j), (i - 1, j - 1), weight=self.matrix[i - 1, j - 1]) if up == selected: if i == 2 and j == 2: self.arrows.append(Arrow((1, 2), (1, 1), self.matrix[1, 2])) self.G.add_edge((1, 2), (1, 1), weight=self.matrix[1, 2]) self.arrows.append(Arrow((i, j), (i, j - 1), self.matrix[i, j - 1])) self.G.add_edge((i, j), (i, j - 1), weight=self.matrix[i, j - 1]) if left == selected: if i == 2 and j == 2: self.arrows.append(Arrow((2, 1), (1, 1), self.matrix[2, 1])) self.G.add_edge((2, 1), (1, 1), weight=self.matrix[2, 1]) self.arrows.append(Arrow((i, j), (i - 1, j), self.matrix[i - 1, j])) self.G.add_edge((i, j), (i - 1, j), weight=self.matrix[i - 1, j])
def findArrows(self): arrow1, arrow1_area = self.loadModelImage(ARROW1, TRAFFIC_MIN1, TRAFFIC_MAX1,0) arrow2, arrow2_area = self.loadModelImage(ARROW2, TRAFFIC_MIN1, TRAFFIC_MAX1,1) arrows = [] for contour in self.traffic_contour: if ((cv2.contourArea(contour) <= 1.5*arrow1_area and cv2.contourArea(contour) >= 0.8*arrow1_area and cv2.matchShapes(contour,arrow1[0], 3,0.0) < 1) or (cv2.contourArea(contour) <= 1.7*arrow2_area and cv2.contourArea(contour) >= 0.2*arrow2_area and cv2.matchShapes(contour, arrow2[0],1,0.0) < 5)): moment = cv2.moments(contour) cx = int(moment['m10']/moment['m00']) cy = int(moment['m01']/moment['m00']) (x,y),(MA,ma),angle = cv2.fitEllipse(contour) arrow = Arrow.Arrow(cx, cy, angle) #for test rect = cv2.minAreaRect(contour) box = cv2.boxPoints(rect) box = np.int0(box) cv2.drawContours(self.image,[box],0,(0,0,255),2) #[X, Y, W, H] = cv2.boundingRect(contour) #cv2.rectangle(self.image, (X,Y), (X+W, Y+H), (255, 0, 0), 2) self.image[cy, cx] = [255, 0, 0] print(angle) arrows.append(arrow) plt.imshow(self.image,cmap='gray') plt.show() return arrows
def show(r): # Affichage du fond for y in range(0, len(r["background"])): for x in range(0, len(r["background"][y])): Utils.goto(x + 2, y + 2) Utils.write(r["background"][y][x] + "\n") # Affichage des coffres for currentChest in r["chests"]: x, y = Chest.getPosition(currentChest) Utils.goto(x + 2, y + 2) Utils.write("C") # Affichage des entités for currentEntity in r["entity"]: Entity.show(currentEntity) # Affichage des projectiles for currentArrow in r["arrows"]: Arrow.show(currentArrow)
def show(r): # Affichage du fond for y in range(0, len(r["background"])): for x in range(0, len(r["background"][y])): Utils.goto(x+2, y+2) Utils.write(r["background"][y][x]+"\n") # Affichage des coffres for currentChest in r["chests"]: x, y = Chest.getPosition(currentChest) Utils.goto(x+2, y+2) Utils.write("C") # Affichage des entités for currentEntity in r["entity"]: Entity.show(currentEntity) # Affichage des projectiles for currentArrow in r["arrows"]: Arrow.show(currentArrow)
def launchArrow(r, entity): arrow = Arrow.create() ax, ay = Entity.getPosition(entity) evx, evy = Entity.getDirection(entity) ax, ay = ax + evx, ay + evy Arrow.setPosition(arrow, ax, ay) Arrow.setSpeed(arrow, evx*60, evy*30) Arrow.setDamage(arrow, Entity.getDamage(entity)*Entity.getStrength(entity)) r["arrows"].append(arrow)
def launchArrow(r, entity): arrow = Arrow.create() ax, ay = Entity.getPosition(entity) evx, evy = Entity.getDirection(entity) ax, ay = ax + evx, ay + evy Arrow.setPosition(arrow, ax, ay) Arrow.setSpeed(arrow, evx * 60, evy * 30) Arrow.setDamage(arrow, Entity.getDamage(entity) * Entity.getStrength(entity)) r["arrows"].append(arrow)
def __init__(self, game_instance): self.all_arrows = pygame.sprite.Group() self.all_health = pygame.sprite.Group() self.all_enemies = pygame.sprite.Group() self.all_red_dots = pygame.sprite.Group() self.all_sprites_list = pygame.sprite.Group() self.all_sprites_list.add(game_instance.Princess) self.health_count = 0 self.red_dot_time = 0 ANTAL_PILE = 10 for i in range(ANTAL_PILE): a = Arrow.arrow() self.all_arrows.add(a) self.all_sprites_list.add(a)
def run(r, dt): player = getPlayer(r) for currentArrow in r["arrows"]: newX, newY = Arrow.live(currentArrow, dt) if isFree(r, newX, newY): Entity.setPosition(currentArrow, newX, newY) else: r["arrows"].remove(currentArrow) # Si une entité (Mob ou Joueur) a été touché, il faut lui enlever de la vie hurtedEntity = getEntityByPosition(r, newX, newY) if hurtedEntity != -1: health = Entity.getHealth(hurtedEntity) resistance = Entity.getResistance(hurtedEntity) newHealth = health - (Arrow.getDamage(currentArrow) / resistance) Entity.setHealth(hurtedEntity, newHealth) r["arrows"].remove(currentArrow) for currentEntity in r["entity"]: if Entity.getType(currentEntity) == "player": newX, newY = Player.live(currentEntity, dt) if Entity.getType(currentEntity) == "ghost": # Le ghost lance des flèches sur le joueur: if random.randint(0, 100) < 2: launchArrow(r, currentEntity) newX, newY = Mob.live(currentEntity, player, 3, dt) if Entity.getType(currentEntity) == "guardian": newX, newY = Mob.live(currentEntity, player, 6, dt) # Le guardian lance des flèches sur le joueur: if random.randint(0, 100) < 5: launchArrow(r, currentEntity) if Entity.getType(currentEntity) == "boss": newX, newY = Mob.live(currentEntity, player, 12, dt) # Le boss peut lancer des fleches de maniere random if random.randint(0, 100) < 20: launchArrow(r, currentEntity) # Déplacement autorisé s'il n'y pas d'obstacle ou de mob / joueur (ni de coffres) if isFree(r, newX, newY) and getEntityByPosition( r, newX, newY, currentEntity) == -1 and getChestByPosition( r, newX, newY) == -1: Entity.setPosition(currentEntity, newX, newY) # Despawn du monstre quand sa vie tombe à 0 if Entity.getHealth(currentEntity) <= 0 and Entity.getType( currentEntity) != "player": r["entity"].remove(currentEntity) # Si on a tué le Boss, alors on a gagné if Entity.getHealth(currentEntity) <= 0 and Entity.getType( currentEntity) == "boss": Entity.setMaxHealth(player, Entity.getMaxHealth(player) + 50) Entity.setHealth(player, Entity.getMaxHealth(player)) Entity.setStrength(player, Entity.getStrength(player) + 0.30) Entity.setResistance(player, Entity.getResistance(player) + 0.20) return "win" return ""
def main(): menu = True pygame.init() deathcounter = 0 textcounter = 0 fpsClock = pygame.time.Clock() message = "" windowSurfaceObj = pygame.display.set_mode((1280, 720), DOUBLEBUF) pygame.display.set_caption("William Wallace Castle Defender X-Treme 2140") soundObjectExplosion = pygame.mixer.Sound('explosion.wav') soundDecoy = pygame.mixer.Sound('decoy.wav') soundPwp = pygame.mixer.Sound('powerup.wav') soundShld = pygame.mixer.Sound('zap2.wav') desertBackground = pygame.image.load( os.path.join(os.curdir, 'desert-background.jpg')).convert_alpha() SurfaceObjLife = pygame.image.load("life.png") level = pygame.image.load(os.path.join(os.curdir, 'LEVEL.png')).convert_alpha() player = Player() ArrowList = [] missileList = [] ShieldList = [] BombList = [] PowerUpList = [] #EXPLOSION exploList = [] #Enemy variables maxEnemies = 50 enemyList = [] #Castle HP HP = 100 points = 0 if menu == True: Menu(menu, windowSurfaceObj, fpsClock, desertBackground) pygame.key.set_repeat(1, 50) playing = True gravityLimit = False soundObjectExplosion = pygame.mixer.Sound("explosion.wav") soundObjectArrow = pygame.mixer.Sound("arrow.wav") pygame.mixer.music.load("BackgroundMusic.mp3") pygame.mixer.music.play(-1) gravityLimit = False #Main Loop LifeUp = 1 while playing: windowSurfaceObj.blit(desertBackground, (0, 0)) windowSurfaceObj.blit(level, (0, 0)) mousex = player.x mousey = player.y #DRAW EXLPLOSIONS count = len(exploList) - 1 while (count >= 0): windowSurfaceObj.blit( exploList[count].images[exploList[count].image], exploList[count].rect) if (exploList[count].updateEnemyPos()): exploList.pop(count) count = count - 1 if (textcounter > 0): #print message textMessage = fontObj.render(str(message), False, pygame.Color(0, 0, 0)) windowSurfaceObj.blit( textMessage, ((1280 - textMessage.get_rect().width) / 2 * 1, 670)) textcounter -= 1 if (deathcounter > 0): if (player.Lives <= 0): player.fall() player.updatePlayerSprite(21, 1) else: player.updatePlayerSprite(20, 1) deathcounter -= 1 windowSurfaceObj.blit(player.images[player.image], player.rect) pygame.display.flip() fpsClock.tick(30) else: if (player.Lives <= 0): retry = gameOver(points, windowSurfaceObj, fpsClock, desertBackground) playing = False #Enemy code enemyGenerator(enemyList, maxEnemies, points) count = len(enemyList) - 1 while (count >= 0): windowSurfaceObj.blit( enemyList[count].images[enemyList[count].image], enemyList[count].rect) enx = enemyList[count].x eny = enemyList[count].y chance = 1 if enemyList[count].boss: chance = 5 if random.randint( 0, 100) < chance: #1% chance that an enemy shoots if enemyList[count].right: speed = -enemyList[count].speed else: speed = enemyList[count].speed tmp = random.randint(0, 100) if player.DecoyCounter > 0: playerX = player.DecoyX playerY = player.DecoyY else: playerX = player.x playerY = player.y if enemyList[count].boss: for i in range(0, 30): m = Missile(enx + random.randint(-180, 180), eny + random.randint(-180, 180), player.x + random.randint(-180, 180), player.y + random.randint(-180, 180), speed) missileList.append(m) elif tmp < 30: m = Missile(enx, eny, playerX, playerY + 20, speed) missileList.append(m) m = Missile(enx, eny, playerX, playerY, speed) missileList.append(m) m = Missile(enx, eny, playerX, playerY - 20, speed) missileList.append(m) elif tmp < 50: m = Missile(enx, eny, playerX, playerY + 20, speed) missileList.append(m) m = Missile(enx, eny, playerX, playerY, speed) missileList.append(m) m = Missile(enx, eny, playerX, playerY - 20, speed) missileList.append(m) m = Missile(enx, eny, playerX, playerY + 40, speed) missileList.append(m) m = Missile(enx, eny, playerX, playerY - 40, speed) missileList.append(m) else: missileList.append( Missile(enx, eny, playerX, playerY, speed)) if enemyList[count].updateEnemyPos(enemyList, count): HP = HP - 2 if HP < 0: HP = 0 exploList.append(Explo(enx, eny, False)) soundObjectExplosion.play() if HP == 0: retry = gameOver(points, windowSurfaceObj, fpsClock, desertBackground) playing = False exploList.append(Explo(enx, eny, False)) count = count - 1 skipFall = False for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() elif event.type == MOUSEMOTION: mousex, mousey = event.pos player.updateVector(mousex, mousey) elif event.type == MOUSEBUTTONDOWN: myx, myy = event.pos if (myx < player.x): player.updatePlayerSprite(18, 1) else: player.updatePlayerSprite(19, 1) elif event.type == MOUSEBUTTONUP: if event.button in (1, 2, 3): mousex, mousey = event.pos #if player.Arrows - 1 >= 0: if 1: arrow = Arrow(player.x, player.y + 24, mousex, mousey, player.gunmode) ArrowList.append(arrow) if player.MultiShot2: arrow = Arrow(player.x, player.y + 24, mousex, mousey + 20, player.gunmode) ArrowList.append(arrow) arrow = Arrow(player.x, player.y + 24, mousex, mousey - 20, player.gunmode) ArrowList.append(arrow) arrow = Arrow(player.x, player.y + 24, mousex, mousey + 40, player.gunmode) ArrowList.append(arrow) arrow = Arrow(player.x, player.y + 24, mousex, mousey - 40, player.gunmode) ArrowList.append(arrow) elif player.MultiShot: arrow = Arrow(player.x, player.y + 24, mousex, mousey + 30, player.gunmode) ArrowList.append(arrow) arrow = Arrow(player.x, player.y + 24, mousex, mousey - 30, player.gunmode) ArrowList.append(arrow) soundObjectArrow.play() #player.Arrows -= 1 #left, middle, right button elif event.button in (4, 5): blah = "blah" #scroll up or down elif event.type == KEYDOWN: x = 0 y = 0 if event.key == K_SPACE: if player.Repel > 0: soundShld.play() ShieldList.append(Shield(player.x, player.y)) player.Repel -= 1 #rep = pygame.image.load("pexpl1.png") #windowSurfaceObj.blit(rep,rep.get_rect()) for i in range(0, len(missileList)): missXp = missileList[i].x missYp = missileList[i].y diffX = missileList[i].x - player.x diffY = missileList[i].y - player.y if diffX != 0 and diffY != 0: missileList[i].vector = Vector( (diffX / sqrt(diffX * diffX + diffY * diffY)), (diffY / sqrt(diffX * diffX + diffY * diffY))) else: if diffX == 0: if diffY < 0: missileList[i].vector = Vector( 0, -1) elif diffY > 0: missileList[i].vector = Vector( 0, 1) elif diffY == 0: if diffX < 0: missileList[i].vector = Vector( -1, 0) elif diffX > 0: missileList[i].vector = Vector( 1, 0) else: missileList[i].vector = Vector(1, 0) missileList[i].vel = 15 if event.key == K_LSHIFT: if player.DecoyNum > 0: soundDecoy.play() player.Decoy(player.x, player.y) player.DecoyNum -= 1 if event.key == K_LEFT or event.key == K_a: x = -10 if event.key == K_RIGHT or event.key == K_d: x = 10 if event.key == K_UP or event.key == K_w: y = -.5 keystate = pygame.key.get_pressed() if keystate[pygame.locals.K_UP] or keystate[ pygame.locals.K_w]: y = -10 if keystate[pygame.locals.K_RIGHT] or keystate[ pygame.locals.K_d]: x = 10 if keystate[pygame.locals.K_LEFT] or keystate[ pygame.locals.K_a]: x = -10 #player.updatePlayerPos(x,0) if y != 0: if player.Gravity - 1 >= 0 and gravityLimit: player.jet() skipFall = True player.Gravity -= 1 else: if player.Gravity >= 20: gravityLimit = True else: gravityLimit = False if event.key == K_ESCAPE: pygame.event.post(pygame.event.Event(QUIT)) #else: x = 0 y = 0 keystate = pygame.key.get_pressed() if keystate[pygame.locals.K_UP] or keystate[pygame.locals.K_w]: y = -10 if keystate[pygame.locals.K_RIGHT] or keystate[pygame.locals.K_d]: x = 10 if keystate[pygame.locals.K_LEFT] or keystate[pygame.locals.K_a]: x = -10 if (x != 0 or y != 0): player.updatePlayerPos(x, 0) if y != 0: if player.Gravity - 1 >= 0 and gravityLimit: player.jet() skipFall = True player.Gravity -= 1 else: if player.Gravity >= 20: gravityLimit = True else: gravityLimit = False #player.updateVector(mousex,mousey) #Castle health bar pygame.draw.rect(windowSurfaceObj, pygame.Color(255, 0, 0), (540, 260, 200, 20)) pygame.draw.rect(windowSurfaceObj, pygame.Color(0, 255, 0), (540, 260, HP * 2, 20)) #Display Points fontObj = pygame.font.Font('freesansbold.ttf', 32) pointsSurfaceObj = fontObj.render("Points: " + str(points), False, pygame.Color(255, 255, 255)) windowSurfaceObj.blit(pointsSurfaceObj, (windowSurfaceObj.get_rect().width - pointsSurfaceObj.get_rect().width - 25, 25)) #Display Lives fontObj = pygame.font.Font('freesansbold.ttf', 32) livesSurfaceObj = fontObj.render("Lives:", False, pygame.Color(255, 255, 255)) windowSurfaceObj.blit(livesSurfaceObj, (300, 25)) for i in range(0, player.Lives): windowSurfaceObj.blit( SurfaceObjLife, (300 + livesSurfaceObj.get_rect().width + (i * (SurfaceObjLife.get_rect().width + 25)), 25 - SurfaceObjLife.get_rect().height / 4)) #Display Arrows and gravity decoysSurfaceObj = fontObj.render( "Decoys: " + str(player.DecoyNum), False, pygame.Color(255, 255, 255)) #arrowsSurfaceObj = fontObj.render("Arrows: " + str(player.Arrows)+"/"+str(player.ArrowsMax), False, pygame.Color(255,255,255)) #gravitySurfaceObj = fontObj.render("Anti-Gravity: ", False, pygame.Color(255,255,255)) windowSurfaceObj.blit(decoysSurfaceObj, (40, 15)) repelSurfaceObj = fontObj.render("Repels: " + str(player.Repel), False, pygame.Color(255, 255, 255)) windowSurfaceObj.blit(repelSurfaceObj, (40, 70)) pygame.draw.rect(windowSurfaceObj, pygame.Color(255, 255, 0), (20, 120, 200, 20)) pygame.draw.rect(windowSurfaceObj, pygame.Color(255, 0, 0), (20, 120, 40, 20)) pygame.draw.rect(windowSurfaceObj, pygame.Color(0, 255, 0), (20, 120, player.Gravity * 2, 20)) #windowSurfaceObj.blit(arrowsSurfaceObj, (25, 25)) #windowSurfaceObj.blit(gravitySurfaceObj, (25, arrowsSurfaceObj.get_rect().height + 50)) #player.updatePos() if not skipFall: player.fall() #Arrow Code end = len(ArrowList) i = end - 1 while i >= 0: chk = ArrowList[i].updateArrowPos() if not chk: ArrowList.pop(i) i = i - 1 else: end = len(enemyList) - 1 count = end chk = True while count >= 0: if ArrowList[i].rect.colliderect( enemyList[count].rect): if (not player.gunmode): ArrowList.pop(i) i = i - 1 enx = enemyList[count].x eny = enemyList[count].y if (enemyList[count].Hit(enemyList, count, 5)): exploList.append(Explo(enx, eny, False)) x = random.randint(0, 100) if x <= 25: tmp = PowerUp(enx, eny) PowerUpList.append(tmp) elif x > 95: b = Bomb(enx, eny) BombList.append(b) soundObjectExplosion.play() points = points + 5 if points / LifeUp >= 100: LifeUp += 1 player.Lives += 1 chk = False count -= 1 if i < 0: count = -1 if chk: ArrowObj = ArrowList[i].ArrowObj windowSurfaceObj.blit(ArrowObj, ArrowList[i].rect) i = i - 1 #Bomb Code i = len(BombList) - 1 while i >= 0: if BombList[i].rect.colliderect(player.rect): killAllEnemies(enemyList, exploList, soundObjectExplosion) #deathcounter=45 points = points + 30 if points / LifeUp >= 100: LifeUp += 1 player.Lives += 1 for i in range(0, 60): x = random.randint(0, 1280) y = random.randint(0, 720) z = random.randint(0, 1) exploList.append(Explo(x, y, z)) BombList = [] missileList = [] arrowList = [] i = -1 else: windowSurfaceObj.blit(BombList[i].image, BombList[i].rect) i = i - 1 #Missile Code end = len(missileList) i = end - 1 while i >= 0: chk = missileList[i].updateMissilePos() if not chk: missileList.pop(i) i = i - 1 else: if missileList[i].rect.colliderect(player.rect): exploList.append( Explo(missileList[i].x, missileList[i].y, True)) soundObjectExplosion.play() missileList.pop(i) player.Lives -= 1 #i = i - 1 if player.Lives <= 0 and playing == True: killAllEnemies(enemyList, exploList, soundObjectExplosion) deathcounter = 70 else: i = -1 player.ArrowsMax = 20 player.ArrowsReplRate = 0.05 missileList = [] arrowList = [] #TODO killAllEnemies(enemyList, exploList, soundObjectExplosion) deathcounter = 45 player.RapidFire = False player.MultiShot = False player.MultiShot2 = False player.gunmode = False chk = False if i < 0: count = -1 if chk: missileObj = missileList[i].missileObj windowSurfaceObj.blit(missileObj, missileList[i].rect) i = i - 1 i = len(PowerUpList) - 1 while i >= 0: PowerUpList[i].updateBoxSprite() if player.rect.colliderect(PowerUpList[i].rect): soundPwp.play() if PowerUpList[i].type == 0: player.Repel += 1 message = "Repel!" textcounter = 120 elif PowerUpList[i].type == 1: if player.MultiShot: player.MultiShot2 = True player.MultiShot = True message = "Multi Shot!" textcounter = 120 elif PowerUpList[i].type == 2: player.DecoyNum += 1 message = "Decoy!" textcounter = 120 elif PowerUpList[i].type == 3: if HP + 10 >= 100: HP = 100 else: HP += 10 message = "Castle HP restored!" textcounter = 120 elif PowerUpList[i].type == 4: player.gunmode = True message = "Piercing bullets!" textcounter = 120 soundObjectArrow = pygame.mixer.Sound("gun.wav") PowerUpList.pop(i) else: windowSurfaceObj.blit( PowerUpList[i].images[PowerUpList[i].image], PowerUpList[i].rect) i = i - 1 #check enemy detection with player i = len(enemyList) - 1 while i >= 0: if player.rect.colliderect(enemyList[i].rect): player.Lives -= 1 exploList.append( Explo(enemyList[i].x, enemyList[i].y, True)) soundObjectExplosion.play() exploList.append( Explo(enemyList[i].x, enemyList[i].y, True)) enemyList.pop(i) if player.Lives <= 0 and playing == True: killAllEnemies(enemyList, exploList, soundObjectExplosion) deathcounter = 70 else: player.ArrowsMax = 20 player.ArrowsReplRate = 0.05 killAllEnemies(enemyList, exploList, soundObjectExplosion) deathcounter = 45 missileList = [] arrowsList = [] player.RapidFire = False player.MultiShot = False player.MultiShot2 = False player.gunmode = False i = len(enemyList) i = i - 1 windowSurfaceObj.blit(player.images[player.image], player.rect) player.DecoyCounter -= 5 if player.DecoyCounter > 0: windowSurfaceObj.blit(player.images[21], (player.DecoyX, player.DecoyY)) #DRAW SHIELD count = len(ShieldList) - 1 while (count >= 0): windowSurfaceObj.blit( ShieldList[count].images[ShieldList[count].image], ShieldList[count].rect) ShieldList[count].x = player.x ShieldList[count].y = player.y if (ShieldList[count].move(0, 0)): ShieldList.pop(count) count = count - 1 #pygame.display.update() pygame.display.flip() fpsClock.tick(30) #if player.Arrows + 1 <= player.ArrowsMax: # player.ArrowsRepl += player.ArrowsReplRate # if player.ArrowsRepl >= 1.0: # player.Arrows += 1 # player.ArrowsRepl = 0.0 if player.Gravity + 1 <= 100: player.GravityRepl += .5 if player.GravityRepl >= 1.0: player.Gravity += 1 player.GravityRepl = 0.0 if retry: pygame.mixer.music.stop main() else: pygame.quit()
rpm = Rpm(canvas,winWidth/2,winHeight/4,winWidth/1.25,winHeight/4,50,"yellow","yellow",20,140,0,10000) speed = Text(canvas,winWidth/2,winHeight/4,"Helvetica",speedFontSize,"bold italic","black","137") mileage = Text(canvas,winWidth/2,(winHeight/10)*3,"Helvetica",10,"bold ","black","162.372 KM") clutch=Bar(canvas,winWidth-92,winHeight,30,-60,"blue") brake=Bar(canvas,winWidth-61,winHeight,30,-30,"red") throttle=Bar(canvas,winWidth-30,winHeight,30,-100,"green") temp1 = Circle(canvas,(winWidth/4)*1,(winHeight/2)*1,100,25,240,300,20,100,"#28cfbc",circleFontSize,"OIL T.") temp2 = Circle(canvas,(winWidth/4)*2,(winHeight/2)*1,100,25,240,300,20,100,"#28cfbc",circleFontSize,"OIL P.") temp3 = Circle(canvas,(winWidth/4)*3,(winHeight/2)*1,100,25,240,300,20,100,"#28cfbc",circleFontSize,"H2O T.") temp4 = Circle(canvas,(winWidth/4)*1,(winHeight/4)*3,100,25,240,300,20,100,"#28cfbc",circleFontSize,"H2O T.2") temp5 = Circle(canvas,(winWidth/4)*2,(winHeight/4)*3,100,25,240,300,20,100,"#28cfbc",circleFontSize,"IAT") #temp6 = Circle(canvas,(winWidth/4)*3,(winHeight/4)*3,100,25,240,300,20,100,"#28cfbc",circleFontSize,"BAT") arrowLeft=Arrow(canvas,(winWidth/3)*1,winHeight/4,0.15,"green","left") arrowRight=Arrow(canvas,(winWidth/3)*2,winHeight/4,0.15,"green","right") g = Gforce(canvas,(winWidth/4)*3,(winHeight/4)*3,125,2,1,"gray",4,"red") canvas.after(10,set,canvas,g) root.mainloop()
def game_arrow_rain(self, game_instance): red_dot_interval = 500 #Hvor hurtigt kommer de røde bolde blocks_hit_list = pygame.sprite.spritecollide(game_instance.Princess, self.all_arrows, True) health_hit_list = pygame.sprite.spritecollide(game_instance.Princess, self.all_health,True) red_dot_hit_list = pygame.sprite.spritecollide(game_instance.Princess, self.all_red_dots, False) enemy_hit_list = pygame.sprite.spritecollide(game_instance.Princess, self.all_enemies, False) for hits in blocks_hit_list: game_instance.Princess.hit() a = Arrow.arrow() self.all_arrows.add(a) self.all_sprites_list.add(a) for hits in health_hit_list: game_instance.Princess.heal(hits.amount) for hits in red_dot_hit_list: game_instance.Princess.heal(-100) self.all_red_dots.remove(hits) self.all_sprites_list.remove(hits) for hits in enemy_hit_list: #Hvad skal der ske når princessen rammer en sort prik continue if game_instance.frames % variables.fps == 0: game_instance.score = self.score + 1 text = game_instance.font.render('HP: {}'.format(game_instance.Princess.hp), True, variables.BLACK) score_text = self.font.render('Score: {}'.format(game_instance.score), True, variables.RED) if game_instance.health_count == 500: H_Box = Health.health() self.all_sprites_list.add(H_Box) self.all_health.add(H_Box) self.health_count = 0 if self.red_dot_time == red_dot_interval: red_dot = redDot.reddot(self.RED_DOT_image, variables.WHITE) red_dot.set_move(float(self.Princess.x), float(self.Princess.y)) self.all_sprites_list.add(red_dot) self.all_red_dots.add(red_dot) draw_fire = 100 self.red_dot_time = 0 else: self.red_dot_time += 1 for health in self.all_health: health.counter += 1 if health.counter == health.time: self.all_health.remove(health) self.all_sprites_list.remove(health) for enemy in self.all_enemies: enemy.move(self.Princess.x, self.Princess.y) self.health_count += 1 for sprite in self.all_sprites_list: sprite.move() for arrow in self.all_arrows: if arrow.x < 0: arrow.x = random.randrange(1000, 1500) arrow.y = random.randrange(300,680) if self.Princess.hp <= 0: return {'text': 'dead', 'score': self.score} self.frames = self.frames + 1 if self.frames % 1800 == 0: NYE_PILE = 10 for i in range(NYE_PILE): a = Arrow.arrow() self.all_arrows.add(a) self.all_sprites_list.add(a) draw_screen(self.screen, self.background_image) self.screen.blit(self.CANNON_image, [820, 20]) if self.draw_fire > 0: self.screen.blit(self.CANNON_FIRE_image, [700, -25]) self.draw_fire = self.draw_fire - 1 self.screen.blit(text,[500, 0]) self.screen.blit(score_text,[200, 0]) self.back_button.draw(self.screen) for sprite in self.all_sprites_list: sprite.draw(self.screen) return
sys.exit() if event.type == pygame.MOUSEBUTTONUP and event.button == 1: mouse = pygame.mouse.get_pos() magnitude = math.sqrt((mouse[0] - anchorPoint[0])**2 + (mouse[1] - anchorPoint[1])**2) try: a = ((float(mouse[1]) - float(anchorPoint[1])) / (float(anchorPoint[0]) - float(mouse[0]))) angle = math.atan(a) print angle except: angle = math.pi / 2 if magnitude <= 80: velocity = magnitude * 2 else: velocity = 170 if len(arrows) < 1: arrows.append(Arrow(anchorPoint, angle, velocity)) if velocity >= 0: for arrow in arrows: arrow.update() if arrow.off_screen(): arrows.pop(0) clock.tick(FPS) pygame.display.update()
def InitializeButtons(self): # Create Text Objects self.Text = pygame.font.SysFont("times", 24, True, False) self.SFXVolumeText = self.Text.render("SFX Volume", 1, (255, 255, 255)) self.MusicVolumeText = self.Text.render("Music Volume", 1, (255, 255, 255)) self.PlayTitleText = self.Text.render("Play", 1, (255, 255, 255)) self.NumDisksText = self.Text.render("Number of Disks", 1, (255, 255, 255)) # Create Slider Bar and Check Box images self.SliderBar = pygame.image.load( os.path.join(os.path.join('data', 'textures'), 'slider_bar.png')) self.CheckBoxEmpty = pygame.image.load( os.path.join(os.path.join('data', 'textures'), 'checkbox_unchecked.png')) self.CheckBoxChecked = pygame.image.load( os.path.join(os.path.join('data', 'textures'), 'checkbox_checked.png')) # Create Button Objects self.MenuButtons = [] ButtonImages = ['Quit', 'Settings', 'High Scores', 'Play'] for button in range(len(ButtonImages)): tNewButton = Button.Button( self.Screen.get_width() / 2 - 75, self.Screen.get_height() - 50 * (2 + button) - (button * 10), ButtonImages[button], 'button_standard.png', 'button_hover.png', self.ButtonHandler) self.MenuButtons.append(tNewButton) self.MenuButtons.append( Button.Button( self.Screen.get_width() / 2 + self.MenuBackgroundOverlay.get_width() / 2 - 160, self.Screen.get_height() / 2 + self.MenuBackgroundOverlay.get_height() / 2 - 60, 'Back', 'button_standard.png', 'button_hover.png', self.ButtonHandler)) self.MenuButtons.append( Button.Button( self.Screen.get_width() / 2 + self.MenuBackgroundOverlay.get_width() / 2 - 320, self.Screen.get_height() / 2 + self.MenuBackgroundOverlay.get_height() / 2 - 60, 'Save', 'button_standard.png', 'button_hover.png', self.ButtonHandler)) self.MenuButtons.append( Button.Button( self.Screen.get_width() / 2 + self.MenuBackgroundOverlay.get_width() / 2 - 160, self.Screen.get_height() / 2 + self.MenuBackgroundOverlay.get_height() / 2 - 120, 'Default', 'button_standard.png', 'button_hover.png', self.ButtonHandler)) self.MenuButtons.append( Button.Button( self.Screen.get_width() / 2 + self.GameController.UserSettings["[MusicVolume]"] - 4, self.Screen.get_height() / 2 + self.MusicVolumeText.get_height() / 2 - 7, 'Music', 'button_slider_standard.png', 'button_slider_hover.png', self.SliderHandler)) self.MenuButtons.append( Button.Button( self.Screen.get_width() / 2 + self.GameController.UserSettings["[SFXVolume]"] - 4, self.Screen.get_height() / 2 + self.SFXVolumeText.get_height() / 2 + 23, 'SFX', 'button_slider_standard.png', 'button_slider_hover.png', self.SliderHandler)) self.MenuButtons.append( Button.Button( self.Screen.get_width() / 2 + self.MenuBackgroundOverlay.get_width() / 2 - 160, self.Screen.get_height() / 2 + self.MenuBackgroundOverlay.get_height() / 2 - 120, 'Start', 'button_standard.png', 'button_hover.png', self.ButtonHandler)) self.MenuButtons.append( Button.Button( self.Screen.get_width() / 2 + self.MenuBackgroundOverlay.get_width() / 2 - 320, self.Screen.get_height() / 2 + self.MenuBackgroundOverlay.get_height() / 2 - 120, 'Reset Scores', 'button_standard.png', 'button_hover.png', self.ButtonHandler)) # Create Arros Objects self.Arrows = [] self.Arrows.append( Arrow.Arrow( self.Screen.get_width() / 2 - self.MenuBackgroundOverlay.get_width() / 2 + 30, self.Screen.get_height() / 2 + self.MenuBackgroundOverlay.get_height() / 2 - 50, 'ScoresLeft', 'arrow_left_standard.png', 'arrow_left_hover.png', 'arrow_left_gray.png', self.ArrowHandler)) self.Arrows.append( Arrow.Arrow( self.Screen.get_width() / 2 - self.MenuBackgroundOverlay.get_width() / 2 + 70, self.Screen.get_height() / 2 + self.MenuBackgroundOverlay.get_height() / 2 - 50, 'ScoresRight', 'arrow_right_standard.png', 'arrow_right_hover.png', 'arrow_right_gray.png', self.ArrowHandler)) self.Arrows.append( Arrow.Arrow( self.Screen.get_width() / 2 - self.MenuBackgroundOverlay.get_width() / 2 + 30, self.Screen.get_height() / 2 - 100, 'DisksLeft', 'arrow_left_standard.png', 'arrow_left_hover.png', 'arrow_left_gray.png', self.ArrowHandler)) self.Arrows.append( Arrow.Arrow( self.Screen.get_width() / 2 - self.MenuBackgroundOverlay.get_width() / 2 + 120, self.Screen.get_height() / 2 - 100, 'DisksRight', 'arrow_right_standard.png', 'arrow_right_hover.png', 'arrow_right_gray.png', self.ArrowHandler)) self.Arrows.append( Arrow.Arrow( self.Screen.get_width() / 2 - self.MenuBackgroundOverlay.get_width() / 2 + 30, self.Screen.get_height() / 2 - 50, 'DiskColorLeft', 'arrow_left_standard.png', 'arrow_left_hover.png', 'arrow_left_gray.png', self.ArrowHandler)) self.Arrows.append( Arrow.Arrow( self.Screen.get_width() / 2 - self.MenuBackgroundOverlay.get_width() / 2 + 180, self.Screen.get_height() / 2 - 50, 'DiskColorRight', 'arrow_right_standard.png', 'arrow_right_hover.png', 'arrow_right_gray.png', self.ArrowHandler)) # Create Check Box Objects self.CheckBoxes = [] Checks = [ 'Fullscreen:', '800x600', '1024x768', '1280x768', '1360x768', '1366x768', '1600x900' ] columns = [ self.Screen.get_width() / 2 - self.MenuBackgroundOverlay.get_width() / 2 + 30, self.Screen.get_width() / 2 ] Fullscreen = CheckBox.CheckBox( columns[0], self.Screen.get_height() / 2 - self.MenuBackgroundOverlay.get_height() / 2 + 30, Checks[0], Checks[0], self.Text) if (self.GameController.UserSettings['[Fullscreen]'] == 1): Fullscreen.Checked = True self.CheckBoxes.append(Fullscreen) resolution = str( self.GameController.UserSettings['[ScreenWidth]']) + 'x' + str( self.GameController.UserSettings['[ScreenHeight]']) count = 1 for col in range(2): for row in range(3): tCheckBox = CheckBox.CheckBox( columns[col], self.Screen.get_height() / 2 - self.MenuBackgroundOverlay.get_height() / 2 + 83 + 30 * row, Checks[count], Checks[count], self.Text) if (resolution == tCheckBox.ButtonId): tCheckBox.Checked = True self.CheckBoxes.append(tCheckBox) count += 1