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中让MySQL查询结果返回字典类型的方法
Aug 22 Python
Python如何实现MySQL实例初始化详解
Nov 06 Python
Python排序搜索基本算法之希尔排序实例分析
Dec 09 Python
python2.7读取文件夹下所有文件名称及内容的方法
Feb 24 Python
Django web框架使用url path name详解
Apr 29 Python
Python何时应该使用Lambda函数
Jul 02 Python
浅谈python图片处理Image和skimage的区别
Aug 04 Python
python 并发编程 多路复用IO模型详解
Aug 20 Python
浅析Django中关于session的使用
Dec 30 Python
Python小整数对象池和字符串intern实例解析
Mar 21 Python
Django Admin后台模型列表页面如何添加自定义操作按钮
Nov 11 Python
TensorFlow的环境配置与安装方法
Feb 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
强烈推荐:php.ini中文版(1)
2006/10/09 PHP
php curl模拟post请求和提交多维数组的示例代码
2015/11/19 PHP
Yii框架实现图片上传的方法详解
2017/05/20 PHP
JavaScript入门教程(6) Window窗口对象
2009/01/31 Javascript
JQUERY 浏览器判断实现函数
2009/08/20 Javascript
基于jquery的跟随屏幕滚动代码
2012/07/24 Javascript
jquery通过a标签删除table中的一行的代码
2013/12/02 Javascript
jQuery 获取兄弟元素的几种不错方法
2014/05/23 Javascript
NodeJS Express框架中处理404页面一个方式
2014/05/28 NodeJs
JavaScript中使用数组方法汇总
2016/02/16 Javascript
easyui validatebox验证
2016/04/29 Javascript
全面解析Bootstrap中scrollspy(滚动监听)的使用方法
2016/06/06 Javascript
非常实用的vue导航钩子
2017/03/20 Javascript
使用Vue动态生成form表单的实例代码
2018/04/26 Javascript
Javascript Symbol原理及使用方法解析
2020/10/22 Javascript
python中split方法用法分析
2015/04/17 Python
python简单读取大文件的方法
2016/07/01 Python
Python简单计算给定某一年的某一天是星期几示例
2018/06/27 Python
Python3 使用cookiejar管理cookie的方法
2018/12/28 Python
Flask之pipenv虚拟环境的实现
2019/11/26 Python
python scrapy重复执行实现代码详解
2019/12/28 Python
基于Python和PyYAML读取yaml配置文件数据
2020/01/13 Python
TensorFlow实现自定义Op方式
2020/02/04 Python
Python pyautogui模块实现鼠标键盘自动化方法详解
2020/02/17 Python
StubHub意大利:购买和出售全球演唱会和体育赛事门票
2017/11/21 全球购物
GafasWorld哥伦比亚:网上购买眼镜
2017/11/28 全球购物
Oracle中delete,truncate和drop的区别
2016/05/05 面试题
EJB的角色和三个对象
2015/12/31 面试题
竞选部长演讲稿
2014/04/26 职场文书
尊老爱亲美德少年事迹材料
2014/08/14 职场文书
2014年十一国庆节活动方案
2014/09/16 职场文书
2014最新自愿离婚协议书范本
2014/11/19 职场文书
优秀大学生事迹材料
2014/12/24 职场文书
搞笑婚庆主持词
2015/06/29 职场文书
清洁工工作总结
2015/08/11 职场文书
详解Python魔法方法之描述符类
2021/05/26 Python