コード例 #1
0
def check_keydown_event(event, AI, screen, my_ship, bullets, bullet_sound):
    # create a new bullet and add to bullets group
    if len(bullets) < AI.bullets_allowed:
        new_bullet_left = Bullet(AI, screen, my_ship)
        bullets.add(new_bullet_left)
        new_bullet_right = Bullet(AI, screen, my_ship)
        bullets.add(new_bullet_right)
        pg.mixer.Sound.play(bullet_sound)
コード例 #2
0
ファイル: game_functions.py プロジェクト: jttwnsnd/pygame
def check_events(hero, bullets, game_settings, screen):
    for event in pygame.event.get():  #run through all events
        if event.type == pygame.QUIT:  #if the events is quit...
            sys.exit()
        elif event.type == pygame.KEYDOWN:  #the user pushed a key and it's down.
            if event.key == pygame.K_RIGHT:
                hero.moving_right = True  #set the flag
            elif event.key == pygame.K_LEFT:
                hero.moving_left = True  #set the flag
            elif event.key == pygame.K_UP:
                hero.moving_up = True
            elif event.key == pygame.K_DOWN:
                hero.moving_down = True
            elif event.key == pygame.K_SPACE:
                new_bullet = Bullet(screen, hero, game_settings)
                bullets.add(new_bullet)
        elif event.type == pygame.KEYUP:  #user let go of a key
            if event.key == pygame.K_RIGHT:
                hero.moving_right = False
            elif event.key == pygame.K_LEFT:
                hero.moving_left = False
            elif event.key == pygame.K_UP:
                hero.moving_up = False
            elif event.key == pygame.K_DOWN:
                hero.moving_down = False
コード例 #3
0
    def keyPressed(self):
        keys = pygame.key.get_pressed()
        currentTime = pygame.time.get_ticks()  #milliseconds
        #space key - fire bullets
        if keys[pygame.K_SPACE] and \
        int(currentTime) - int(self.lastBulletTime) >= 250:
            #fire bullets every half a second
            self.cat.isFiring = True
            vel = -1
            if self.cat.dir == "Right": vel = 1
            self.bullets.append(
                Bullet(self.cat.x, self.cat.y - self.cat.h / 4, vel))
            self.lastBulletTime = currentTime

        #move left and right
        if keys[pygame.K_LEFT]:
            if not self.cat.collision(self.platforms, (-1, 0)):
                self.cat.move("Left", self.platforms)
        elif keys[pygame.K_RIGHT]:
            if not self.cat.collision(self.platforms, (1, 0)):
                self.cat.move("Right", self.platforms)
        else:
            self.cat.isWalk = False

        #up key = player jump
        if not (self.cat.isJump):
            if keys[pygame.K_UP]:
                self.cat.isJumping = True
                self.cat.isJump = True
            else:
                self.cat.isJump = False
        else:
            self.cat.jumpUp(self.platforms)
コード例 #4
0
def fire_bullet(ai_settings, screen, ship, bullets):
	"""Fire a bullet if limit not reached yet."""

	# Create a new bullet and add it to the bullets group.
	if len(bullets) < ai_settings.bullets_allowed:
		new_bullet = Bullet(ai_settings, screen, ship)
		bullets.add(new_bullet)
コード例 #5
0
def check_events(hero, bullets, game_settings, screen, aliens, play_button,
                 score_board):
    for event in pygame.event.get():  #run through all pygame events
        if event.type == pygame.QUIT:
            sys.exit()  #quit

        #button handler
        elif event.type == pygame.MOUSEBUTTONDOWN:
            mouse_x, mouse_y = pygame.mouse.get_pos()
            # print mouse_x
            # print mouse_y
            if play_button.rect.collidepoint(mouse_x, mouse_y):
                game_settings.game_active = True
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RIGHT:
                hero.moving_right = True
            elif event.key == pygame.K_LEFT:
                hero.moving_left = True
            elif event.key == pygame.K_UP:
                hero.moving_up = True
            elif event.key == pygame.K_DOWN:
                hero.moving_down = True
            elif event.key == pygame.K_SPACE:
                new_bullet = Bullet(screen, hero, game_settings)
                bullets.add(new_bullet)

        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_RIGHT:  #right arrow
                hero.moving_right = False
            if event.key == pygame.K_LEFT:  #left arrow
                hero.moving_left = False
            if event.key == pygame.K_UP:
                hero.moving_up = False
            if event.key == pygame.K_DOWN:
                hero.moving_down = False
コード例 #6
0
ファイル: main.py プロジェクト: LucaMangano/Tankgame-MED1
    def __init__(self):
        ## sets dimension of the screen and the background
        pygame.init()
        self.w = 900
        self.h = 600
        self.screen = pygame.display.set_mode((self.w, self.h), pygame.FULLSCREEN)
        self.background = pygame.image.load("background.png")

        ## creates all the objects and lists needed for the game
        self.player = Player(self.w, self.h, self.background)
        self.shooter = Shooter(self.player.x, self.player.y, self.player.d)
        self.shield = Shield(self.player.x, self.player.y, self.player.d)
        self.generator = Generator(self.player.x, self.player.y, self.player.d)
        self.bullet = Bullet(self.shooter.x, self.shooter.y, self.shooter.image_rotation)
        self.enemies = [Enemy(self.w, self.h)]
        self.enemies_2 = []
        self.counter_enemies_2_dead = 0
        self.collectible = Collectible(self.w, self.h)
        self.ray = Ray(self.w, self.h)

        ## loads energy image and sets its default value
        self.energy_5 = pygame.image.load("energy_5.png")
        self.energy = 100

        ## sets all default values
        self.points = 0
        self.killed_big_enemies = 0
        self.killed_small_enemies = 0
        self.rays_collected = 0
        self.collectibles_collected = 0

        ## initializes fonts and creates the first text
        font = pygame.font.SysFont("comic", 64)
        self.font = pygame.font.SysFont("arial", 10)
        text = font.render("Click to play", True, (255, 255, 255))

        ## loads sound
        self.sound = pygame.mixer.Sound("blip.wav")

        ## sets timer and default timer variables
        self.clock = pygame.time.Clock()
        self.timer = 0
        self.seconds = 0
        self.minutes = 0

        ## manages beginning screen and first inputs
        beginning = True
        while beginning:
            self.screen.fill((0, 0, 0))
            self.screen.blit(text, ((self.w / 2) - 150, (self.h / 2) - 64))
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.display.quit()
                if event.type == pygame.MOUSEBUTTONUP:
                    beginning = False
                    self.screen.blit(self.background, (0, 0))
                    self.screen.blit(self.player.images[1], (self.player.x, self.player.y))
                    self.screen.blit(self.shooter.image, (self.shooter.x, self.shooter.y))
                    self.running()
            pygame.display.update()
