pygame游戏之旅 添加游戏介绍


Posted in Python onNovember 20, 2018

本文为大家分享了pygame游戏之旅的第9篇,供大家参考,具体内容如下

在游戏开始之前定义一个函数,用来显示游戏介绍:

def game_intro():
  intro = True
  while intro:
    for event in pygame.event.get():
      print(event)
      if event.type == pygame.QUIT:
        pygame.quit()
        quit()
    gameDisplay.fill(white)
    largeText = pygame.font.Font('freesansbold.ttf',115)
    TextSurf, TextRect = text_objects('A bit Racey', largeText)
    TextRect.center = ((display_width/2),(display_height/2))
    gameDisplay.blit(TextSurf, TextRect)
    pygame.display.update()
    clock.tick(15)

全部代码:

import pygame
import time
import random
 
pygame.init()
 
white = (255,255,255)
black = (0,0,0)
 
car_width = 100
 
display_width = 800
display_height = 600
 
 
gameDisplay = pygame.display.set_mode( (display_width,display_height) )
pygame.display.set_caption('A bit Racey')
clock = pygame.time.Clock()
 
carImg = pygame.image.load('car.png')
 
def things_dodged(count):
  font = pygame.font.SysFont(None, 25)
  text = font.render("Dodged:"+str(count), True, black)
  gameDisplay.blit(text,(0,0))
 
def things(thingx, thingy, thingw, thingh, color):
  pygame.draw.rect(gameDisplay, color, [thingx, thingy, thingw, thingh])
 
 
 
def car(x, y):
  gameDisplay.blit(carImg, (x,y))
  
 
def text_objects(text, font):
  textSurface = font.render(text, True, black)
  return textSurface, textSurface.get_rect()
 
def message_diaplay(text):
  largeText = pygame.font.Font('freesansbold.ttf',115)
  TextSurf, TextRect = text_objects(text, largeText)
  TextRect.center = ((display_width/2),(display_height/2))
  gameDisplay.blit(TextSurf, TextRect)
  pygame.display.update()
  time.sleep(2)
  game_loop()
 
def crash():
  message_diaplay('You Crashed')
 
def game_intro():
  intro = True
  while intro:
    for event in pygame.event.get():
      print(event)
      if event.type == pygame.QUIT:
        pygame.quit()
        quit()
    gameDisplay.fill(white)
    largeText = pygame.font.Font('freesansbold.ttf',115)
    TextSurf, TextRect = text_objects('A bit Racey', largeText)
    TextRect.center = ((display_width/2),(display_height/2))
    gameDisplay.blit(TextSurf, TextRect)
    pygame.display.update()
    clock.tick(15)
 
def game_loop():
  x = display_width * 0.45
  y = display_height * 0.8
  x_change = 0
 
  dodged = 0
 
  gameExit = False
 
  thing_startx = random.randrange(0, display_width)
  thing_starty = -600
  thing_speed = 7
  thing_width = 100
  thing_height = 100
 
  while not gameExit:
    for event in pygame.event.get():
      if event.type == pygame.QUIT:
        pygame.quit()
        quit()
      if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_LEFT:
          x_change = -5
        elif event.key == pygame.K_RIGHT:
          x_change = 5
      if event.type == pygame.KEYUP:
        if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
          x_change = 0
      print(event)
    x += x_change
    gameDisplay.fill(white)
 
    things(thing_startx, thing_starty, thing_width, thing_height, black)
    thing_starty += thing_speed
    
    car(x,y)
    things_dodged(dodged)
    if x > display_width - car_width or x < 0:
      gameExit = True
    if thing_starty > display_height:
      thing_starty = 0 - thing_height
      thing_startx = random.randrange(0, display_width)
      dodged += 1
      thing_speed += 1
      thing_width += (dodged * 1.2)
    if y < thing_starty + thing_height:
      print('y crossover')
      if x > thing_startx and x < thing_startx + thing_width or x + car_width > thing_startx and x + car_width < thing_startx + thing_width:
        print('x crossover')
        crash()
    pygame.display.update()
    clock.tick(60)
#crash()
game_intro()
game_loop()
pygame.quit()
quit()

结果图:

