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验证文件是否可读写代码分享
Dec 11 Python
使用Anaconda3建立虚拟独立的python2.7环境方法
Jun 11 Python
Win8.1下安装Python3.6提示0x80240017错误的解决方法
Jul 31 Python
opencv导入头文件时报错#include的解决方法
Jul 31 Python
Python 在OpenCV里实现仿射变换—坐标变换效果
Aug 30 Python
python sorted函数的小练习及解答
Sep 18 Python
wxpython+pymysql实现用户登陆功能
Nov 19 Python
Tensorflow矩阵运算实例(矩阵相乘,点乘,行/列累加)
Feb 05 Python
Python中猜拳游戏与猜筛子游戏的实现方法
Sep 04 Python
python爬虫构建代理ip池抓取数据库的示例代码
Sep 22 Python
如何在 Matplotlib 中更改绘图背景的实现
Nov 26 Python
pycharm 复制代码出现空格的解决方式
Jan 15 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极大的增强功能和性能
2006/10/09 PHP
php 获取当前访问的url文件名的方法小结
2010/02/08 PHP
ThinkPHP中__initialize()和类的构造函数__construct()用法分析
2014/11/29 PHP
php的sso单点登录实现方法
2015/01/08 PHP
PHP下载远程文件到本地存储的方法
2015/03/24 PHP
PHP数组和explode函数示例总结
2015/05/08 PHP
PHP简单的MVC框架实现方法
2015/12/01 PHP
PHP函数超时处理方法
2016/02/14 PHP
php实现搜索类封装示例
2016/03/31 PHP
使用Yii2实现主从数据库设置
2016/11/20 PHP
php 如何禁用eval() 函数实例详解
2016/12/01 PHP
关于ThinkPHP中的异常处理详解
2018/05/11 PHP
PHP连接及操作PostgreSQL数据库的方法详解
2019/01/30 PHP
javascript支持firefox,ie7页面布局拖拽效果代码
2007/12/20 Javascript
JavaScript 应用技巧集合[推荐]
2009/08/30 Javascript
深入理解javascript作用域和闭包
2014/09/23 Javascript
深入理解JavaScript系列(47):对象创建模式(上篇)
2015/03/04 Javascript
JS动态插入并立即执行回调函数的方法
2016/04/21 Javascript
图解Javascript——作用域、作用域链、闭包
2017/03/21 Javascript
ES6正则表达式扩展笔记
2017/07/25 Javascript
基于vue 开发中出现警告问题去除方法
2018/01/25 Javascript
vue实现微信分享朋友圈,发送朋友的示例讲解
2018/02/10 Javascript
JavaScript图片处理与合成总结
2018/03/04 Javascript
微信小程序实现运动步数排行功能(可删除)
2018/07/05 Javascript
Vue.js标签页组件使用方法详解
2019/10/19 Javascript
JavaScript实现alert弹框效果
2020/11/19 Javascript
Perl中著名的Schwartzian转换问题解决实现
2015/06/02 Python
PySide和PyQt加载ui文件的两种方法
2019/02/27 Python
CSS3实现背景透明文字不透明的示例代码
2018/06/25 HTML / CSS
关于HTML5的安全问题开发人员需要牢记的
2012/06/21 HTML / CSS
html5指南-3.如何实现html元素拖拽功能
2013/01/07 HTML / CSS
涉外经济法专业毕业生推荐信
2013/11/24 职场文书
会议活动邀请函
2014/01/27 职场文书
中药学专业毕业生推荐信
2014/07/10 职场文书
毕业证明书
2015/06/19 职场文书
Mysql如何实现不存在则插入,存在则更新
2022/03/25 MySQL