コード例 #7
0
ファイル: gamefunctions.py プロジェクト: cylvis/misc
def check_events(hero, bullets, game_settings, screen, play_button):
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.MOUSEBUTTONDOWN:
            mouse_x, mouse_y = pygame.mouse.get_pos()
            if play_button.rect.collidepoint(mouse_x, mouse_y):
                game_settings.game_active = True
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RIGHT:
                hero.moving_right = True
            elif event.key == pygame.K_LEFT:
                hero.moving_left = True
            elif event.key == pygame.K_UP:
                hero.moving_up = True
            elif event.key == pygame.K_DOWN:
                hero.moving_down = True
            elif event.key == pygame.K_SPACE:
                new_bullet = Bullet(screen, hero, game_settings)
                bullets.add(new_bullet)
        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_RIGHT:
                hero.moving_right = False
            elif event.key == pygame.K_LEFT:
                hero.moving_left = False
            elif event.key == pygame.K_UP:
                hero.moving_up = False
            elif event.key == pygame.K_DOWN:
                hero.moving_down = False
コード例 #8
0
ファイル: events.py プロジェクト: jinz82/python
def check_events(ship, screen, settings, group, button):
  """ Handles all events in game """
  if (settings.game_active == 1):
    for event in pygame.event.get():
     if event.type == pygame.QUIT:
      sys.exit()
     elif event.type == pygame.KEYDOWN:
      if event.key == pygame.K_RIGHT:
       ship.move_right = True
      if event.key == pygame.K_LEFT:
       ship.move_left = True
      if event.key == pygame.K_SPACE:
       if len(group) < settings.bullets_max:
        new_bullet = Bullet(ship,screen,settings)
        group.add(new_bullet)
     elif event.type == pygame.KEYUP:
      if event.key == pygame.K_RIGHT:
       ship.move_right = False
      if event.key == pygame.K_LEFT:
       ship.move_left = False
  else:
    for event in pygame.event.get():
      if event.type == pygame.QUIT:
       sys.exit()
      elif event.type == pygame.MOUSEBUTTONDOWN:
        mouse_x, mouse_y = pygame.mouse.get_pos()
        if button.rect.collidepoint(mouse_x, mouse_y):
          settings.game_active = 1
コード例 #9
0
ファイル: game_functions.py プロジェクト: Leovaldez42/IRI
def fire_bullet(ai_settings: Settings, game_items: GameItems):
    """Fire a bullet if limit not reached."""

    # Create a new bullet and add it to the bullets group.
    if len(game_items.bullets) < ai_settings.bullets_allowed:
        new_bullet = Bullet(ai_settings, game_items.screen, game_items.ship)
        game_items.bullets.add(new_bullet)
コード例 #10
0
def check_events(hero, bullets, game_settings, screen):
    for event in pygame.event.get():  #run through all pygame events
        if event.type == pygame.QUIT:  #if the event is the quit event...
            sys.exit()  #quit
        elif event.type == pygame.KEYDOWN:  #the user pushed a key and it's down
            if event.key == pygame.K_UP:  #the user pressed right
                hero.moving_up = True  #set the flag
            elif event.key == pygame.K_DOWN:
                hero.moving_down = True  #set the flag
            if event.key == pygame.K_RIGHT:  #the user pressed right
                hero.moving_right = True  #set the flag
            elif event.key == pygame.K_LEFT:
                hero.moving_left = True  #set the flag
            elif event.key == pygame.K_SPACE:  #user pushed space bar
                new_bullet = Bullet(screen, hero, game_settings)
                bullets.add(new_bullet)
        elif event.type == pygame.KEYUP:  #user let go of a key
            if event.key == pygame.K_UP:  #specifically the rigth arrow
                hero.moving_up = False
            elif event.key == pygame.K_DOWN:  #specifically the rigth arrow
                hero.moving_down = False
            if event.key == pygame.K_RIGHT:  #specifically the rigth arrow
                hero.moving_right = False
            elif event.key == pygame.K_LEFT:  #specifically the rigth arrow
                hero.moving_left = False
コード例 #11
0
ファイル: shooter_3.py プロジェクト: AmzadHossainrafis/pygame
 def fire_bullets(self, event):
     """this function handle bullet movement and maintain bullet limite """
     if event.key == pygame.K_SPACE:
         if len(self.bullets) < self.settings.bullet_limit:
             fire = mixer.Sound("music/gun_sound_1.mp3")
             fire.play()
             new_bullets = Bullet(self)
             self.bullets.add(new_bullets)
コード例 #12
0
    def fire_bullet(self):
        """Create a new bullet and add it to the bullets group"""
        if len(self.bullets) < self.settings.bullets_allowed:
            new_bullet = Bullet(self)
            self.bullets.add(new_bullet)

        # display enemy plane
        self.enemy_planes.draw(self.screen)
コード例 #13
0
def fire_bullet(ai_settings, screen, stats, ship, bullets):
    # creating new bullet and adding to the bullets group
    if len(bullets) < ai_settings.bullets_allowed:
        new_bullet = Bullet(ai_settings, screen, ship)
        bullets.add(new_bullet)

    #Counts the number of times a bullet is fired (since this function only occurs when
    #space bar is hit).
    stats.bullets_fired += 1
コード例 #14
0
ファイル: tank.py プロジェクト: GredziszewskiK/tanks
 def fire_bullet(self, bullets):
     """
     Enemy tank shot. Tank can shot evry x second. X is time saved in
     game_settings.py > self.e_tank_shot_time
     """
     if (time.time() - self.start_timer) > self.shot_time:
         self.start_timer = time.time()
         new_bullet = Bullet(self.g_settings, self.surface, self.screen,
                             self.rect, self.moving_direction)
         bullets.add(new_bullet)
