当前位置: 首页 > 工具软件 > Fire > 使用案例 >

fire_practice

麻书
2023-12-01

alien_fire.py:

import sys
import pygame
from fire import Fire
from settings import Settings
import game_functions as gf

def run_game():
    pygame.init()
    
    ai_settings = Settings()
    
    screen = pygame.display.set_mode((ai_settings.screen_width, ai_settings.screen_height))
    pygame.display.set_caption("Alien Fire")
    
    fire = Fire(screen,ai_settings)
    
    keydown = True
    key = 0
    
    while True:
        gf.check_events(fire)
        
        fire.update()
                    
        gf.update_screen(ai_settings, screen, fire)
    
                    
run_game()

fire.py:

import pygame

class Fire():
    def __init__(self, screen, ai_settings):
        self.screen = screen
        self.screen_rect = screen.get_rect()
        
        self.ai_settings = ai_settings
        
        self.image = pygame.image.load("images/fire.bmp")
        self.rect = self.image.get_rect()
        
        self.rect.centerx = self.screen_rect.centerx
        self.rect.centery = self.screen_rect.centery
        
        self.moving_right = False
        self.moving_left = False
        self.moving_up = False
        self.moving_down = False
        
    def blitme(self):
        self.screen.blit(self.image, self.rect)
        
    def right(self):
        if self.rect.right < self.screen_rect.right:
            self.rect.centerx += self.ai_settings.fire_speed_factor
        
    def left(self):
        if self.rect.left > 0:
            self.rect.centerx -= self.ai_settings.fire_speed_factor
    
    def up(self):
        if self.rect.top > 0:
            self.rect.centery -= self.ai_settings.fire_speed_factor
    
    def down(self):
        if self.rect.bottom < self.screen_rect.bottom:
            self.rect.centery += self.ai_settings.fire_speed_factor
            
    def update(self):
        if self.moving_right:
            self.right()
        if self.moving_left:
            self.left()
        if self.moving_up:
            self.up()
        if self.moving_down:
            self.down()

        

settings.py:

class Settings():
    def __init__(self):
        self.screen_width = 1200
        self.screen_height = 600
        self.bg_color = (255,255,255)
        self.fire_speed_factor = 1.5
        

game_functions.py:

import pygame
from fire import Fire

def key_down(event, fire):
    if event.key == pygame.K_RIGHT:
        fire.moving_right = True
    elif event.key == pygame.K_LEFT:
        fire.moving_left = True
    elif event.key == pygame.K_UP:
        fire.moving_up = True
    elif event.key == pygame.K_DOWN:
        fire.moving_down = True

def key_up(event, fire):
    if event.key == pygame.K_RIGHT:
        fire.moving_right = False
    elif event.key == pygame.K_LEFT:
        fire.moving_left = False
    elif event.key == pygame.K_UP:
        fire.moving_up = False
    elif event.key == pygame.K_DOWN:
        fire.moving_down = False

def check_events(fire):
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            key_down(event, fire)
        elif event.type == pygame.KEYUP:
            key_up(event, fire)
            
def update_screen(ai_settings, screen, fire):
    screen.fill(ai_settings.bg_color)
    fire.blitme()
    pygame.display.flip()

 

 类似资料:

相关阅读

相关文章

相关问答