python游戏飞船与外星人代码

初学者遵循教程创建的Python游戏,已成功运行,目前在解决打包过程中遇到的问题。游戏包括:主程序、设置、飞船、子弹、外星人、功能、开关和信息显示等模块。
摘要由CSDN通过智能技术生成

第一个游戏,菜鸡的我按教程来的,成功运行了,打包遇到点问题,还在摸索。

模块:

主程序:game1_main.py

"""
对象screen是一个surface,每经过一次循环都将自动重绘这个 surface。在Pygame中,surface是屏幕的一部分,用于显示游戏元素。
  在这个游戏中,每个元素(如外星人或飞船)都是一个 surface。
"""

import pygame
from  game1.game1_set import sett
from  game1.game1_ship_set import ship_set
import game1.game1_function as game_function
from pygame.sprite import Group
from game1.game1_info import game_stats
from  game1.game1_button_set import button
from  game1.game1_score_set import scoreboard
#import sys
def mygame():
    #print(sys.path)
    myset = sett() # 屏幕、飞船、子弹 设置类set实例化

    pygame.init()  # 背景初始化
    screen = pygame.display.set_mode((myset.screen_width, myset.screen_height)) # 创建屏幕窗口对象screen,并设置大小--->参数【一个元组】
    pygame.display.set_caption("python_game") # 游戏窗口名字

    play_button = button(myset, screen, "start") # 开始按钮实例化
    stats = game_stats(myset) # 游戏信息类实例化
    sb = scoreboard(myset,  screen, stats)
    myship = ship_set(myset,screen) # 飞船类ship_set实例化
    bullets = Group() # 初始化子弹编组
    aliens = Group() # 初始化外星人编组 alienn = alien(myset,screen)  # 一个外星人类实例化

    game_function.aliens_doing(myset,screen, myship, aliens)

    while True: # 开始游戏的主循环【游戏主体】
        game_function.mylisten(myset, stats,sb,screen, myship,aliens, bullets,play_button) # 事件监听【鼠标及键盘】
        if stats.game_active : # 游戏状态【活跃】
            myship.update()  # 检测控制条件
            game_function.bullet_doing(myset, myship,stats,sb,screen, aliens, bullets) # 子弹事件
            game_function.update_aliens(myset, stats,sb, screen, myship, aliens, bullets) # 外星人事件
        game_function.screen_doing(myset,stats,screen,sb,myship,aliens,bullets,play_button) # 屏幕事件

mygame()

设置类:game1_set.py


class sett: # 屏幕对象设置类         【setting.py】
    def __init__(self):
        self.screen_width = 800 # 设置窗口宽度
        self.screen_height = 400 # 设置窗口高度
        self.bg_color = (230, 230, 230)  # 设置默认颜色

        self.ship_limit = 3 #飞船设置

        self.bullet_width = 3 #子弹设置
        self.bullet_height = 15
        self.bullet_color = (60,60,60)
        self.bullet_num = 10

        self.fleet_drop_speed = 25 #外星人设置

        self.speedup_scale = 1.1 # 游戏节奏加速
        self.score_scale = 1.5 # 外星人点数的提高速度
        self.initialize_dynamic_settings()

    def initialize_dynamic_settings(self):
        self.ship_speed = 3 # 飞船移动速度
        self.bullet_speed = 3 #子弹设置
        self.alien_speed = 0.5 # 外星人速度
        self.fleet_direction = 1 # fleet_direction 为1表示向右移,为-1表示向左移
        self.alien_points = 50

    def increase_speed(self):
        self.ship_speed *= self.speedup_scale
        self.bullet_speed *= self.speedup_scale
        self.alien_speed *= self.speedup_scale

        self.alien_points = int(self.alien_points * self.score_scale)

飞船类:game1_ship_set.py


"""
在Pygame中,原点 (0, 0)位于屏幕左上角,向右下方移动时,坐标值将增大。在 1200×800 的屏幕上,原点位于左上角,而右下角的坐标为 (1200, 800);
要将游戏元素居中,可设置相 rect对象的属性 center 、centerx 或 centery。要让游戏元素与屏幕边缘对齐,可使用属性top、bottom、left或right;
要调整游戏元素的水平或垂直位置,可使用属性x和 y ,它们分别是相应矩形左上角的x和y坐标。
"""
imp
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 Python 外星人入侵小游戏代码: ```python import pygame import random # 初始化 pygame pygame.init() # 设置游戏窗口 screen_width = 800 screen_height = 600 screen = pygame.display.set_mode((screen_width, screen_height)) # 设置游戏标题 pygame.display.set_caption("Alien Invasion") # 加载游戏背景 background_image = pygame.image.load("background.png") # 加载玩家飞船 player_image = pygame.image.load("player.png") player_width = player_image.get_width() player_height = player_image.get_height() player_x = (screen_width - player_width) // 2 player_y = screen_height - player_height - 10 # 加载外星人 alien_image = pygame.image.load("alien.png") alien_width = alien_image.get_width() alien_height = alien_image.get_height() alien_x = random.randint(0, screen_width - alien_width) alien_y = 0 alien_speed = 5 # 设置游戏循环 running = True while running: # 处理游戏事件 for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # 绘制游戏背景 screen.blit(background_image, (0, 0)) # 移动玩家飞船 keys = pygame.key.get_pressed() if keys[pygame.K_LEFT] and player_x > 0: player_x -= 5 if keys[pygame.K_RIGHT] and player_x < screen_width - player_width: player_x += 5 if keys[pygame.K_UP] and player_y > 0: player_y -= 5 if keys[pygame.K_DOWN] and player_y < screen_height - player_height: player_y += 5 # 绘制玩家飞船外星人 screen.blit(player_image, (player_x, player_y)) screen.blit(alien_image, (alien_x, alien_y)) # 移动外星人 alien_y += alien_speed if alien_y > screen_height: alien_x = random.randint(0, screen_width - alien_width) alien_y = 0 # 检测玩家和外星人是否相撞 if player_x < alien_x + alien_width and player_x + player_width > alien_x and player_y < alien_y + alien_height and player_y + player_height > alien_y: running = False # 刷新屏幕 pygame.display.flip() # 结束游戏 pygame.quit() ``` 在运行游戏之前,需要先准备好游戏所需的图片,并将其命名为 `background.png`、`player.png` 和 `alien.png`,并与代码文件放在同一个目录下。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值