コード例 #15
0
ファイル: run_game.py プロジェクト: wangpeilin/pygame-
def update_ship(screen, ship, ship_bullets):
    # 按下空格键时持续开火
    if pygame.key.get_pressed()[pygame.K_SPACE]:
            current_time = pygame.time.get_ticks()
            global pretime
            interval = current_time - pretime
            if interval >= 300 and len(ship_bullets) < ship.bullet_num:
                new_bullet = Bullet(screen, ship)
                ship_bullets.add(new_bullet)
                pretime = current_time
コード例 #16
0
ファイル: game.py プロジェクト: seliceantitus/SpaceBlocks
def shoot(time, last_shot, shoot_interval):
    if (time - last_shot >= shoot_interval):
        if bullet_boost != 2:
            bullet = Bullet(bullet_boost, bullet_width, bullet_height,
                            player.get_x() + 15, 600 - 75, 0)
            bullets_group.add(bullet)
        else:
            bullet_center = Bullet(bullet_boost, bullet_width, bullet_height,
                                   player.get_x() + 15, 600 - 75, 0)
            bullet_left = Bullet(bullet_boost, bullet_width, bullet_height,
                                 player.get_x() + 10, 600 - 75, 6)
            bullet_right = Bullet(bullet_boost, bullet_width, bullet_height,
                                  player.get_x() + 20, 600 - 75, -6)
            bullets_group.add(bullet_center)
            bullets_group.add(bullet_left)
            bullets_group.add(bullet_right)
        return True
    else:
        return False
コード例 #17
0
    def on_key_press(self, key: int, modifiers: int):
        """
        Puts the current key in the set of keys that are being held.
        You will need to add things here to handle firing the bullet.
        """
        if self.ship.alive:
            self.held_keys.add(key)

            if key == arcade.key.SPACE:
                # TODO: Fire the bullet here!
                angle = self.ship.angle

                pnt = Point()
                pnt.x = self.ship.center.x
                pnt.y = self.ship.center.y

                bullet = Bullet()
                bullet.fire(angle, pnt)

                self.bullets.append(bullet)
コード例 #18
0
 def fire_bullet(self):
     """Create a new bullet and add it to the bullets group."""
     if self.fired_since_reload < self.settings.max_bullets:
         playsound(self, 'shoot_sound')
         new_bullet = Bullet(self)
         self.bullets.add(new_bullet)
         if self.limited:
             self.fired_since_reload += 1
     else:
         self.reloading = True
         self.reload_time_left = self.settings.reload_time
コード例 #19
0
def fire_bullet(ai_settings: Settings, game_items: GameItems):
    """Fire a bullet if limit not reached."""

    #game_items.ship.explode_ship()
    #resetstuff(ai_settings, GameStats , game_items)
    #game_items.ship.reset_ship()
    # Create a new bullet and add it to the bullets group.

    if len(game_items.bullets) < ai_settings.bullets_allowed:

        new_bullet = Bullet(ai_settings, game_items.screen, game_items.ship)
        lasersound.play()
        game_items.bullets.add(new_bullet)
コード例 #20
0
    def fire(self):

        if self.counter > self.fire_rate and self.start_counter > self.settings.spawn_time:
            # self.settings.fire_audio.play()
            # self.settings.fire_sound.play()
            ws.PlaySound("music/fire.wav", ws.SND_ASYNC)
            # self.settings.fire_sound.play()

            self.settings.bullets.append(
                Bullet(self.settings, (self.x, self.y), self.bullet_move, 1,
                       self.settings.tanks_bullets_speed[self.tank_level - 1],
                       self.ind))
            self.counter = 0
コード例 #21
0
    def fire(self):

        # 一次发射3枚子弹
        for i in (0, 1, 2):
            # 创建子弹精灵
            bullet = Bullet()

            # 设置精灵的位置
            bullet.rect.bottom = self.rect.y - i*20
            bullet.rect.centerx = self.rect.centerx

            # 将子弹精灵添加到子弹精灵组中
            self.bullets.add(bullet)
コード例 #22
0
def fire_bullet(ai_settings, screen, ship_obj, bullets):
    #ADD NEW BULLET AND ADD IT TO BULLET GROUP
    if len(bullets) < ai_settings.bullet_num_allowed:
        if ai_settings.level_num % 10 == 0:
            ai_settings.bullet_width = 150
        elif ai_settings.level_num % 15 == 0:
            ai_settings.bullet_width = 200
        elif ai_settings.level_num % 5 == 0:
            ai_settings.bullet_width = 50
        else:
            ai_settings.bullet_width = 5
        new_bullet = Bullet(ai_settings, screen, ship_obj)
        bullets.add(new_bullet)
コード例 #23
0
def check_events(screen, the_hero, game_settings, bullets, enemies):
    for event in pygame.event.get():

        # check to see if the event that occurred is the quit event
        if event.type == pygame.QUIT:
            # the user clicked the red X and the game should stop
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            # what key was pressed?
            if event.key == pygame.K_SPACE:
                new_bullet = Bullet(screen, the_hero, game_settings, 'up',
                                    'vertical')
                bullets.add(new_bullet)

            elif event.key == pygame.K_a:
                new_bullet = Bullet(screen, the_hero, game_settings, 'right',
                                    'horizontal')
                bullets.add(new_bullet)

            elif event.key == pygame.K_RIGHT:
                the_hero.moving_right = True
            elif event.key == pygame.K_LEFT:
                the_hero.moving_left = True
            elif event.key == pygame.K_UP:
                the_hero.moving_up = True
            elif event.key == pygame.K_DOWN:
                the_hero.moving_down = True
        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_RIGHT:
                the_hero.moving_right = False
            elif event.key == pygame.K_LEFT:
                the_hero.moving_left = False
            elif event.key == pygame.K_UP:
                the_hero.moving_up = False
            elif event.key == pygame.K_DOWN:
                the_hero.moving_down = False
