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使用scrapy解析js示例
Jan 23 Python
一个小示例告诉你Python语言的优雅之处
Jul 04 Python
python Django框架实现自定义表单提交
Mar 25 Python
遗传算法之Python实现代码
Oct 10 Python
python迭代dict的key和value的方法
Jul 06 Python
flask框架使用orm连接数据库的方法示例
Jul 16 Python
python3 property装饰器实现原理与用法示例
May 15 Python
Flask框架模板继承实现方法分析
Jul 31 Python
python的time模块和datetime模块实例解析
Nov 29 Python
django model通过字典更新数据实例
Apr 01 Python
全网首秀之Pycharm十大实用技巧(推荐)
Apr 27 Python
Python爬虫模拟登陆哔哩哔哩(bilibili)并突破点选验证码功能
Dec 21 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
一个简单的自动发送邮件系统(二)
2006/10/09 PHP
二十行语句实现从Excel到mysql的转化
2006/10/09 PHP
php中define用法实例
2015/07/30 PHP
PHP导出带样式的Excel示例代码
2016/08/28 PHP
PHP 等比例缩放图片详解及实例代码
2016/09/18 PHP
PHPExcel导出2003和2007的excel文档功能示例
2017/01/04 PHP
利用onresize使得div可以随着屏幕大小而自适应的代码
2010/01/15 Javascript
前端开发的开始---基于面向对象的Ajax类
2010/09/17 Javascript
javascript操作数组详解
2014/12/17 Javascript
浅析Bootstrap组件之面板组件
2016/05/04 Javascript
Extjs4.0 ComboBox如何实现三级联动
2016/05/11 Javascript
浅谈js中的this问题
2017/08/31 Javascript
详解小程序原生使用ES7 async/await语法
2018/08/06 Javascript
JS使用Prim算法和Kruskal算法实现最小生成树
2019/01/17 Javascript
在vue项目中使用Jquery-contextmenu插件的步骤讲解
2019/01/27 jQuery
小程序rich-text组件如何改变内部img图片样式的方法
2019/05/22 Javascript
详解Vue.js和layui日期控件冲突问题解决办法
2019/07/25 Javascript
Vue向后台传数组数据,springboot接收vue传的数组数据实例
2020/11/12 Javascript
python 文件与目录操作
2008/12/24 Python
Python中的类学习笔记
2014/09/23 Python
Python编程把二叉树打印成多行代码
2018/01/04 Python
Python实现的堆排序算法示例
2018/04/29 Python
python的pyecharts绘制各种图表详细(附代码)
2019/11/11 Python
美国林业供应商:Forestry Suppliers
2019/05/01 全球购物
美国亚洲时尚和美容产品的一站式网上商店:Stylevana
2019/09/05 全球购物
JPA的优势都有哪些
2013/07/04 面试题
文秘人员工作职责
2014/01/31 职场文书
酒店行政人事部经理职务说明书
2014/02/26 职场文书
廉洁使者实施方案
2014/03/29 职场文书
新年寄语大全
2014/04/12 职场文书
抽样调查项目计划书
2014/04/24 职场文书
植树造林的宣传标语
2014/06/23 职场文书
2014年教研室工作总结
2014/12/06 职场文书
写给纪委的违纪检讨书
2015/05/05 职场文书
个人催款函范文
2015/06/24 职场文书
Win11运行cmd提示“请求的操作需要提升”的两种解决方法
2022/07/07 数码科技