python游戏--投球与接球小游戏

最近没啥事情可以去做,就去了解了一下pygame这个包。用这个包做了一个简单的小游戏。闲的蛋疼的小同学可以了解一下,哈。
代码路径一个可以控制小飞机投球和竹篮接球的小游戏 (github.com)

具体操作方式

  • 按任意键进入游戏
  • 游戏中按下esc退出游戏
  • 上下左右控制无人机的行动
  • enter键可以投球
  • space可以停止运动
  • a,d可以控制竹篮的左右运动
  • 左上角则是记录得分与生命值


    代码结构图

alien_invasion.py中是游戏的主函数,有四个类来支撑主函数中的四样东西,无人机(ship)篮球(bullet)鸟(bird)竹篮(bucket),将一些需要经常修改该参数放在settings里面,调用的函数放在game_functions中。

首先来说主函数alien_invasion.py里面放了三个函数run_game,begin_game,over_game。对应游戏主函数,游戏开始之前的选择页面,和游戏结束页面

import sys
import time
import random
import pygame
from pygame.sprite import Group
from settings import Settings
from ship import Ship
from bullet import Bullet
from bucket import Bucket
from bird import Bird
import game_functions as gf


def begin_game(ai_settings,screen):
    while True:
        text = pygame.font.SysFont(ai_settings.begin_game_textFont, ai_settings.begin_game_textSize)
        text_fmt_old = text.render(ai_settings.begin_game_text, 1, ai_settings.begin_game_textColor,ai_settings.begin_game_textBgColor)
     #   print(text_fmt.get_size())
     #   print(ai_settings.scale)
        if ai_settings.scale == 0:
        #    ai_settings.scale = random.randint(0,1)*ai_settings.begin_game_textSize
            ai_settings.scale = ai_settings.begin_game_textSize
        if ai_settings.scale > ai_settings.begin_game_textSize/2:
            pygame.display.update()
            text_fmt = text.render(ai_settings.begin_game_text, 1, ai_settings.begin_game_textBgColor,ai_settings.begin_game_textColor)
            ai_settings.scale -= 1
        else:
            text_fmt = text_fmt_old
            ai_settings.scale -= 1
        screen.blit(text_fmt, (ai_settings.screen_width/2-text_fmt.get_size()[0]/2,ai_settings.screen_height/2-text_fmt.get_size()[1]*1.5))
     #   print(ai_settings.screen_width,ai_settings.screen_height)
        # 选择控制无人机还是控制竹篮还是两个都选择
        #显示可以选择项
        text = pygame.font.SysFont(ai_settings.begin_game_textFont, ai_settings.begin_game_textSize_choose)
        text_plant = text.render(ai_settings.begin_game_text_plant, 1, ai_settings.begin_game_textBgColor,
                               ai_settings.begin_game_textColor)
        text_bucket = text.render(ai_settings.begin_game_text_bucket, 1, ai_settings.begin_game_textBgColor,
                                 ai_settings.begin_game_textColor)
        text_all = text.render(ai_settings.begin_game_text_all, 1, ai_settings.begin_game_textBgColor,
                                 ai_settings.begin_game_textColor)
        #显示画面

        screen.blit(text_bucket, ((ai_settings.screen_width  - text_plant.get_size()[0] - text_bucket.get_size()[0]-text_all.get_size()[0])/
                                  (ai_settings.begin_game_text_number+1),
                                  ai_settings.screen_height / 2 ))
        screen.blit(text_plant, ((ai_settings.screen_width  - text_plant.get_size()[0] - text_bucket.get_size()[0]-text_all.get_size()[0])/
                                  (ai_settings.begin_game_text_number+1)*2+text_bucket.get_size()[0],
                                 ai_settings.screen_height / 2))
        screen.blit(text_all, ((ai_settings.screen_width  - text_plant.get_size()[0] - text_bucket.get_size()[0]-text_all.get_size()[0])/
                                  (ai_settings.begin_game_text_number+1)*3+text_bucket.get_size()[0]+text_plant.get_size()[0],
                                 ai_settings.screen_height / 2))
     #   print((ai_settings.screen_width  - text_plant.get_size()[0] - text_bucket.get_size()[0]-text_all.get_size()[0])/(ai_settings.begin_game_text_number+1)*3+text_bucket.get_size()[0]+text_plant.get_size()[0])

        pygame.display.flip()
     #   pygame.time.delay(100000)
        gf.check_event_begin_game(ai_settings,text_bucket,text_plant,text_all)
     #   print(ai_settings.begin_game_num)
        if ai_settings.begin_game_num>0:
            break