コード例 #24
0
def check_keydown_events(ship, event, bullets, ai_settings, screen,
                         play_button):
    #to move the ship right
    if event.key == pygame.K_RIGHT:
        ship.moving_right = True
    #to move the ship left
    elif event.key == pygame.K_LEFT:
        ship.moving_left = True
    if event.key == pygame.K_p:
        play_button.p_pressed = True
    #to shoot
    if event.key == pygame.K_SPACE:
        if len(bullets) < ai_settings.bullets_allowed_num:
            new_bullet = Bullet(ai_settings, ship, screen)
            bullets.add(new_bullet)
            ai_settings.ship_shooting_sound.play()
コード例 #25
0
def check_events_down(event, ai_settings, screen, ship, bullets, stats):
    if event.key == pygame.K_q:
        high_score_save_and_exit(stats)

    elif event.key == pygame.K_UP:
        ship.moving_up = True

    elif event.key == pygame.K_DOWN:
        ship.moving_down = True

    elif event.key == pygame.K_SPACE:

        if len(bullets) <= ai_settings.bullet_allowed:
            pygame.mixer.music.play()
            new_bullet = Bullet(ai_settings, screen, ship)
            bullets.add(new_bullet)
コード例 #26
0
def check_events(game_settings, screen, defendor, bullets, game_status,
                 play_button):
    """响应按键和鼠标事件"""

    for event in pg.event.get():

        # 响应点击退出游戏事件
        if event.type == pg.QUIT:
            sys.exit()

        # 响应点击"play"按钮事件
        elif event.type == pg.MOUSEBUTTONDOWN:
            mouse_x, mouse_y = pg.mouse.get_pos()
            check_play_button(play_button, mouse_x, mouse_y, game_status)

        # 响应通过箭头按键移动位置事件
        elif event.type == pg.KEYDOWN:
            if event.key == pg.K_RIGHT:
                defendor.moving_right = True
            if event.key == pg.K_UP:
                defendor.moving_up = True
            if event.key == pg.K_DOWN:
                defendor.moving_down = True
            if event.key == pg.K_LEFT:
                defendor.moving_left = True

            # 响应通过空格发射子弹事件
            if event.key == pg.K_SPACE:
                new_bullet = Bullet(game_settings, screen, defendor)
                bullets.add(new_bullet)

            # 响应通过q键退出游戏事件
            if event.key == pg.K_q:
                sys.exit()

        # 响应松开按键停止移动事件
        elif event.type == pg.KEYUP:
            if event.key == pg.K_RIGHT:
                defendor.moving_right = False
            if event.key == pg.K_LEFT:
                defendor.moving_left = False
            if event.key == pg.K_UP:
                defendor.moving_up = False
            if event.key == pg.K_DOWN:
                defendor.moving_down = False
コード例 #27
0
ファイル: board.py プロジェクト: preet021/Super-Mario
    def render(self, engine):

        for spring in self.springs:
            if spring.posX in list(
                    range(engine.gridX, engine.gridX + self.screenWidth)):
                spring.checkCollision(engine)

        self.updateEnemies(engine)

        self.updateBullets(engine)
        '''update the position of bridge when onscreen'''
        k = engine.gridX + engine.mario.legsMaxX
        if k >= 480 and k <= 690:
            if time.time() - self.bridge.updTime > 0.09:
                self.bridge.updatePos(engine)
        '''update the position of Boss when onscreen'''
        if engine.gridX + engine.mario.legsMaxX >= 1270:
            if time.time() - self.boss.updTime1 >= 2:
                self.bullets.append(
                    Bullet('<', self.boss.posX - 2, self.boss.posY + 5,
                           time.time(), -1))
                self.boss.updTime1 = time.time()
            if time.time() - self.boss.updTime >= 1:
                x, y = self.boss.posX, self.boss.posY
                self.eraseBoss(x, y)
                self.boss.updatePos()
                self.boss.updTime = time.time()
            self.placeBoss(engine)
        '''place the mario on screen'''
        tmp_grid = self.placeMario(engine)
        if not tmp_grid:
            return False
        '''clear the terminal and print the current state of the game'''
        os.system('tput reset')
        s = str(' ' * 10) + 'SCORE : {}'.format(engine.score) + str(' ' * 10) + 'COINS COLLECTED : {}'.format(engine.coins) + str(' ' * 10) + \
            'TIME LEFT = {}'.format(engine.lefTime) + str(' ' * 10) + 'LIVES REMAINING : {}'.format(
                engine.lives) + str(' ' * 10) + 'BOSS LIVES : {}'.format(self.boss.lives)
        print(self.red + s + self.reset)
        for i in tmp_grid:
            s = ''
            for j in i:
                s += j
            print(s)

        return True
コード例 #28
0
def check_events(hero, bullets, game_settings, screen, play_button):
    for event in pygame.event.get():  #run through all pygame events
        if event.type == pygame.QUIT:  #if the event is the quit event...
            sys.exit()  #quit
        #handle button click
        elif event.type == pygame.MOUSEBUTTONDOWN:
            mouse_x, mouse_y = pygame.mouse.get_pos()
            # print mouse_x, mouse_y
            if play_button.rect.collidepoint(mouse_x, mouse_y):
                game_settings.game_active = True

        elif event.type == pygame.KEYDOWN:  #the user pushed a key and it's down
            if event.key == pygame.K_RIGHT:  #the user pressed right
                hero.moving_right = True  #set the flag

            elif event.key == pygame.K_LEFT:
                hero.moving_left = True  #set the flag

            elif event.key == pygame.K_SPACE:  #user pushed space bar
                new_bullet = Bullet(hero, game_settings, screen)
                bullets.add(new_bullet)

            elif event.key == pygame.K_UP:
                hero.moving_up = True

            elif event.key == pygame.K_DOWN:
                hero.moving_down = True

        elif event.type == pygame.KEYUP:  #user let go of a key
            if event.key == pygame.K_RIGHT:  #specifically the right arrow
                hero.moving_right = False

            elif event.key == pygame.K_LEFT:
                hero.moving_left = False

            elif event.key == pygame.K_UP:
                hero.moving_up = False

            elif event.key == pygame.K_DOWN:
                hero.moving_down = False
