def multi_bullet(group_a, group_b, pos, fire_direction, game_modevalue): bullet_1 = sprites.Bullet( pos, dictionaries.VELOCITY_VALUES['Convert']['Multi_Bullet'] [game_modevalue][1], dictionaries.MEDIA['red_bullet'], 'Bullet') bullet_2 = sprites.Bullet( pos, dictionaries.VELOCITY_VALUES['Convert']['Multi_Bullet'] [game_modevalue][2], dictionaries.MEDIA['orange_bullet'], 'Bullet') bullet_3 = sprites.Bullet( pos, dictionaries.VELOCITY_VALUES['Convert']['Multi_Bullet'] [game_modevalue][3], dictionaries.MEDIA['yellow_bullet'], 'Bullet') bullet_4 = sprites.Bullet( pos, dictionaries.VELOCITY_VALUES['Convert']['Multi_Bullet'] [game_modevalue][4], dictionaries.MEDIA['green_bullet'], 'Bullet') bullet_5 = sprites.Bullet( pos, dictionaries.VELOCITY_VALUES['Convert']['Multi_Bullet'] [game_modevalue][5], dictionaries.MEDIA['blue_bullet'], 'Bullet') bullet_6 = sprites.Bullet( pos, dictionaries.VELOCITY_VALUES['Convert']['Multi_Bullet'] [game_modevalue][6], dictionaries.MEDIA['purple_bullet'], 'Bullet') bullet_7 = sprites.Bullet( pos, dictionaries.VELOCITY_VALUES['Convert']['Multi_Bullet'] [game_modevalue][7], dictionaries.MEDIA['white_bullet'], 'Bullet') bullet_8 = sprites.Bullet( pos, dictionaries.VELOCITY_VALUES['Convert']['Multi_Bullet'] [game_modevalue][8], dictionaries.MEDIA['grey_bullet'], 'Bullet') group_a.add(bullet_1, bullet_2, bullet_3, bullet_4, bullet_5, bullet_6, bullet_7, bullet_8) group_b.add(bullet_1, bullet_2, bullet_3, bullet_4, bullet_5, bullet_6, bullet_7, bullet_8) dictionaries.MEDIA['multi_shoot_sound'].play()
def monsterPatternFire(self, monst): monst.firing = False dam,img,degreeNum,speed,angleFactor,speedFactor,interval,switchBackPoints = monst.fire_data img = load_image_trans(img,-1) x,y,width,height = monst.rect x += (width / 2) y += (height / 2) ctrpt = x,y degrees = 0 oldDegrees = 0 passedDegrees = switchBackPoints[0] rotation = -90 + switchBackPoints[0] while degrees < degreeNum: if (degrees - oldDegrees) >= switchBackPoints[1]: passedDegrees = switchBackPoints[0] rotation = -90 + switchBackPoints[0] speed += 2 oldDegrees = degrees data = [dam,speed,angleFactor,speedFactor,False] bullet = sprites.Bullet(ctrpt,img,passedDegrees,data) self.monster_bullet_group.add(bullet) degrees += interval passedDegrees += interval rotation -= interval
def playerPatternFire(self): imgID,interval,degreeNum,switchBackPoints,damFactor,speed,angleFactor,speedFactor,homing,spin = self.hitbox.charge_bullet_data self.hitbox.charge = 0 x,y,width,height = self.hitbox.rect x += (width / 2) y -= (height / 2) ctrpt = x,y img = load_image_trans(imgID,-1) dam = (self.hitbox.dam * damFactor * self.hitbox.stamina) / 100 degrees = 0 passedDegrees = switchBackPoints[0] oldDegrees = 0 rotation = -90 - switchBackPoints[0] while degrees <= degreeNum: if (oldDegrees + 360) == degrees: speed += speedFactor oldDegrees = degrees if (degrees - oldDegrees) == switchBackPoints[1]: passedDegrees = switchBackPoints[0] rotation = -switchBackPoints[0] - 90 oldDegrees = degrees speed += speedFactor image = pygame.transform.rotate(img,rotation) data = [dam,speed,angleFactor,speedFactor,True,homing,spin] bullet = sprites.Bullet(ctrpt,image,passedDegrees,data) self.player_bullet_group.add(bullet) degrees += interval passedDegrees += interval rotation -= interval
def ranged_attack(self): if self.current_energy >= 20: self.current_energy -= 20 self.current_frame = 0 self.ranged_attacking = True direction = None if self.facing_right: direction = 'right' else: direction = 'left' bullet = sprites.Bullet(self.game, self.hit_rect.center, direction) self.game.all_sprites.add(bullet) self.game.bullets.add(bullet)
def render(self, DISPLAYSURF): # Drawing/updating code DISPLAYSURF.fill(WHITE) self.cat_list.draw(DISPLAYSURF) #draws cat self.enemy_list.draw(DISPLAYSURF) # draws enemy self.enemy_list.update() # makes enemies move self.bullet_list.draw(DISPLAYSURF) # draws the bullets self.bullet_list.update() # ensures the bullets move self.ebullet_list.draw(DISPLAYSURF) self.ebullet_list.update() DISPLAYSURF.blit(self.font.render(self.text, True, BLACK), (32, 48)) DISPLAYSURF.blit( self.font.render(str(self.score).rjust(4), True, BLACK), (DISPLAYSURF.get_width() - 50, 48)) # Enemy Creation Code. Everytime it updates it creates an enemy and ebullet enemy = sprites.Enemy() enemybullet = sprites.EnemyBullet() if random.randint(1, 10) <= self.level: enemy.spawn(DISPLAYSURF) self.enemy_list.add(enemy) enemybullet.spawn(enemy.getmidx(), enemy.getmidy()) self.ebullet_list.add(enemybullet) #enemy reshooting code for enemy in self.enemy_list: if enemy.shoot(DISPLAYSURF) == True: enemybullet.spawn(enemy.getmidx(), enemy.getmidy()) self.ebullet_list.add(enemybullet) # Bullet creation code. Everytime it updates it'll create a bullet bullet = sprites.Bullet() bullet.spawn(self.mousex, self.mousey) self.bullet_list.add(bullet) enemy.remove(self.enemy_list, self.cat_list, DISPLAYSURF) enemybullet.remove(self.ebullet_list, self.cat_list, DISPLAYSURF) self.score = bullet.remove(self.bullet_list, self.enemy_list, self.score) pass
def fire(self): if self.hitbox.stamina == 0: self.hitbox.firing = True self.hitbox.stamina_out = False else: self.hitbox.stamina -= 0 if self.hitbox.stamina < 100: self.stamina = 100 x,y,width,height = self.hitbox.rect x += (width / 2) ctrpt = x,y img = load_image_trans(self.hitbox.bullet_data[0],-1) spray_angle_a,spray_angle_b = self.hitbox.bullet_data[2] if self.hitbox.bullet_data[1] == True: flight_angle = random.randint(spray_angle_a,spray_angle_b) flight_angle = -flight_angle else: flight_angle = -90 rotation = -flight_angle - 90 img = pygame.transform.rotate(img,rotation) bullet = sprites.Bullet(ctrpt,img,flight_angle,self.hitbox.bullet_data[3:]) self.player_bullet_group.add(bullet)
def main(): # Game Variables all_sprites = pygame.sprite.Group() bullets1 = pygame.sprite.Group() bullets2 = pygame.sprite.Group() player1 = s.Player((35, 35), bullets2, (8, 0), v.P1Char, all_sprites) player2 = s.Player((465, 465), bullets1, (-8, 0), v.P2Char, all_sprites) clock = pygame.time.Clock() textstatic1 = v.font2.render('Player 1', True, v.white) textstatic2 = v.font2.render('Player 2', True, v.white) textstatic3 = v.font2.render('Escape to leave', True, v.white) textstatic4 = v.font2.render('Enter to restart', True, v.white) # Conditionals loop = True time = True onStart = True onEnd = True confirm = False # Integers vel = 8 vel_reset = 0 timer = 30 dt = clock.tick(60) / 1000 textlocal = (222, 520) while loop: for event in pygame.event.get(): if event.type == pygame.QUIT: v.superloop = False loop = False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_e and player1.toggle == False: bullet = s.Bullet( player1.rect.center, pygame.math.Vector2(player1.fire_direction), player1.bullet_image) m.MEDIA['shoot'].play() bullets1.add(bullet) all_sprites.add(bullet) if event.key == pygame.K_SPACE and player2.toggle == False: bullet = s.Bullet( player2.rect.center, pygame.math.Vector2(player2.fire_direction), player2.bullet_image) m.MEDIA['shoot'].play() bullets2.add(bullet) all_sprites.add(bullet) if event.key == pygame.K_d and player1.toggle == False: player1.vel.x = 5 player1.fire_direction = pygame.math.Vector2(vel, 0) if event.key == pygame.K_a and player1.toggle == False: player1.vel.x = -5 player1.fire_direction = pygame.math.Vector2(-vel, 0) if event.key == pygame.K_s and player1.toggle == False: player1.vel.y = 5 player1.fire_direction = pygame.math.Vector2(0, vel) if event.key == pygame.K_w and player1.toggle == False: player1.vel.y = -5 player1.fire_direction = pygame.math.Vector2(0, -vel) if event.key == pygame.K_RIGHT and player2.toggle == False: player2.vel.x = 5 player2.fire_direction = pygame.math.Vector2(vel, 0) if event.key == pygame.K_LEFT and player2.toggle == False: player2.vel.x = -5 player2.fire_direction = pygame.math.Vector2(-vel, 0) if event.key == pygame.K_DOWN and player2.toggle == False: player2.vel.y = 5 player2.fire_direction = pygame.math.Vector2(0, vel) if event.key == pygame.K_UP and player2.toggle == False: player2.vel.y = -5 player2.fire_direction = pygame.math.Vector2(0, -vel) if event.type == pygame.KEYUP: if event.key == pygame.K_d: player1.vel.x = vel_reset if event.key == pygame.K_a: player1.vel.x = vel_reset if event.key == pygame.K_s: player1.vel.y = vel_reset if event.key == pygame.K_w: player1.vel.y = vel_reset if event.key == pygame.K_RIGHT: player2.vel.x = vel_reset if event.key == pygame.K_LEFT: player2.vel.x = vel_reset if event.key == pygame.K_DOWN: player2.vel.y = vel_reset if event.key == pygame.K_UP: player2.vel.y = vel_reset keys = pygame.key.get_pressed() if onStart: m.MEDIA['fight'].play() m.MEDIA['music'].play() onStart = False if keys[pygame.K_TAB] and not confirm and onEnd: player1.toggle = True player2.toggle = True confirm = True time = False for bullet in bullets1: bullet.toggle = True for bullet in bullets2: bullet.toggle = True pygame.mixer.pause() m.MEDIA['pause'].play() elif keys[pygame.K_LSHIFT] and confirm: player1.toggle = False player2.toggle = False confirm = False time = True for bullet in bullets1: bullet.toggle = False for bullet in bullets2: bullet.toggle = False pygame.mixer.unpause() m.MEDIA['pause'].play() elif keys[pygame.K_ESCAPE] and confirm: v.superloop = False loop = False elif keys[pygame.K_RETURN] and confirm: m.MEDIA['music'].stop() loop = False if time: timer -= dt txt = v.font.render(str(round(timer, 1)), True, f.get('time', timer)) if timer <= 0: player1.toggle = True player2.toggle = True for bullet in bullets1: bullet.toggle = True for bullet in bullets2: bullet.toggle = True m.MEDIA['die'].play() m.MEDIA['music'].stop() time = False onEnd = False textlocal = (190, 530) txt = v.font.render('Times Up!', True, v.grey) if not time and keys[pygame.K_ESCAPE]: v.superloop = False loop = False elif not time and keys[pygame.K_RETURN] and not confirm: m.MEDIA['music'].stop() loop = False if player1.health == 0: txt = v.font.render('Player 2 Wins!', True, player2.color) textlocal = (155, 530) time = False onEnd = False m.MEDIA['music'].stop() if keys[pygame.K_ESCAPE] and not confirm: v.superloop = False loop = False elif keys[pygame.K_RETURN] and not confirm: loop = False # Player 2 Outcome if player2.health == 0: txt = v.font.render('Player 1 Wins!', True, player1.color) textlocal = (155, 530) time = False onEnd = False m.MEDIA['music'].stop() if keys[pygame.K_ESCAPE] and not confirm: v.superloop = False loop = False if keys[pygame.K_RETURN] and not confirm: loop = False # Draw Outcome if player1.health == 0 and player2.health == 0: txt = v.font.render('Draw!', True, v.grey) textlocal = (210, 530) time = False onEnd = False m.MEDIA['music'].stop() if keys[pygame.K_ESCAPE] and not confirm: v.superloop = False loop = False elif keys[pygame.K_RETURN] and not confirm: loop = False # Drawing all_sprites.update() m.screen.fill(v.black) m.screen.blit(m.MEDIA['wall'], (0, 0)) m.screen.blit(f.get('hp1', player1), (20, 530)) m.screen.blit(f.get('hp2', player2), (380, 530)) m.screen.blit(txt, textlocal) m.screen.blit(textstatic1, (19, 515)) m.screen.blit(textstatic2, (429, 515)) all_sprites.draw(m.screen) if not onEnd: m.screen.blit(textstatic3, (395, 10)) m.screen.blit(textstatic4, (10, 10)) if confirm: m.screen.blit(m.MEDIA['paused'], (154, 165)) pygame.display.flip() clock.tick(60)
def gameplay(): # creating screen and clock objects screen = pg.display.set_mode((WIDTH, HEIGHT)) clock = pg.time.Clock() # sprite groups orated here all_sprites = pg.sprite.Group() enemy_sprites = pg.sprite.Group() blast_sprites = pg.sprite.Group() # player sprite created here player = sprites.Player() all_sprites.add(player) # enemy sprite created here for x in range(10): enemy = sprites.Enemy() enemy_sprites.add(enemy) all_sprites.add(enemy) global game_score game_score = 0 background_image1 = pg.image.load('field background.jpg') click_sound = pg.mixer.Sound("8-bit-laser.aiff") playing = True while playing: #--- Main event loop for event in pg.event.get(): if event.type == pg.QUIT or ( event.type == pg.KEYDOWN and event.key == pg.K_ESCAPE): #if user clicked close started = False #Flag that user is done and exit loop if event.type == pg.KEYDOWN and event.key == pg.K_SPACE: marshmellow = sprites.Bullet() marshmellow.rect.bottom = player.rect.top marshmellow.rect.centerx = player.rect.centerx blast_sprites.add(marshmellow) all_sprites.add(marshmellow) click_sound.play() screen.fill(BLACK) #set screen background color screen.blit(background_image1, [0, 0]) #text = font.render("Score: " + str(score), True, RED) #update all_sprites.update() hits = pg.sprite.spritecollide(player, enemy_sprites, False) if hits: playing = False hits2 = pg.sprite.groupcollide(blast_sprites, enemy_sprites, True, True) for hit in hits2: enemy = sprites.Enemy() enemy_sprites.add(enemy) all_sprites.add(enemy) game_score += 10 # --- DRAWING CODE--- ############################################################################################### scoredisplay(game_score) all_sprites.draw(screen) #screen.blit(text, [10, 10]) ##################################################################################################################### #waits to display the screen until the program has finished drawing, # then "flips" the graphics drawn to the screen pg.display.flip() # Limit framerate to (FPS) frames per second clock.tick(FPS)
running = True # game loop control variable power_up_funcs = [ sprites.Player.increase_HP, sprites.Player.activate_shield ] # list for to-do fucntions # initialize pygame and create window player = sprites.Player() # have to use this because, otherwise, for the first SPACE key pressing, the newest_bullet is not defined yet. newest_bullet = sprites.Bullet(0, 0) # set the music !!! pygame.mixer.music.play(loops=-1) # just repeat it # Game loop while running: # keep loop running at the right speed clock.tick(FPS) # Process input (events) for event in pygame.event.get(): # check for closing window if event.type == pygame.QUIT: running = False else: pass
def shoot(self): self.hasShot = True self.bullet = sprites.Bullet(self.game, self, self.entity, self.color, self.start, self.vector, self.magnitude, self.damage)
pass elif event.type == MOUSEMOTION: mousex, mousey = event.pos cat.update(mousex, mousey) elif event.type == MOUSEBUTTONUP: pass elif event.type == MOUSEBUTTONDOWN: pass # Enemy Creation Code. Everytime it updates it creates an enemy enemy = sprites.Enemy() enemy.spawn() enemy_list.add(enemy) # Bullet creation code. Everytime it updates it'll create a bullet bullet = sprites.Bullet() bullet.spawn(mousex, mousey) bullet_list.add(bullet) # Removes Bullet sprite when it goes off the screen for bullet in bullet_list: hit_list = pygame.sprite.spritecollide(bullet, enemy_list, True) for enemy in hit_list: bullet_list.remove(bullet) enemy_list.remove(enemy) if bullet.rect.y <= -10: bullet_list.remove(bullet) for enemy in enemy_list: hit_list = pygame.sprite.spritecollide(enemy, cat_list, True)
def fire (self, dir): self.fireState = True if self.gun == "Flamethrower": fire = sprites.Bullet(dataFiles.explosionSIm, self.rect.center, dir, self.damage, self.bulletLife, self.bulletStr, self.gun) self.bulletSprites.add(fire) elif self.gun == "Shotgun": dirx = dir[0] diry = dir[1] dir1x = dir[0] dir1y = dir[1] dir2x = dir[0] dir2y = dir[1] dir1x += .1 dir1y += .1 radians = math.atan2 (diry, dirx) angle = radians * 180 / math.pi angle += 180 print angle, "ANGLE ORIGINAL" print dirx, diry, "ORIGINAL VECTOR" radians = math.atan2 (dir1y, dirx) angle2 = radians * 180 / math.pi angle2 += 180 print angle2, "ANGLE 2" print dir1x, dir1y, "ORIGINAL VECTOR" bullet = sprites.Bullet(dataFiles.bulletIm, self.rect.center, dir, self.damage, self.bulletLife, self.bulletStr, self.gun) self.bulletSprites.add(bullet) if dir[1] > 0 and dir[1] > dir[0] and dir[0] > -.7: "up" dir1x -= .1 dir2x += .1 if dir[1] > 0 and dir[0] > 0 and dir[1] > dir [0]: "up right" dir1y -= .1 dir2x += .1 elif dir[0] > 0 and dir[0] > dir[1]: "right" dir1y += .1 dir2y -= .1 elif dir[0] < 0 and dir[0] < dir[1]: "left" dir1y -= .1 dir2y += .1 elif dir[1] < 0 and dir[1] < dir[0]: "down" dir1x += .1 dir2x -= .1 dir1 = (dir1x, dir1y) dir2 = (dir2x, dir2y) bullet = sprites.Bullet(dataFiles.bulletIm, self.rect.center, dir1, self.damage, self.bulletLife, self.bulletStr, self.gun) self.bulletSprites.add(bullet) bullet = sprites.Bullet(dataFiles.bulletIm, self.rect.center, dir2, self.damage, self.bulletLife, self.bulletStr, self.gun) self.bulletSprites.add(bullet) elif self.gun == "Pistol": bullet = sprites.Bullet(dataFiles.bulletIm, self.rect.center, dir, self.damage, self.bulletLife, self.bulletStr, self.gun) self.bulletSprites.add(bullet) elif self.gun == "MP5": bullet = sprites.Bullet(dataFiles.bulletIm, self.rect.center, dir, self.damage, self.bulletLife, self.bulletStr, self.gun) self.bulletSprites.add(bullet) elif self.gun == "Bazooka": rocket = sprites.Bullet(dataFiles.rocketIm, self.rect.center, dir, self.damage, self.bulletLife, self.bulletStr, self.gun) self.bulletSprites.add(rocket) self.fireSnd.play() self.ammo -= 1 return self.bulletSprites
def main(): # Game Variables ABIL = { 'Blue': functions.big_bullet, 'Orange': functions.big_bullet, 'Green': functions.split_bullet, 'Yellow': functions.split_bullet, 'Red': functions.beam, 'Purple': functions.beam, 'Grey': functions.reverse_bullet, 'White': functions.reverse_bullet, 'Rainbow': functions.multi_bullet } all_sprites = pygame.sprite.Group() bullets_o = pygame.sprite.Group() bullets_t = pygame.sprite.Group() clock = pygame.time.Clock() TEXTS1 = globals.FONT_SMALL.render( 'Player 1', True, dictionaries.PLAYER_MEDIA[globals.playero_charvalue]['Color']) TEXTS2 = globals.FONT_SMALL.render( 'Player 2', True, dictionaries.PLAYER_MEDIA[globals.playert_charvalue]['Color']) TEXTS3 = globals.FONT_SMALL.render('Escape to leave', True, globals.WHITE) TEXTS4 = globals.FONT_SMALL.render('Enter to restart', True, globals.WHITE) # Using G.MODE, selects attributes to use for game game_music = dictionaries.MODE_VALUES[globals.game_modevalue]['Music'] timer = dictionaries.MODE_VALUES[globals.game_modevalue]['Timer'] player_velocity = dictionaries.MODE_VALUES[ globals.game_modevalue]['Player_Velocity'] bullet_velocity = dictionaries.MODE_VALUES[ globals.game_modevalue]['Bullet_Velocity'] player_o = sprites.RectPlayer((35, 35), bullets_t, (bullet_velocity, 0), globals.playero_charvalue, ABIL[globals.playero_charvalue], all_sprites) player_t = sprites.RectPlayer((465, 465), bullets_o, (-bullet_velocity, 0), globals.playert_charvalue, ABIL[globals.playert_charvalue], all_sprites) player_o.health = dictionaries.MODE_VALUES[ globals.game_modevalue]['Health'] player_t.health = dictionaries.MODE_VALUES[ globals.game_modevalue]['Health'] cooldown_o = dictionaries.MODE_VALUES[globals.game_modevalue]['Cooldown'] cooldown_t = dictionaries.MODE_VALUES[globals.game_modevalue]['Cooldown'] # Bools loop = True time = True ability_o = False ability_t = False time_o = True time_t = True on_start = True on_end = False confirm = False draw = False # Integers velocity_reset = 0 dt = clock.tick(60) / 1000 text_location = (222, 520) while loop: keys = pygame.key.get_pressed() for event in pygame.event.get(): if event.type == pygame.QUIT: globals.superloop = False loop = False # Keymap elif event.type == pygame.KEYDOWN: if event.key == pygame.K_f and not player_o.toggle: BULLET = sprites.Bullet(player_o.rect.center, (player_o.fire_direction), player_o.bullet_image, 'Bullet') bullets_o.add(BULLET) all_sprites.add(BULLET) dictionaries.MEDIA['shoot_sound'].play() if event.key == pygame.K_SPACE and not player_t.toggle: BULLET = sprites.Bullet(player_t.rect.center, (player_t.fire_direction), player_t.bullet_image, 'Bullet') bullets_t.add(BULLET) all_sprites.add(BULLET) dictionaries.MEDIA['shoot_sound'].play() if event.key == pygame.K_e and not player_o.toggle and ability_o: player_o.ability(bullets_o, all_sprites, player_o.rect.center, (player_o.fire_direction), *player_o.params) time_o = True ability_o = False cooldown_o = dictionaries.MODE_VALUES[ globals.game_modevalue]['Cooldown'] if event.key == pygame.K_RCTRL and not player_t.toggle and ability_t: player_t.ability(bullets_t, all_sprites, player_t.rect.center, (player_t.fire_direction), *player_t.params) time_t = True ability_t = False cooldown_t = dictionaries.MODE_VALUES[ globals.game_modevalue]['Cooldown'] if event.key == pygame.K_d and not player_o.toggle and player_o.vel.x == 0: player_o.vel.x = player_velocity player_o.fire_direction = (bullet_velocity, 0) if event.key == pygame.K_a and not player_o.toggle and player_o.vel.x == 0: player_o.vel.x = -player_velocity player_o.fire_direction = (-bullet_velocity, 0) if event.key == pygame.K_s and not player_o.toggle and player_o.vel.y == 0: player_o.vel.y = player_velocity player_o.fire_direction = (0, bullet_velocity) if event.key == pygame.K_w and not player_o.toggle and player_o.vel.y == 0: player_o.vel.y = -player_velocity player_o.fire_direction = (0, -bullet_velocity) if event.key == pygame.K_RIGHT and not player_t.toggle and player_t.vel.x == 0: player_t.vel.x = player_velocity player_t.fire_direction = (bullet_velocity, 0) if event.key == pygame.K_LEFT and not player_t.toggle and player_t.vel.x == 0: player_t.vel.x = -player_velocity player_t.fire_direction = (-bullet_velocity, 0) if event.key == pygame.K_DOWN and not player_t.toggle and player_t.vel.y == 0: player_t.vel.y = player_velocity player_t.fire_direction = (0, bullet_velocity) if event.key == pygame.K_UP and not player_t.toggle and player_t.vel.y == 0: player_t.vel.y = -player_velocity player_t.fire_direction = (0, -bullet_velocity) if event.type == pygame.KEYUP: if event.key == pygame.K_d: player_o.vel.x = velocity_reset if event.key == pygame.K_a: player_o.vel.x = velocity_reset if event.key == pygame.K_s: player_o.vel.y = velocity_reset if event.key == pygame.K_w: player_o.vel.y = velocity_reset if event.key == pygame.K_RIGHT: player_t.vel.x = velocity_reset if event.key == pygame.K_LEFT: player_t.vel.x = velocity_reset if event.key == pygame.K_DOWN: player_t.vel.y = velocity_reset if event.key == pygame.K_UP: player_t.vel.y = velocity_reset # Code that runs on first iteration, runs music and the "FIGHT!" announcer but only once if on_start: dictionaries.MEDIA['fight_sound'].play() game_music.play() on_start = False # On TAB keypress, all game functions cease and pause screen appears until LSHIFT/ESC/ENTER is pressed # ESC: Game leaves, both superloop and loop are declared false as the game ends # ENTER: Restarts, only loop ends, restarting the game # LSHIFT: Continues operation of game if keys[pygame.K_TAB] and not confirm and not on_end: confirm = True time = False time_o = False time_t = False for sprite in all_sprites: sprite.toggle = True pygame.mixer.pause() dictionaries.MEDIA['pause_sound'].play() elif keys[pygame.K_LSHIFT] and confirm: confirm = False time = True time_o = True time_t = True for sprite in all_sprites: sprite.toggle = False pygame.mixer.unpause() dictionaries.MEDIA['pause_sound'].play() elif keys[pygame.K_ESCAPE] and confirm: globals.superloop = False loop = False elif keys[pygame.K_RETURN] and confirm: game_music.stop() loop = False # Time code, subtracts timer and cooldown, detects when time is @0 and does according actions, and contains more action code if time: timer -= dt txt = dictionaries.TIMER_DICT[timer < 10][1].render( str(round(timer, 1)), True, dictionaries.TIMER_DICT[timer < 10][0]) if timer <= 0: for sprite in all_sprites: sprite.toggle = True game_music.stop() time = False time_o = False time_t = False on_end = True text_location = (190, 530) txt = globals.FONT_BIG.render('Times Up!', True, globals.GREY) dictionaries.MEDIA['die_sound'].play() if not time and keys[pygame.K_ESCAPE]: globals.superloop = False loop = False elif not time and keys[pygame.K_RETURN] and not confirm: game_music.stop() loop = False if time_o: cooldown_o -= dt if cooldown_o <= 0: time_o = False ability_o = True if time_t: cooldown_t -= dt if cooldown_t <= 0: time_t = False ability_t = True # Outcome code, when health of a player(s) is at 0, code is run that shows outcome on timer area, stops time, and also contains key actions if player_o.health <= 0 and not draw: txt = globals.FONT_BIG.render('Player 2 Wins!', True, player_t.color) text_location = (155, 530) time = False time_o = False time_t = False on_end = True game_music.stop() if keys[pygame.K_ESCAPE] and not confirm: globals.superloop = False loop = False elif keys[pygame.K_RETURN] and not confirm: loop = False if globals.mem_activate: globals.mem.append(1) print(globals.mem) if globals.mem == globals.mem_ideal: pygame.display.set_caption('Regg fitte') if player_t.health <= 0 and not draw: txt = globals.FONT_BIG.render('Player 1 Wins!', True, player_o.color) text_location = (155, 530) time = False time_o = False time_t = False on_end = True game_music.stop() if keys[pygame.K_ESCAPE] and not confirm: globals.superloop = False loop = False elif keys[pygame.K_RETURN] and not confirm: loop = False if globals.mem_activate: globals.mem.append(0) print(globals.mem) if globals.mem == globals.mem_ideal: pygame.display.set_caption('Regg fitte') if player_o.health <= 0 and player_t.health <= 0: txt = globals.FONT_BIG.render('Draw!', True, globals.GREY) text_location = (210, 530) time = False time_o = False time_t = False on_end = True draw = True game_music.stop() if keys[pygame.K_ESCAPE] and not confirm: globals.superloop = False loop = False elif keys[pygame.K_RETURN] and not confirm: loop = False if globals.mem_activate: globals.mem.append(' ') print(globals.mem) if globals.mem == globals.mem_ideal: pygame.display.set_caption('Regg fitte') all_sprites.update() # Drawing code, draws media, hp bars, text, sprites, etc. globals.screen.fill(globals.BLACK) globals.screen.blit(dictionaries.MEDIA['wall'], (0, 0)) globals.screen.blit( pygame.transform.flip(dictionaries.HP_MEDIA[player_o.health], True, False), (20, 530)) globals.screen.blit(dictionaries.HP_MEDIA[player_t.health], (380, 530)) globals.screen.blit(txt, text_location) globals.screen.blit(TEXTS1, (19, 515)) globals.screen.blit(TEXTS2, (429, 515)) DRAWPARAMS1 = [ functions.get_cooldown_img(globals.game_modevalue, cooldown_o), (100, 515) ] DRAWPARAMS2 = [ functions.get_cooldown_img(globals.game_modevalue, cooldown_t), (380, 515) ] globals.screen.blit(*DRAWPARAMS1) globals.screen.blit(*DRAWPARAMS2) all_sprites.draw(globals.screen) if on_end: globals.screen.blit(TEXTS3, (395, 10)) globals.screen.blit(TEXTS4, (10, 10)) if confirm: globals.screen.blit(dictionaries.MEDIA['paused'], (154, 165)) pygame.display.flip() clock.tick(60)
def big_bullet(group_a, group_b, pos, vel, img, type_of): vel = dictionaries.VELOCITY_VALUES['Convert']['Big_Bullet'][vel] _big_bullet = sprites.Bullet(pos, vel, img, type_of) group_a.add(_big_bullet) group_b.add(_big_bullet) dictionaries.MEDIA['big_shoot_sound'].play()
def _add_bullets(self, bullets): for bullet in bullets: sprite = sprites.Bullet(entity=bullet) self.bullet_sprites.add(sprite)