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笔记(叁)继续学习
Oct 24 Python
Python中优化NumPy包使用性能的教程
Apr 23 Python
解密Python中的描述符(descriptor)
Jun 03 Python
用python写一个windows下的定时关机脚本(推荐)
Mar 21 Python
Python实现学生成绩管理系统
Apr 05 Python
解决Python pandas df 写入excel 出现的问题
Jul 04 Python
python判断完全平方数的方法
Nov 13 Python
Python面向对象程序设计多继承和多态用法示例
Apr 08 Python
Python爬取知乎图片代码实现解析
Sep 17 Python
python爬虫之遍历单个域名
Nov 20 Python
pytorch 利用lstm做mnist手写数字识别分类的实例
Jan 10 Python
Python3查找列表中重复元素的个数的3种方法详解
Feb 13 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
如何将数据从文本导入到mysql
2006/10/09 PHP
PHP读取文件并可支持远程文件的代码分享
2012/10/03 PHP
php array_udiff_assoc 计算两个数组的差集实例
2016/11/12 PHP
Yaf框架封装的MySQL数据库操作示例
2019/03/06 PHP
PHP+redis实现的限制抢购防止商品超发功能详解
2019/09/19 PHP
JavaScript的面向对象(一)
2006/11/09 Javascript
JS加ASP二级域名转向的代码
2007/05/17 Javascript
浅谈jQuery中replace()方法
2015/05/13 Javascript
基于Bootstrap实现Material Design风格表单插件 附源码下载
2016/04/18 Javascript
javascript中异常处理案例(推荐)
2016/10/03 Javascript
node+express+ejs使用模版引擎做的一个示例demo
2017/09/18 Javascript
微信小程序使用navigateTo数据传递的实例
2017/09/26 Javascript
node前端开发模板引擎Jade的入门
2018/05/11 Javascript
node.js中ws模块创建服务端和客户端,网页WebSocket客户端
2019/03/06 Javascript
vue-iview动态新增和删除的方法
2020/06/17 Javascript
[05:59]2018DOTA2国际邀请赛寻真——只为胜利的Secret
2018/08/13 DOTA
python使用PIL缩放网络图片并保存的方法
2015/04/24 Python
实例讲解Python中global语句下全局变量的值的修改
2016/06/16 Python
Python进阶_关于命名空间与作用域(详解)
2017/05/29 Python
你眼中的Python大牛 应该都有这份书单
2017/10/31 Python
Python实现按特定格式对文件进行读写的方法示例
2017/11/30 Python
Django使用uwsgi部署时的配置以及django日志文件的处理方法
2019/08/30 Python
python openvc 裁剪、剪切图片 提取图片的行和列
2019/09/19 Python
一款基于css3的动画按钮代码教程
2014/11/23 HTML / CSS
web字体加载方案优化小结
2019/11/29 HTML / CSS
美国高街时尚品牌:OASAP
2016/07/24 全球购物
西海岸男士和男童服装:Johnnie-O
2018/03/15 全球购物
优秀的计算机专业求职信范文
2013/12/27 职场文书
优秀求职信
2014/05/29 职场文书
五年级学生期末评语
2014/12/26 职场文书
大学生敬老院活动总结
2015/05/07 职场文书
2015年社区工会工作总结
2015/05/26 职场文书
国庆放假通知怎么写
2015/07/30 职场文书
大学军训心得体会800字
2016/01/11 职场文书
旅游安全责任协议书
2016/03/22 职场文书
深度学习tensorflow基础mnist
2021/04/14 Python