コード例 #29
0
def check_events(hero, bullets, game_settings, screen, monster, play_button):
    for event in pygame.event.get():  #run through all pygame events
        if event.type == pygame.QUIT:  #if the event is the quit event then quit
            sys.exit()  #quit
        # Handle button click
        elif event.type == pygame.MOUSEBUTTONDOWN:
            mouse_x, mouse_y = pygame.mouse.get_pos()
            if play_button.rect.collidepoint(mouse_x, mouse_y):
                game_settings.game_active = True
        elif event.type == pygame.KEYDOWN:  #the user pushed a key and its down
            if event.key == pygame.K_RIGHT:  # the user pressed right
                hero.moving_right = True  #set the flag
            elif event.key == pygame.K_LEFT:
                hero.moving_left = True  #set the flag
            elif event.key == pygame.K_SPACE:  #user pushed the space bar
                new_bullet = Bullet(screen, hero, game_settings)
                bullets.add(new_bullet)
        elif event.type == pygame.KEYUP:  #the user let go of the key
            if event.key == pygame.K_RIGHT:
                hero.moving_right = False
            elif event.key == pygame.K_LEFT:
                hero.moving_left = False
コード例 #30
0
ファイル: game.py プロジェクト: wiedzmin1414/Ninja
 def mouse(self):
     mouse_buttons = pygame.mouse.get_pressed()
     mouse_position = list(pygame.mouse.get_pos())
     mouse_position[0] += self.delta_view
     if mouse_buttons[
             0] and not self.ninja.shuriken and self.ninja.position.get_y(
             ) >= 0:
         #print("Leci shuriken")
         self.ninja.shuriken = Link_shuriken(self.ninja.link_hand(),
                                             mouse_position,
                                             speed=50)
     if not mouse_buttons[0] and self.ninja.shuriken:
         #print("Konczy wisiec")
         if self.ninja.is_hanging:
             self.ninja.stop_hanging()
         else:
             self.ninja.shuriken = None
     if mouse_buttons[2]:
         if self.ninja.is_shot_available():
             self.ninja.shot()
             self.list_of_bullets.append(
                 Bullet(self.ninja.armed_hand(), mouse_position, speed=50))
コード例 #31
0
def check_events(hero, bullets, monsters, screen, game_settings, play_button):
  #run through all pygame events
  for event in pygame.event.get():
    #...if it's QUIT, exit the game
    if event.type == pygame.QUIT:
      sys.exit()
    elif event.type == pygame.MOUSEBUTTONDOWN:
        mouse_x, mouse_y = pygame.mouse.get_pos()
        if play_button.rect.collidepoint(mouse_x, mouse_y):
          game_settings.game_active = True
    elif event.type == pygame.KEYDOWN: 
      if event.key == pygame.K_RIGHT: #righ pressed
        hero.moving_right = True #set the flag
      elif event.key == pygame.K_LEFT:
        hero.moving_left = True #set the flag to move the hero
      elif event.key == pygame.K_SPACE:
        new_bullet = Bullet(screen, hero, game_settings)
        bullets.add(new_bullet)
    elif event.type == pygame.KEYUP:
      if event.key == pygame.K_RIGHT:
        hero.moving_right = False
      elif event.key == pygame.K_LEFT:
        hero.moving_left = False
