def shootingpaddle(self): for powerup in self.powerups: if powerup.utility == "shooting": if powerup.status: bullet1 = Bullet(self.paddle.x, self.paddle.y - 1, 1, 1) bullet2 = Bullet(self.paddle.x + self.paddle.width - 1, self.paddle.y - 1, 1, 1) self.bullets.append(bullet1) self.bullets.append(bullet2)
def shoot_bullet(): x, y = p.rect.center[0], p.rect.y if p.powerup > 0: for dx in range(-3, 4): b = Bullet(x, y, 4, dx) player_bullet_group.add(b) p.powerup -= 1 else: b = Bullet(x - 30, y, 6) player_bullet_group.add(b) b = Bullet(x + 30, y, 6) player_bullet_group.add(b) player_bullet_fx.play()
def update(self, dt, win): x = 0.0 y = 0.0 if k.kUp(): #If key up is pressed y -= self.vel * dt if k.kDown(): #If key down is pressed y += self.vel * dt if k.kLeft(): #If key left is pressed x -= self.vel * dt if k.kRight(): #If key right is pressed x += self.vel * dt #Check if mouse has been clicked mouse = k.kMouseLeft(win) if mouse != None: #print(mouse) self.bullets.append( Bullet(self.pos, win, 10, Point(mouse.getX(), mouse.getY()), 1000, self.colour)) #Update bullets and check if out of bounds for bullet in self.bullets: bullet.update(dt, win) if bullet.getAliveFlag() != True: bullet.undraw() self.bullets.remove(bullet) self.object.move(x, y) self.pos = self.object.getCenter()
def handle_input(self): super().handle_input() pygame.key.set_repeat() keys_pressed = pygame.key.get_pressed() if keys_pressed[K_LEFT] and self.ship: self.ship.rotate(-1) if keys_pressed[K_RIGHT] and self.ship: self.ship.rotate(1) if keys_pressed[K_UP] and self.ship: self.ship.accelerate(0.05) if keys_pressed[K_DOWN] and self.ship: self.ship.accelerate(0) #TODO: Set to (0) to stop the ship instantly with down-key AKA EASYMODE. if keys_pressed[K_SPACE] and self.ship: if len(self.bullets) >= 15: del self.bullets[0] self.bullets.append(Bullet(self.ship.position, self.ship.rotation, self.frame)) else: self.bullets.append(Bullet(self.ship.position, self.ship.rotation, self.frame))
def handle_input(self): super().handle_input() pygame.key.set_repeat(0, 100) keys_pressed = pygame.key.get_pressed() if keys_pressed[K_LEFT] and self.ship: self.ship.rotate(-3) if keys_pressed[K_RIGHT] and self.ship: self.ship.rotate(3) if keys_pressed[K_UP] and self.ship: self.ship.accelerate(0.05) if keys_pressed[K_DOWN] and self.ship: self.ship.accelerate( 0 ) #TODO: Set to (0) to stop the ship instantly with down-key AKA EASYMODE. if keys_pressed[K_SPACE] and self.ship: if time.time( ) - self.ship.shot_timer > self.ship.shot_delay: #Limits the rate of fire. Cannot fire more often than shot_delay value self.ship.shot_timer = time.time( ) #if it shoots, saves last fired timestamp self.ship.spawnProtection = False #removes Spawn protection if bullet is fired if len( self.bullets ) >= 15: #Does not allow more than 15 bullets in total. deletes the oldest if more than 15. del self.bullets[0] self.bullets.append( Bullet(self.ship.position.copy(), self.ship.rotation, self.ship.shot_timer) ) #Spawns a bullet with ships location. rotation and timestamp when fired. else: self.bullets.append( Bullet(self.ship.position.copy(), self.ship.rotation, self.ship.shot_timer)) if keys_pressed[K_f] and self.ship: self.asteroids.append( Asteroid( random.randrange(0, self.width, 5), random.randrange(0, self.height, 5))) #Command for spawning more asteroids if keys_pressed[K_t] and self.ship: if time.time( ) - self.ship.jump_timer > self.ship.jump_delay: #Checks if jumpdrive is on cooldown self.ship.jump_timer = time.time() #Saves timestamp for jump self.ship.jumpDrive() #Jumps the ship
def handle_input(self): super().handle_input() keys_pressed = pygame.key.get_pressed() if keys_pressed[K_LEFT] and self.ship: self.ship.rotate(-1) if keys_pressed[K_RIGHT] and self.ship: self.ship.rotate(1) if keys_pressed[K_UP] and self.ship: self.ship.accelerate(0.05) if keys_pressed[K_DOWN] and self.ship: self.ship.accelerate(0) #TODO: Set to (0) to stop the ship instantly with down-key AKA EASYMODE. if keys_pressed[K_SPACE] and self.ship: if len(self.bullets) == 0: self.bullets.append(Bullet(self.ship.position, self.ship.rotation, self.frame)) else: if self.bullets[len(self.bullets)-1].ttl > 50: self.bullets.append(Bullet(self.ship.position, self.ship.rotation, self.frame)) # Försöker få delay i bullet interval if len(self.bullets) >= 10: del self.bullets[0] self.bullets.append(Bullet(self.ship.position, self.ship.rotation, self.frame))
def handle_input(self): super().handle_input() keys_pressed = pygame.key.get_pressed() if keys_pressed[K_LEFT] and self.ship: self.ship.rotate(-0.5) if keys_pressed[K_RIGHT] and self.ship: self.ship.rotate(0.5) if keys_pressed[K_UP] and self.ship: self.ship.accelerate(0.005) if keys_pressed[K_DOWN] and self.ship: self.ship.accelerate( 0 ) #TODO: Remove comment to stop the ship instantly with down-key AKA EASYMODE. pass if keys_pressed[K_SPACE] and self.ship: if len(self.bullets) < 10: self.bullets.append( Bullet(self.ship.position, self.ship.rotation)) elif len(self.bullets) >= 10: del self.bullets[0] self.bullets.append( Bullet(self.ship.position, self.ship.rotation))
def main(self): #generating game objects def_img = pygame.image.load('char.png') character = Character([500, 500], [10, 10], 20, def_img) self.game_objects.append(character) dummy = Dummy([480, 480], [8, 8], def_img) dummy1 = Dummy([460, 500], [8, 8], def_img) self.game_objects.append(dummy) self.game_objects.append(dummy1) camera = Camera(self.screen, self.unit, character.pos) camera.scale(self.game_objects) #scale all game objects #game loop while self.running: camera.pos = character.pos tick = self.time.tick(Constants.FPS) self.screen.fill(Constants.WHITE) for event in pygame.event.get(): if event.type == pygame.QUIT: self.running = False elif event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1 or event.button == 6: bullet = Bullet(character.pos, [1, 1], def_img) self.game_objects.append(bullet) camera.scale(bullet) if event.button == 4: camera.zoom(-1) camera.scale(self.game_objects) elif event.button == 5: camera.zoom(+1) camera.scale(self.game_objects) elif event.type == pygame.KEYDOWN: key = 0 if event.key == pygame.K_UP or event.key == pygame.K_w: key = 0 if event.key == pygame.K_LEFT or event.key == pygame.K_a: key = 1 if event.key == pygame.K_DOWN or event.key == pygame.K_s: key = 2 if event.key == pygame.K_RIGHT or event.key == pygame.K_d: key = 3 character.key_press(key) elif event.type == pygame.KEYUP: key = 0 if event.key == pygame.K_UP or event.key == pygame.K_w: key = 0 if event.key == pygame.K_LEFT or event.key == pygame.K_a: key = 1 if event.key == pygame.K_DOWN or event.key == pygame.K_s: key = 2 if event.key == pygame.K_RIGHT or event.key == pygame.K_d: key = 3 character.key_release(key) objects_to_render = camera.in_render_distance(self.game_objects) character.update(tick, objects_to_render, pygame.mouse.get_pos(), camera.game_to_screen_pos(character)) camera.draw(objects_to_render) pygame.display.update()
def fire(self): self.bulletList.append( Bullet(self, self.canvas, self.bulletImg, self.hero.x + (self.hero.width / 2), self.hero.y, 20, 20, 27, 0))
def start_game(**params): width, height = params['width'], params['height'] ship = params['ship'] score, lives = params['score'], params['lives'] bullets, rocks = params['bullets'], params['rocks'] bullet_group, rock_group = params['bullet_group'], params['rock_group'] rock_generator = params['rock_generator'] game_info = params['game_info'] DISPLAYSURF = params['DISPLAYSURF'] fps_clock, FPS = params['fps_clock'], params['FPS'] audio = params['audio'] while True: kwargs = { 'width': width, 'height': height, 'keys': pygame.key.get_pressed(), 'position': ship.get_center(), 'score': score, 'lives': lives } for event in pygame.event.get(): if event.type == QUIT: quit_game() elif event.type == KEYUP and event.key == K_SPACE: bullet = Bullet.instance(**kwargs) bullet_group.add(bullet) bullets.append(bullet) audio.play('bullet') if rock_generator.should_generate(): rock = rock_generator.next() rocks.append(rock) rock_group.add(rock) DISPLAYSURF.fill((255, 255, 255, 0)) game_info.draw(DISPLAYSURF, **kwargs) ship.draw(DISPLAYSURF, **kwargs) for bullet in bullets: bullet.draw(DISPLAYSURF, **kwargs) for rock in rocks: rock.draw(DISPLAYSURF, **kwargs) bullets, rocks, points = get_remaining_objects(bullets, rocks, bullet_group, rock_group) score += points for _ in range(points): audio.play('explosion') rocks, hits = get_remaining_rocks(ship, rocks, rock_group) lives += hits pygame.display.update() fps_clock.tick(FPS) if lives <= 0: break return score, lives
def fire(): bulletList.append( Bullet(canvas, bulletImg, hero.x + (hero.width / 2), hero.y, 20, 20, 27, 0))
win.blit(startbg, (0, 0)) else: if not musicStarted: pygame.mixer.music.load('music/rpg_ambience_-_exploration.ogg') pygame.mixer.music.play(loops=-1) musicStarted = True for event in pygame.event.get(): if event.type == QUIT: running = False if event.type == KEYDOWN: if event.key == K_ESCAPE: running = False if event.key == K_SPACE: pos = rocket.rect[:2] bullet = Bullet(pos, rocket.dir, SIZE) bullets.add(bullet) all_sprites.add(bullet) gunshot_sound.play() if event.key == K_q: rocket.rotate_left() if event.key == K_e: rocket.rotate_right() elif event.type == ADDAST1: ast = Asteroid(1, SIZE) asteroids.add(ast) all_sprites.add(ast) elif event.type == ADDAST2: ast = Asteroid(2, SIZE) asteroids.add(ast)
quit() if KEY == 'd': if time.time() - INSTANT > 0.1: MARIO.forward() INSTANT = time.time() if KEY == 'a': if time.time() - INSTANT > 0.1: MARIO.backward() INSTANT = time.time() if KEY == 'm' and MARIO.get_level() > 1: os.system('aplay -q ./sounds/fireball.wav &') SCENE.bullets.append( Bullet(MARIO.get_x() + i + 2, MARIO.get_y() + 1, 1)) if KEY == 'n' and MARIO.get_level() > 1: os.system('aplay -q ./sounds/fireball.wav &') SCENE.bullets.append( Bullet(MARIO.get_x() + i - 2, MARIO.get_y() + 1, -1)) if KEY == 'w': MARIO.start_jump() while MARIO.on_ground is not True: os.system('sleep 0.05') MARIO.jump_upd() if KB.kbhit(): KEY = KB.getch() else:
def shoot(self): self.time = 0 ball = Bullet(self.x + self.size_x // 2, self.y, 5, 5, 0, -17, 1, 'bullet.png') return ball
ship_enemies = [[Ship(x*100+100, i*60, 64, 64, 15, enemy_image) for x in range(8)] for i in range(4)] while run: if not ship_enemies: restart_game('Wygrałeś!', (0, 255, 0)) continue redraw_window() enemy_move_time += clock.get_rawtime() clock.tick(30) bullet_time += clock.get_rawtime() for event in pygame.event.get(): if event.type == pygame.QUIT: run = False if event.type == pygame.USEREVENT+1: ship_row = random.choice(ship_enemies) ship_enemy = random.choice(ship_row) bullets.append(Bullet(ship_enemy.x+ship_enemy.width/2, ship_enemy.y, 10, 26, 8, bullet_image_down)) keys = pygame.key.get_pressed() if keys[pygame.K_LEFT]: ship.move(-1) if keys[pygame.K_RIGHT]: ship.move(1) if keys[pygame.K_UP] and bullet_time/250 >= 1: bullet_time = 0 bullets.append(Bullet(ship.x+ship.width/2, ship.y, 10, 26, -8, bullet_image_up)) if ship.x+ship.width >= screen_width: ship.x = screen_width - ship.width if ship.x <= 0: ship.x = 0