def over_game(ai_settings,screen,WOL):

    text = pygame.font.SysFont(ai_settings.over_game_textFont, ai_settings.over_game_textSize)
    if WOL:
        text_over_game = text.render(ai_settings.over_game_win_text, 1, ai_settings.over_game_textColor,
                                     ai_settings.over_game_textBgColor)
    else:
        text_over_game = text.render(ai_settings.over_game_loss_text, 1, ai_settings.over_game_textColor,
                                     ai_settings.over_game_textBgColor)
    screen.blit(text_over_game,(ai_settings.screen_width/2 - text_over_game.get_size()[0]/2,ai_settings.screen_height/2 - text_over_game.get_size()[1]/2))
    pygame.display.flip()
    pygame.time.delay(3000)
    run_game()
def run_game():
    # 初始化pygame
    pygame.init()
    #初始化页面
    ai_settings = Settings()
    screen = pygame.display.set_mode((ai_settings.screen_width,ai_settings.screen_height))
    ship = Ship(screen)
    bucket = Bucket(screen)
    bird = Bird(screen)
    birds = Group()
    bullets = Group()
    pygame.display.set_caption(ai_settings.title)
    # print(pygame.font.get_fonts())
    # 进入游戏页面
    begin_game(ai_settings,screen)
    # game loop
    while True:
        gf.check_event(ship,bucket)
      #  gf.update_screen(ai_settings, screen, ship)
    #如果选择的模式只操作一个的话,那么另外一个的物品会自己运动
        if ai_settings.begin_game_num == 1:
            ship.moving_down = False
            ship.moving_up = False
            ship.moving_left = False
            ship.moving_right = False


            if ai_settings.game_ship_UD == 50:
                ship.add = True
                ai_settings.game_ship_UD = random.randint(0,100)
            elif ai_settings.game_ship_UD > 50 :
                ship.moving_up = True
                ai_settings.game_ship_UD -= 1
            elif ai_settings.game_ship_UD < 50:
                ship.moving_down = True
                ai_settings.game_ship_UD += 1
            if ai_settings.game_ship_LR == 50:
                ai_settings.game_ship_LR = random.randint(0,100)
            elif ai_settings.game_ship_LR > 50 :
                ship.moving_left = True
                ai_settings.game_ship_LR -= 1
            elif ai_settings.game_ship_LR < 50:
                ship.moving_right = True
                ai_settings.game_ship_LR += 1
        if ai_settings.begin_game_num == 2:
            bucket.moving_right = False
            bucket.moving_left = False
            if ai_settings.game_bucket_LR == 50:
                ai_settings.game_bucket_LR = random.randint(0,100)
            elif ai_settings.game_bucket_LR > 50:
                bucket.moving_right = True
                ai_settings.game_bucket_LR -=1
            elif ai_settings.game_bucket_LR <50:
                bucket.moving_left = True
                ai_settings.game_bucket_LR +=1

        ship.update(ai_settings)
        bucket.update(ai_settings)
        if ship.add:
            if pygame.time.get_ticks()-ai_settings.ball_lastT>ai_settings.ball_interval:
                print(pygame.time.get_ticks())
                new_bullet = Bullet(screen,ship,bucket,ai_settings.score)
                bullets.add(new_bullet)
                ship.bullet = True
                ai_settings.ball_lastT = pygame.time.get_ticks()
               # print(ai_settings.ball_lastT)
        if random.randint(0,3) == 1:
            if pygame.time.get_ticks()-ai_settings.bird_lastT>ai_settings.bird_interval:
                new_bird = Bird(screen)
                birds.add(new_bird)
                ai_settings.bird_lastT = pygame.time.get_ticks()
        gf.update_screen(ai_settings, screen, ship, bullets, bucket, birds)
    #    if ship.bullet :
    #        gf.update_screen(ai_settings, screen, ship,bullets,bucket,birds)
    #    else:
    #        gf.update_screen1(ai_settings, screen, ship,bucket,birds)

        if ai_settings.score >= ai_settings.winScore:
            over_game(ai_settings,screen,True)
        if ai_settings.health_point <=0 :
            over_game(ai_settings,screen,False)
