[python] 小游戏 - play_plane

  alien_invasion.py

import sys

import pygame

from settings import Settings
from ship import Ship
import game_functions as gf
from pygame.sprite import Group

def run_game():
    # 初始化游戏并创建一个屏幕对象
    pygame.init()
    ai_settings = Settings()
    screen = pygame.display.set_mode((ai_settings.screen_width, ai_settings.screnn_height))
    pygame.display.set_caption(‘Alien Invasion‘)

    # 创建一艘飞船
    ship = Ship(ai_settings, screen)
    # 创建一个存储的子弹组
    bullets = Group()

    # 设置背景色
    bg_color = (230, 230, 230)

    # 开始游戏的主循环
    while True:
        gf.check_events(ai_settings, screen, ship, bullets)
        ship.update()
        gf.update_bullets(bullets)
        gf.update_screen(ai_settings, screen, ship, bullets)

run_game()

  bullet.py

import pygame

from pygame.sprite import Sprite

class Bullet(Sprite):

    def __init__(self, ai_settings, screen, ship):
        super(Bullet, self).__init__()
        self.screen = screen

        self.rect = pygame.Rect(0, 0, ai_settings.bullet_width, ai_settings.bullet_height)
        self.rect.centerx = ship.rect.centerx
        self.rect.top = ship.rect.top

        # 存储用小数表示的子弹位置
        self.y = float(self.rect.y)

        self.color = ai_settings.bullet_color
        self.speed_factor = ai_settings.bullet_speed_factor

    def update(self):
        self.y -= self.speed_factor
        # 更新子弹位置
        self.rect.y = self.y

    def draw_bullet(self):
        pygame.draw.rect(self.screen, self.color, self.rect)

  game_functions

import sys

import pygame

from bullet import Bullet

def check_events(ai_settings, screen, ship, bullets):
    # 监听键盘和鼠标的事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            check_keydown_events(event, ai_settings, screen, ship, bullets)
        elif event.type == pygame.KEYUP:
            check_keyup_events(event, ship)

def update_screen(ai_settings, screen, ship, bullets):
    # 每次循环时都重绘屏幕
    screen.fill(ai_settings.bg_color)

    # 在飞船和外星人后面重绘所有子弹
    for bullet in bullets.sprites():
        bullet.draw_bullet()

    ship.blitme()

    # 让最近绘制的屏幕可见
    pygame.display.flip()

def check_keydown_events(event, ai_settings, screen, ship, bullets):
    if event.key == pygame.K_RIGHT:
        ship.moving_right = True
    elif event.key == pygame.K_LEFT:
        ship.moving_left = True
    elif event.key == pygame.K_SPACE:
        # 创建一颗子弹,并将其加入到编组 bullets 中
        fire_bullet(ai_settings, screen, ship, bullets)

def check_keyup_events(event, ship):
    if event.key == pygame.K_RIGHT:
        ship.moving_right = False
    elif event.key == pygame.K_LEFT:
        ship.moving_left = False

def update_bullets(bullets):
    bullets.update()

    # 删除已经消失的子弹
    for bullet in bullets.copy():
        if bullet.rect.bottom <= 0:
            bullets.remove(bullet)

def fire_bullet(ai_settings, screen, ship, bullets):
    if len(bullets) < ai_settings.bullets_allowed:
        new_bullet = Bullet(ai_settings, screen, ship)
        bullets.add(new_bullet)

  settings.py

class Settings():

    def __init__(self):
        # 屏幕设置
        self.screen_width = 1200
        self.screnn_height = 800
        self.bg_color = (230, 230, 230)

        self.ship_speed_factor = 1.5

        # 子弹设置
        self.bullet_speed_factor = 1
        self.bullet_width = 3
        self.bullet_height = 15
        self.bullet_color = 60, 60, 60
        self.bullets_allowed = 5

  ship.py

import pygame