コード例 #32
0
ファイル: main.py プロジェクト: LucaMangano/Tankgame-MED1
class Game(object):
    def __init__(self):
        ## sets dimension of the screen and the background
        pygame.init()
        self.w = 900
        self.h = 600
        self.screen = pygame.display.set_mode((self.w, self.h), pygame.FULLSCREEN)
        self.background = pygame.image.load("background.png")

        ## creates all the objects and lists needed for the game
        self.player = Player(self.w, self.h, self.background)
        self.shooter = Shooter(self.player.x, self.player.y, self.player.d)
        self.shield = Shield(self.player.x, self.player.y, self.player.d)
        self.generator = Generator(self.player.x, self.player.y, self.player.d)
        self.bullet = Bullet(self.shooter.x, self.shooter.y, self.shooter.image_rotation)
        self.enemies = [Enemy(self.w, self.h)]
        self.enemies_2 = []
        self.counter_enemies_2_dead = 0
        self.collectible = Collectible(self.w, self.h)
        self.ray = Ray(self.w, self.h)

        ## loads energy image and sets its default value
        self.energy_5 = pygame.image.load("energy_5.png")
        self.energy = 100

        ## sets all default values
        self.points = 0
        self.killed_big_enemies = 0
        self.killed_small_enemies = 0
        self.rays_collected = 0
        self.collectibles_collected = 0

        ## initializes fonts and creates the first text
        font = pygame.font.SysFont("comic", 64)
        self.font = pygame.font.SysFont("arial", 10)
        text = font.render("Click to play", True, (255, 255, 255))

        ## loads sound
        self.sound = pygame.mixer.Sound("blip.wav")

        ## sets timer and default timer variables
        self.clock = pygame.time.Clock()
        self.timer = 0
        self.seconds = 0
        self.minutes = 0

        ## manages beginning screen and first inputs
        beginning = True
        while beginning:
            self.screen.fill((0, 0, 0))
            self.screen.blit(text, ((self.w / 2) - 150, (self.h / 2) - 64))
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.display.quit()
                if event.type == pygame.MOUSEBUTTONUP:
                    beginning = False
                    self.screen.blit(self.background, (0, 0))
                    self.screen.blit(self.player.images[1], (self.player.x, self.player.y))
                    self.screen.blit(self.shooter.image, (self.shooter.x, self.shooter.y))
                    self.running()
            pygame.display.update()

    def running(self):
        while True:

            ## manages time
            time_passed = self.clock.tick(30)
            time_seconds = time_passed / 1000.0
            self.timer += 1
            self.seconds += 0.03
            if self.seconds >= 60:
                self.minutes += 1
                self.seconds = 0

            ## gets all the inputs and calls function related to each input
            for event in pygame.event.get():
                if event.type == pygame.KEYUP:
                    if event.key == pygame.K_ESCAPE:
                        pygame.display.quit()
                    if event.key == pygame.K_a:
                        self.shield.index -= 1
                        if self.shield.index == -5:
                            self.shield.index = -1
                    elif event.key == pygame.K_j:
                        self.shield.index += 1
                        if self.shield.index == 4:
                            self.shield.index = 0
                    elif event.key == pygame.K_l:
                        self.generator.index += 1
                        if self.generator.index == 4:
                            self.generator.index = 0
                    elif event.key == pygame.K_s:
                        self.generator.index -= 1
                        if self.generator.index == -5:
                            self.generator.index = -1
            key = pygame.key.get_pressed()
            if key[pygame.K_UP]:
                self.player.moves_up(time_seconds)
                self.player.index = 0
            if key[pygame.K_DOWN]:
                self.player.moves_down(time_seconds)
                self.player.index = 1
            if key[pygame.K_LEFT]:
                self.player.moves_left(time_seconds)
                self.player.index = 2
            if key[pygame.K_RIGHT]:
                self.player.moves_right(time_seconds)
                self.player.index = 3

            ## blits the background
            self.screen.blit(self.background, (0, 0))

            ## manages energy and prints it
            if self.energy >= 100:
                self.energy = 100
            if self.energy <= 10:
                self.sound.play()
            if self.energy <= 0:
                self.collectible.finished = True
            for n in xrange(0, self.energy / 5):
                self.screen.blit(self.energy_5, (self.w - 35 - 10 * n, 4))

            ## manages the text (points and time)
            text = self.font.render((str(self.minutes) + ":" + str(self.seconds)), True, (255, 255, 255))
            self.screen.blit(text, (10, 10))
            text = self.font.render(("Points: " + str(self.points)), True, (255, 255, 255))
            self.screen.blit(text, (440, 10))

            ## manages collectibles
            self.collectible.blit_check(self.background, self.timer)
            if self.collectible.blit:
                self.screen.blit(self.collectible.image, (self.collectible.x, self.collectible.y))
                self.collectible_rect = pygame.Rect(self.collectible.x, self.collectible.y, 20, 20)

            ## manages player and collision with collectible
            self.screen.blit(self.player.images[self.player.index], (self.player.x, self.player.y))
            self.player_rect = pygame.Rect(self.player.x, self.player.y, 40, 40)
            if self.collectible.blit:
                if self.player_rect.colliderect(self.collectible_rect):
                    i = self.collectible.index
                    self.collectible = Collectible(self.w, self.h)
                    self.collectible.index = i
                    self.points += 100
                    self.collectibles_collected += 1

            ## manages bullet, checks hits with walls
            self.bullet.moves(self.shooter.x, self.shooter.y)
            if self.bullet.rotate:
                self.bullet.rotates(time_seconds)
                self.screen.blit(self.bullet.rotated_image, self.bullet.image_draw_pos)
                self.bullet.check_shot()
                if self.bullet.shot:
                    self.energy -= 5
            if self.bullet.shot:
                self.screen.blit(self.bullet.rotated_image, (self.bullet.x, self.bullet.y))
                self.bullet.hits_borders(self.background)
            if self.bullet.hits_bg:
                self.bullet = Bullet(self.shooter.x, self.shooter.y, self.shooter.image_rotation)
            self.bullet_rect = pygame.Rect(self.bullet.x, self.bullet.y, 4, 10)

            ## manages generator
            self.generator.moves(self.player.x, self.player.y, self.player.d)
            self.screen.blit(self.generator.images[self.generator.index], (self.generator.x, self.generator.y))
            generator_rect = pygame.Rect(self.generator.x, self.generator.y, self.generator.w, self.generator.h)

            ## manages shooter
            self.shooter.moves(self.player.x, self.player.y, self.player.d)
            self.shooter.rotates(time_seconds)
            self.screen.blit(self.shooter.rotated_image, self.shooter.image_draw_pos)

            ## manages shield
            self.shield.moves(self.player.x, self.player.y, self.player.d)
            self.screen.blit(self.shield.images[self.shield.index], (self.shield.x, self.shield.y))
            shield_rect = pygame.Rect(self.shield.x, self.shield.y, self.shield.w, self.shield.h)

            ## manages big enemies one by one, checks collisions with bullets, shield and tank
            for n in xrange(0, len(self.enemies)):
                enemy = self.enemies[n]
                enemy.moves(time_seconds)
                enemy.bounces(self.background)
                if enemy.error:
                    self.enemies[n] = Enemy(self.w, self.h)
                self.screen.blit(enemy.image, (enemy.x, enemy.y))
                enemy_rect = pygame.Rect(enemy.x, enemy.y, enemy.d, enemy.d)
                if enemy_rect.colliderect(self.bullet_rect):
                    self.bullet = Bullet(self.shooter.x, self.shooter.y, self.shooter.image_rotation)
                    self.enemies_2.append(Enemy_2(enemy.x, enemy.y))
                    self.enemies_2.append(Enemy_2(enemy.x, enemy.y))
                    self.enemies_2.append(Enemy_2(enemy.x, enemy.y))
                    enemy.alive = False
                    self.points += 10
                    self.killed_big_enemies += 1
                elif enemy_rect.colliderect(shield_rect):
                    enemy.reverse()
                    self.energy -= 5
                elif enemy_rect.colliderect(self.player_rect):
                    enemy.reverse()
                    self.energy -= 10
            if (self.timer == 30 * 5 or self.timer == 30 * 10) and len(self.enemies) <= 2:
                self.enemies.append(Enemy(self.w, self.h))
            ## temporary list to manage the elimination of some enemies
            l = []
            for n in xrange(0, len(self.enemies)):
                enemy = self.enemies[n]
                if enemy.alive:
                    l.append(self.enemies[n])
            self.enemies = l

            ## manages small enemies one by one, checks collision with bullets, shield and tank
            for n in xrange(0, len(self.enemies_2)):
                enemy_2 = self.enemies_2[n]
                enemy_2.moves(time_seconds)
                enemy_2.bounces(self.background)
                if enemy.error:
                    self.enemies_2[n] = Enemy_2(self.w, self.h)
                self.screen.blit(enemy_2.image, (enemy_2.x, enemy_2.y))
                enemy_2_rect = pygame.Rect(enemy_2.x, enemy_2.y, enemy_2.d, enemy_2.d)
                if enemy_2_rect.colliderect(self.player_rect):
                    enemy_2.reverse()
                    self.energy -= 10
                elif enemy_2_rect.colliderect(shield_rect):
                    enemy_2.reverse()
                    self.energy -= 5
                elif enemy_2_rect.colliderect(self.bullet_rect):
                    self.bullet = Bullet(self.shooter.x, self.shooter.y, self.shooter.image_rotation)
                    enemy_2.alive = False
                    self.counter_enemies_2_dead += 1
                    self.points += 10
                    self.killed_small_enemies += 1
            ## temporary list to manage the elimination of some enemies
            l = []
            for n in xrange(0, len(self.enemies_2)):
                enemy_2 = self.enemies_2[n]
                if enemy_2.alive:
                    l.append(self.enemies_2[n])
            self.enemies_2 = l
            if self.counter_enemies_2_dead == 3:
                self.counter_enemies_2_dead = 0
                self.enemies.append(Enemy(self.w, self.h))

            ## manages rays of energy and collision with generator and tank and manages life time
            self.ray.moves(time_seconds)
            self.ray.bounces(self.background)
            self.screen.blit(self.ray.image, (self.ray.x, self.ray.y))
            ray_rect = pygame.Rect(self.ray.x, self.ray.y, self.ray.d, self.ray.d)
            if ray_rect.colliderect(generator_rect):
                self.ray.check_caught()
                if self.ray.caught:
                    self.ray = Ray(self.w, self.h)
                    self.energy += 20
                    self.points += 10
                    self.rays_collected += 1
                else:
                    self.ray.reverse()
            if ray_rect.colliderect(self.player_rect):
                self.ray.reverse()
            if (
                self.timer >= 30 * 15
                and self.timer <= 30 * 15.01
                or self.timer >= 30 * 30
                and self.timer <= 30 * 30.01
                or self.timer >= 30 * 45
                and self.timer <= 30 * 45.01
                or self.timer >= 30 * 60
                and self.timer <= 30 * 60.01
                or self.timer >= 30 * 75
                and self.timer <= 30 * 75.01
                or self.timer >= 30 * 90
                and self.timer <= 30 * 90.01
                or self.timer >= 30 * 105
                and self.timer <= 30 * 105.01
                or self.timer >= 30 * 120
                and self.timer <= 30 * 120.01
            ):
                self.ray = Ray(self.w, self.h)

            ## manages the end of the loop
            if self.collectible.finished == True:
                pygame.display.quit()
                self.end()

            pygame.display.update()

    def end(self):
        ## creates a whole new screen, calculates points and blits results
        pygame.init()
        screen = pygame.display.set_mode((600, 300), 0, 32)
        screen.fill((0, 0, 0))

        points = self.seconds + (self.minutes * 60) + self.energy + self.points
        if (
            self.killed_big_enemies >= 3
            and self.killed_small_enemies >= 3
            and self.rays_collected >= 3
            and self.collectibles_collected >= 4
        ):
            msg = "You did great!!!"
        else:
            msg = "You could have done better! ;)"

        end = True
        while end:
            font = pygame.font.SysFont("comic", 40)
            text = font.render(("Total points: " + str(points)), True, (255, 255, 255))
            text1 = font.render(msg, True, (255, 255, 255))
            screen.blit(text, (100, 100))
            screen.blit(text1, (100, 200))

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.display.quit()
                    end = False
            pygame.display.update()
