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 Matplotlib库入门指南
May 18 Python
python通过cookie模拟已登录状态的初步研究
Nov 09 Python
使用EduBlock轻松学习Python编程
Oct 08 Python
在python中只选取列表中某一纵列的方法
Nov 28 Python
Python基础之函数的定义与使用示例
Mar 23 Python
Python3内置模块之json编解码方法小结【推荐】
Dec 09 Python
Python使用百度api做人脸对比的方法
Aug 28 Python
python运用pygame库实现双人弹球小游戏
Nov 25 Python
超级实用的8个Python列表技巧
Aug 24 Python
python爬虫用scrapy获取影片的实例分析
Nov 23 Python
python实现自定义日志的具体方法
May 28 Python
python中 Flask Web 表单的使用方法
May 20 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
数据库中排序的对比及使用条件详解
2012/02/23 PHP
php限制文件下载速度的代码
2015/10/20 PHP
PHP中如何判断exec函数执行成功?
2016/08/04 PHP
TextArea 控件的最大长度问题(js json)
2009/12/16 Javascript
jQuery焦点控制图层展示延迟隐藏的方法
2015/03/09 Javascript
JS脚本根据手机浏览器类型跳转WAP手机网站(两种方式)
2015/08/04 Javascript
angular ngClick阻止冒泡使用默认行为的方法
2016/11/03 Javascript
Bootstrap 填充Json数据的实例代码
2017/01/11 Javascript
深入理解Node中的buffer模块
2017/06/03 Javascript
详解React 16 中的异常处理
2017/07/28 Javascript
vue.js中$set与数组更新方法
2018/03/08 Javascript
Vue表单demo v-model双向绑定问题
2018/06/29 Javascript
Vue-router的使用和出现空白页,路由对象属性详解
2018/09/03 Javascript
详解uniapp的全局变量实现方式
2021/01/11 Javascript
解析Python中的变量、引用、拷贝和作用域的问题
2015/04/07 Python
简单介绍Python2.x版本中的cmp()方法的使用
2015/05/20 Python
实例讲解Python中global语句下全局变量的值的修改
2016/06/16 Python
python+influxdb+shell编写区域网络状况表
2018/07/27 Python
Python使用qrcode二维码库生成二维码方法详解
2020/02/17 Python
Python中常用的os操作汇总
2020/11/05 Python
德国团购网站:Groupon德国
2018/03/13 全球购物
如何拷贝一整个Java对象,包括它的状态
2013/12/27 面试题
四种会话跟踪技术
2015/05/20 面试题
竞选班干部的演讲稿
2014/04/24 职场文书
团日活动总结书
2014/05/08 职场文书
学前教育专业求职信
2014/09/02 职场文书
暑假社会实践证明格式
2014/10/28 职场文书
赔偿协议书
2015/01/27 职场文书
项目建议书
2015/02/04 职场文书
大学毕业生个人总结
2015/02/28 职场文书
小学生家长意见
2015/06/03 职场文书
2015年社区国庆节活动总结
2015/07/30 职场文书
你为什么是穷人?可能是这5个缺点造成
2019/07/11 职场文书
长辈生日祝福语大全(72句)
2019/08/09 职场文书
Golang 如何实现函数的任意类型传参
2021/04/29 Golang
Ajax异步刷新功能及简单案例
2021/11/20 Javascript