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实现读取命令行参数的方法
May 22 Python
python与C互相调用的方法详解
Jul 14 Python
Python 获得命令行参数的方法(推荐)
Jan 24 Python
python之super的使用小结
Aug 13 Python
python简单贪吃蛇开发
Jan 28 Python
十行代码使用Python写一个USB病毒
Jun 21 Python
Python目录和文件处理总结详解
Sep 02 Python
Python scipy的二维图像卷积运算与图像模糊处理操作示例
Sep 06 Python
python批量处理txt文件的实例代码
Jan 13 Python
Python for循环搭配else常见问题解决
Feb 11 Python
Django migrate报错的解决方案
May 20 Python
关于Python中进度条的六个实用技巧分享
Apr 05 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本地服务器分享
2013/02/19 PHP
PHP的fsockopen、pfsockopen函数被主机商禁用的解决办法
2014/07/08 PHP
destoon调用企业会员公司形象图片的实现方法
2014/08/21 PHP
Linux安装配置php环境的方法
2016/01/14 PHP
浅谈PHP中的面向对象OOP中的魔术方法
2017/06/12 PHP
鼠标移动到一张图片时变为另一张图片
2006/12/05 Javascript
File文件控件,选中文件(图片,flash,视频)即立即预览显示
2009/04/09 Javascript
仿猪八戒网左下角的文字滚动效果
2011/10/28 Javascript
解决js数据包含加号+通过ajax传到后台时出现连接错误
2013/08/01 Javascript
node.js中的fs.existsSync方法使用说明
2014/12/17 Javascript
node.js+express制作网页计算器
2016/01/17 Javascript
浅谈JavaScript的闭包函数
2016/12/08 Javascript
js实现漫天星星效果
2017/01/19 Javascript
详解angularjs实现echart图表效果最简洁教程
2017/11/29 Javascript
小程序实现带年月选取效果的日历
2018/06/27 Javascript
Vue动态加载图片在跨域时无法显示的问题及解决方法
2020/03/10 Javascript
Vue页面渲染中key的应用实例教程
2021/01/12 Vue.js
[06:21]完美世界亚洲区首席发行官竺琦TI3采访
2013/08/26 DOTA
Python 面向对象 成员的访问约束
2008/12/23 Python
利用Python的Flask框架来构建一个简单的数字商品支付解决方案
2015/03/31 Python
详解Python中的array数组模块相关使用
2016/07/05 Python
深入理解Python爬虫代理池服务
2018/02/28 Python
pymongo中group by的操作方法教程
2019/03/22 Python
利用python实现冒泡排序算法实例代码
2019/12/01 Python
TensorFlow dataset.shuffle、batch、repeat的使用详解
2020/01/21 Python
离线状态下在jupyter notebook中使用plotly实例
2020/04/24 Python
Python实现查找数据库最接近的数据
2020/06/08 Python
详解HTML5中的Communication API基本使用方法
2016/01/29 HTML / CSS
某某同志考察材料
2014/05/28 职场文书
教师年度考核个人总结
2015/02/12 职场文书
婚宴致辞
2015/07/28 职场文书
解决redis sentinel 频繁主备切换的问题
2021/04/12 Redis
JavaScript中时间格式化新思路toLocaleString()
2021/11/07 Javascript
Python实现位图分割的效果
2021/11/20 Python
mysql数据插入覆盖和时间戳的问题及解决
2022/03/25 MySQL
Redis+AOP+自定义注解实现限流
2022/06/28 Redis