class Ship():

    def __init__(self, ai_settings, screen):
        self.screen = screen
        self.ai_settings = ai_settings

        # 加载飞船图像并获取其外接矩形
        self.image = pygame.image.load(‘images/ship.bmp‘)
        self.rect = self.image.get_rect()
        self.screen_rect = screen.get_rect()

        # 将每艘新飞船放在屏幕底部中央
        self.rect.centerx = self.screen_rect.centerx
        self.rect.bottom = self.screen_rect.bottom

        # 在飞船的属性 center 中存储小数值
        self.center = float(self.rect.centerx)

        # 移动标识
        self.moving_right = False
        self.moving_left = False

    def blitme(self):
        # 屏幕指定位置绘制飞船
        self.screen.blit(self.image, self.rect)

    def update(self):
        if self.moving_right and self.rect.right < self.screen_rect.right:
            self.center += self.ai_settings.ship_speed_factor
        elif self.moving_left and self.rect.left > 0:
            self.center -= self.ai_settings.ship_speed_factor

        # 更新 rect 对象
        self.rect.centerx = self.center
时间: 2024-10-05 10:10:05

[python] 小游戏 - play_plane的相关文章

python小游戏之课堂提问器

今天,接着前边的python小游戏,又写了个课堂提问器小程序.供大家一乐! #coding:utf-8 from random import randint print '\033[1;32;40m', print '你好,请选择需要几位作答者?\n' print '请输入作答者的人数:', t=1 i=input() data=[] while t<=i:     r=randint(0,32)    # print m[r],     data.append(m[r])     t+=1 p

python小游戏之一--------猜数字

看了一下python的入门书,拿个小游戏来练练手,其实就几行代码,也只有新手和脸皮厚的吊丝才好意思拿出来献丑 就好像张X忠一样,来,上海带,天朝海军天下无敌了 这里主要是一个random函数和 while 循环语句的使用,没了 #-*- encoding: utf-8 -*- ''' Created on 2014年5月8日 @author: Administrator ''' import random keys = random.randint(1,100)    #这个函数的作用是给一个随

Python小游戏、小程序

python 小游戏之摇骰子猜大小 python 实现一个双色球生成程序 python-循环与判断练习题 原文地址:https://www.cnblogs.com/wushuaishuai/p/9219065.html

python小游戏之圣诞树

圣诞期间,看了下python的for循环.刚好实践一下,一时兴起,就写了个圣诞树的小程序.供诸君一乐! 下面附上代码: #coding:utf-8 import sys def christmas(h):         print '\033[1;36;40m\t欢迎光临!\n正在为你制作专属于您的圣诞树,请稍等!\033[0m'         print '\033[1;32;40m'        # print '\t',             for i in range(1,h+

python小游戏之彩票机选器二

今天又对这个小程序做了些修改,使其更贴近现实,也更加人性化. 比如:当系统提示你选择彩票种类时,本来只有1和2两个选项,但你不小心输错了,怎么办呢?没关系,它会提示你重新输入1和2.说了这么多,还是先上代码吧.各位可以一试为快! #coding=utf-8 n=34 m=range(0,n) import time from random import randint def chnum():     print '请选择彩票种类:1为排列三,2为双色球'     print '请输入:',  

python小游戏(剪刀石头布)

#!/usr/bin/python#-- coding: utf8--import random #导入随机数的模块 computer = random.choice(['石头', '剪刀', '布'])player = raw_input('请出拳(石头/剪刀/布): ') print "Your choice: %s, Computer's choice: %s" % (player, computer)if player == '石头':if computer == '石头':p

python小游戏

import time,random # 需要的数据和变量放在开头player_list = ['[狂血战士]','[森林箭手]','[光明骑士]','[独行剑客]','[格斗大师]','[枪弹专家]']enemy_list = ['[暗黑战士]','[黑暗弩手]','[暗夜骑士]','[嗜血刀客]','[首席刺客]','[陷阱之王]']players = random.sample(player_list,3) enemies = random.sample(enemy_list,3)play

弱智python小游戏猜数字

from random import randintnum = randint(0,100)print("Guess what I think:?")bingo = Falsewhile bingo == False: answer = int(input()) if answer < num: print("too small!") if answer > num: print("too big") if answer == num

python小游戏,石头/剪子/布

#从控制台输入石头(1)/剪子(2)/布(3) player=int(input("玩家出拳 石头(1)/剪子(2)/布(3)")) #电脑随机出拳 computer computer == 1 print("玩家出的拳是%d - 电脑出的拳是%d" %(player , computer)) #玩家选择出拳%d player if (player == 1 and computer == 2)or(player == 2 and computer == 3)or(