コード例 #33
0
ファイル: main.py プロジェクト: LucaMangano/Tankgame-MED1
    def running(self):
        while True:

            ## manages time
            time_passed = self.clock.tick(30)
            time_seconds = time_passed / 1000.0
            self.timer += 1
            self.seconds += 0.03
            if self.seconds >= 60:
                self.minutes += 1
                self.seconds = 0

            ## gets all the inputs and calls function related to each input
            for event in pygame.event.get():
                if event.type == pygame.KEYUP:
                    if event.key == pygame.K_ESCAPE:
                        pygame.display.quit()
                    if event.key == pygame.K_a:
                        self.shield.index -= 1
                        if self.shield.index == -5:
                            self.shield.index = -1
                    elif event.key == pygame.K_j:
                        self.shield.index += 1
                        if self.shield.index == 4:
                            self.shield.index = 0
                    elif event.key == pygame.K_l:
                        self.generator.index += 1
                        if self.generator.index == 4:
                            self.generator.index = 0
                    elif event.key == pygame.K_s:
                        self.generator.index -= 1
                        if self.generator.index == -5:
                            self.generator.index = -1
            key = pygame.key.get_pressed()
            if key[pygame.K_UP]:
                self.player.moves_up(time_seconds)
                self.player.index = 0
            if key[pygame.K_DOWN]:
                self.player.moves_down(time_seconds)
                self.player.index = 1
            if key[pygame.K_LEFT]:
                self.player.moves_left(time_seconds)
                self.player.index = 2
            if key[pygame.K_RIGHT]:
                self.player.moves_right(time_seconds)
                self.player.index = 3

            ## blits the background
            self.screen.blit(self.background, (0, 0))

            ## manages energy and prints it
            if self.energy >= 100:
                self.energy = 100
            if self.energy <= 10:
                self.sound.play()
            if self.energy <= 0:
                self.collectible.finished = True
            for n in xrange(0, self.energy / 5):
                self.screen.blit(self.energy_5, (self.w - 35 - 10 * n, 4))

            ## manages the text (points and time)
            text = self.font.render((str(self.minutes) + ":" + str(self.seconds)), True, (255, 255, 255))
            self.screen.blit(text, (10, 10))
            text = self.font.render(("Points: " + str(self.points)), True, (255, 255, 255))
            self.screen.blit(text, (440, 10))

            ## manages collectibles
            self.collectible.blit_check(self.background, self.timer)
            if self.collectible.blit:
                self.screen.blit(self.collectible.image, (self.collectible.x, self.collectible.y))
                self.collectible_rect = pygame.Rect(self.collectible.x, self.collectible.y, 20, 20)

            ## manages player and collision with collectible
            self.screen.blit(self.player.images[self.player.index], (self.player.x, self.player.y))
            self.player_rect = pygame.Rect(self.player.x, self.player.y, 40, 40)
            if self.collectible.blit:
                if self.player_rect.colliderect(self.collectible_rect):
                    i = self.collectible.index
                    self.collectible = Collectible(self.w, self.h)
                    self.collectible.index = i
                    self.points += 100
                    self.collectibles_collected += 1

            ## manages bullet, checks hits with walls
            self.bullet.moves(self.shooter.x, self.shooter.y)
            if self.bullet.rotate:
                self.bullet.rotates(time_seconds)
                self.screen.blit(self.bullet.rotated_image, self.bullet.image_draw_pos)
                self.bullet.check_shot()
                if self.bullet.shot:
                    self.energy -= 5
            if self.bullet.shot:
                self.screen.blit(self.bullet.rotated_image, (self.bullet.x, self.bullet.y))
                self.bullet.hits_borders(self.background)
            if self.bullet.hits_bg:
                self.bullet = Bullet(self.shooter.x, self.shooter.y, self.shooter.image_rotation)
            self.bullet_rect = pygame.Rect(self.bullet.x, self.bullet.y, 4, 10)

            ## manages generator
            self.generator.moves(self.player.x, self.player.y, self.player.d)
            self.screen.blit(self.generator.images[self.generator.index], (self.generator.x, self.generator.y))
            generator_rect = pygame.Rect(self.generator.x, self.generator.y, self.generator.w, self.generator.h)

            ## manages shooter
            self.shooter.moves(self.player.x, self.player.y, self.player.d)
            self.shooter.rotates(time_seconds)
            self.screen.blit(self.shooter.rotated_image, self.shooter.image_draw_pos)

            ## manages shield
            self.shield.moves(self.player.x, self.player.y, self.player.d)
            self.screen.blit(self.shield.images[self.shield.index], (self.shield.x, self.shield.y))
            shield_rect = pygame.Rect(self.shield.x, self.shield.y, self.shield.w, self.shield.h)

            ## manages big enemies one by one, checks collisions with bullets, shield and tank
            for n in xrange(0, len(self.enemies)):
                enemy = self.enemies[n]
                enemy.moves(time_seconds)
                enemy.bounces(self.background)
                if enemy.error:
                    self.enemies[n] = Enemy(self.w, self.h)
                self.screen.blit(enemy.image, (enemy.x, enemy.y))
                enemy_rect = pygame.Rect(enemy.x, enemy.y, enemy.d, enemy.d)
                if enemy_rect.colliderect(self.bullet_rect):
                    self.bullet = Bullet(self.shooter.x, self.shooter.y, self.shooter.image_rotation)
                    self.enemies_2.append(Enemy_2(enemy.x, enemy.y))
                    self.enemies_2.append(Enemy_2(enemy.x, enemy.y))
                    self.enemies_2.append(Enemy_2(enemy.x, enemy.y))
                    enemy.alive = False
                    self.points += 10
                    self.killed_big_enemies += 1
                elif enemy_rect.colliderect(shield_rect):
                    enemy.reverse()
                    self.energy -= 5
                elif enemy_rect.colliderect(self.player_rect):
                    enemy.reverse()
                    self.energy -= 10
            if (self.timer == 30 * 5 or self.timer == 30 * 10) and len(self.enemies) <= 2:
                self.enemies.append(Enemy(self.w, self.h))
            ## temporary list to manage the elimination of some enemies
            l = []
            for n in xrange(0, len(self.enemies)):
                enemy = self.enemies[n]
                if enemy.alive:
                    l.append(self.enemies[n])
            self.enemies = l

            ## manages small enemies one by one, checks collision with bullets, shield and tank
            for n in xrange(0, len(self.enemies_2)):
                enemy_2 = self.enemies_2[n]
                enemy_2.moves(time_seconds)
                enemy_2.bounces(self.background)
                if enemy.error:
                    self.enemies_2[n] = Enemy_2(self.w, self.h)
                self.screen.blit(enemy_2.image, (enemy_2.x, enemy_2.y))
                enemy_2_rect = pygame.Rect(enemy_2.x, enemy_2.y, enemy_2.d, enemy_2.d)
                if enemy_2_rect.colliderect(self.player_rect):
                    enemy_2.reverse()
                    self.energy -= 10
                elif enemy_2_rect.colliderect(shield_rect):
                    enemy_2.reverse()
                    self.energy -= 5
                elif enemy_2_rect.colliderect(self.bullet_rect):
                    self.bullet = Bullet(self.shooter.x, self.shooter.y, self.shooter.image_rotation)
                    enemy_2.alive = False
                    self.counter_enemies_2_dead += 1
                    self.points += 10
                    self.killed_small_enemies += 1
            ## temporary list to manage the elimination of some enemies
            l = []
            for n in xrange(0, len(self.enemies_2)):
                enemy_2 = self.enemies_2[n]
                if enemy_2.alive:
                    l.append(self.enemies_2[n])
            self.enemies_2 = l
            if self.counter_enemies_2_dead == 3:
                self.counter_enemies_2_dead = 0
                self.enemies.append(Enemy(self.w, self.h))

            ## manages rays of energy and collision with generator and tank and manages life time
            self.ray.moves(time_seconds)
            self.ray.bounces(self.background)
            self.screen.blit(self.ray.image, (self.ray.x, self.ray.y))
            ray_rect = pygame.Rect(self.ray.x, self.ray.y, self.ray.d, self.ray.d)
            if ray_rect.colliderect(generator_rect):
                self.ray.check_caught()
                if self.ray.caught:
                    self.ray = Ray(self.w, self.h)
                    self.energy += 20
                    self.points += 10
                    self.rays_collected += 1
                else:
                    self.ray.reverse()
            if ray_rect.colliderect(self.player_rect):
                self.ray.reverse()
            if (
                self.timer >= 30 * 15
                and self.timer <= 30 * 15.01
                or self.timer >= 30 * 30
                and self.timer <= 30 * 30.01
                or self.timer >= 30 * 45
                and self.timer <= 30 * 45.01
                or self.timer >= 30 * 60
                and self.timer <= 30 * 60.01
                or self.timer >= 30 * 75
                and self.timer <= 30 * 75.01
                or self.timer >= 30 * 90
                and self.timer <= 30 * 90.01
                or self.timer >= 30 * 105
                and self.timer <= 30 * 105.01
                or self.timer >= 30 * 120
                and self.timer <= 30 * 120.01
            ):
                self.ray = Ray(self.w, self.h)

            ## manages the end of the loop
            if self.collectible.finished == True:
                pygame.display.quit()
                self.end()

            pygame.display.update()