run_game()
    

先由run_game运行,进入begin_game,用一个死循环显示选择游戏模式页面ai_settings是所有设置的结构体。

游戏模式选择

'''
text = pygame.font.SysFont(ai_settings.begin_game_textFont, ai_settings.begin_game_textSize)
text_fmt_old = text.render(ai_settings.begin_game_text, 1, ai_settings.begin_game_textColor,ai_settings.begin_game_textBgColor)
# print(text_fmt.get_size())
# print(ai_settings.scale)
if ai_settings.scale == 0:
# ai_settings.scale = random.randint(0,1)ai_settings.begin_game_textSize
ai_settings.scale = ai_settings.begin_game_textSize
if ai_settings.scale > ai_settings.begin_game_textSize/2:
pygame.display.update()
text_fmt = text.render(ai_settings.begin_game_text, 1, ai_settings.begin_game_textBgColor,ai_settings.begin_game_textColor)
ai_settings.scale -= 1
else:
text_fmt = text_fmt_old
ai_settings.scale -= 1
screen.blit(text_fmt, (ai_settings.screen_width/2-text_fmt.get_size()[0]/2,ai_settings.screen_height/2-text_fmt.get_size()[1]
1.5))
# print(ai_settings.screen_width,ai_settings.screen_height)
'''
显示游戏开始,并且是将文字颜色在红和浅蓝色之间闪烁。同理,将下面的三个按键位置显示出来。同时调用game_function中的check_begin_game来按键和鼠标点击判断选择模式。 for event in pygame.event.get():获得按键值。并且用1,2,3来判断模式。放入ai_settings.begin_game_num中,然后跳出begin_game。
'''
def check_event_begin_game(ai_settings,text_bucket,text_plant,text_all):
temp = (ai_settings.screen_width - text_plant.get_size()[0] - text_bucket.get_size()[0] - text_all.get_size()[
0]) / (ai_settings.begin_game_text_number + 1)
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
# ai_settings.begin_game_num = True
if event.key == pygame.K_1:
ai_settings.begin_game_num = 1
elif event.key == pygame.K_2:
ai_settings.begin_game_num = 2
elif event.key == pygame.K_3:
ai_settings.begin_game_num = 3
if event.key == pygame.K_ESCAPE:
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
x, y = event.pos
# print("x=%d,y=%d",x,y)
if x > temp and x < temp + text_bucket.get_size()[ 0] and y > ai_settings.screen_height / 2 and
y < ai_settings.screen_height / 2 + text_bucket.get_size()[1]:
ai_settings.begin_game_num = 1
elif x > temp * 2 + text_bucket.get_size()[0] and x < temp * 2 + text_bucket.get_size()[0] +
text_plant.get_size()[0] and y > ai_settings.screen_height / 2 and y < ai_settings.screen_height / 2 +
text_bucket.get_size()[1]:
ai_settings.begin_game_num = 2
elif x > temp * 3 + text_bucket.get_size()[0] + text_plant.get_size()[0] and x <
temp * 3 + text_bucket.get_size()[0] + text_plant.get_size()[0] + text_all.get_size()[0] and
y > ai_settings.screen_height / 2 and y < ai_settings.screen_height / 2 +
text_bucket.get_size()[1]:
ai_settings.begin_game_num = 3

'''

run_game函数

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 202,406评论 5 475
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,976评论 2 379
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 149,302评论 0 335
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,366评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,372评论 5 363
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,457评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,872评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,521评论 0 256
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,717评论 1 295
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,523评论 2 319
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,590评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,299评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,859评论 3 306
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,883评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,127评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,760评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,290评论 2 342

推荐阅读更多精彩内容