pygame游戏之旅 添加游戏介绍

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。<

Python 相关文章推荐
Python 字典(Dictionary)操作详解
Mar 11 Python
Python实用日期时间处理方法汇总
May 09 Python
Python保存MongoDB上的文件到本地的方法
Mar 16 Python
Python利用itchat对微信中好友数据实现简单分析的方法
Nov 21 Python
python如何实现内容写在图片上
Mar 23 Python
python实现移位加密和解密
Mar 22 Python
Django 中自定义 Admin 样式与功能的实现方法
Jul 04 Python
Python中的四种交换数值的方法解析
Nov 18 Python
python3 pathlib库Path类方法总结
Dec 26 Python
解决Python发送Http请求时,中文乱码的问题
Apr 30 Python
Python实现验证码识别
Jun 15 Python
Python爬虫入门案例之爬取去哪儿旅游景点攻略以及可视化分析
Oct 16 Python
pygame游戏之旅 计算游戏中躲过的障碍数量
Nov 20 #Python
pygame游戏之旅 添加碰撞效果的方法
Nov 20 #Python
pygame游戏之旅 如何制作游戏障碍
Nov 20 #Python
用Python编写一个简单的CS架构后门的方法
Nov 20 #Python
python pygame实现2048游戏
Nov 20 #Python
python pygame模块编写飞机大战
Nov 20 #Python
Python Scapy随心所欲研究TCP协议栈
Nov 20 #Python
You might like
php获取客户端电脑屏幕参数的方法
2015/01/09 PHP
php实现encode64编码类实例
2015/03/24 PHP
php序列化函数serialize() 和 unserialize() 与原生函数对比
2015/05/08 PHP
PHP模糊查询的实现方法(推荐)
2016/09/06 PHP
Thinkphp框架中D方法与M方法的区别
2016/12/23 PHP
PHP Class SoapClient not found解决方法
2018/01/20 PHP
JQuery UI皮肤定制
2009/07/27 Javascript
JS实现拖动示例代码
2013/11/01 Javascript
jquery实现的美女拼图游戏实例
2015/05/04 Javascript
bootstrap weebox 支持ajax的模态弹出框
2017/02/23 Javascript
react native带索引的城市列表组件的实例代码
2017/08/08 Javascript
理解javascript async的用法
2017/08/22 Javascript
JavaScript基础进阶之数组方法总结(推荐)
2017/09/04 Javascript
Angular2开发环境搭建教程之VS Code
2017/12/15 Javascript
Vue利用canvas实现移动端手写板的方法
2018/05/03 Javascript
JavaScript中.min.js和.js文件的区别讲解
2019/02/13 Javascript
原生js实现trigger方法示例代码
2019/05/22 Javascript
关于layui导航栏不展示下拉列表的解决方法
2019/09/25 Javascript
python操作sqlite的CRUD实例分析
2015/05/08 Python
全面解析Python的While循环语句的使用方法
2015/10/13 Python
Python异常处理操作实例详解
2018/05/10 Python
python ipset管理 增删白名单的方法
2019/01/14 Python
pyqt5 键盘监听按下enter 就登陆的实例
2019/06/25 Python
Python通过Manager方式实现多个无关联进程共享数据的实现
2019/11/07 Python
python中dict()的高级用法实现
2019/11/13 Python
Python操作多维数组输出和矩阵运算示例
2019/11/28 Python
简单的Python人脸识别系统
2020/07/14 Python
解析浏览器的一些“滚动”行为鉴赏
2019/09/16 HTML / CSS
美国最大的宠物用品零售商:PetSmart
2016/11/14 全球购物
运动鞋中的劳斯莱斯:索康尼(SAUCONY)
2017/08/09 全球购物
The Hut英国:英国领先的豪华在线百货商店
2019/07/26 全球购物
欧舒丹俄罗斯官方网站:L’OCCITANE俄罗斯
2019/11/22 全球购物
服务行业个人求职的自我评价
2013/12/12 职场文书
优秀通讯员事迹材料
2014/01/28 职场文书
餐饮收银员岗位职责
2014/02/07 职场文书
2015年大学生实习评语
2015/03/25 职场文书