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 strip()函数 介绍
May 24 Python
Python中的异常处理学习笔记
Jan 28 Python
解析Python中的生成器及其与迭代器的差异
Jun 20 Python
python Selenium爬取内容并存储至MySQL数据库的实现代码
Mar 16 Python
python中将正则过滤的内容输出写入到文件中的实例
Oct 21 Python
Python使用Shelve保存对象方法总结
Jan 28 Python
Django之无名分组和有名分组的实现
Apr 16 Python
解决webdriver.Chrome()报错:Message:'chromedriver' executable needs to be in Path
Jun 12 Python
python字符串下标与切片及使用方法
Feb 13 Python
Python连接mysql方法及常用参数
Sep 01 Python
tensorflow学习笔记之tfrecord文件的生成与读取
Mar 31 Python
详解Python常用的魔法方法
Jun 03 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 版本]
2007/03/20 PHP
基于jsTree的无限级树JSON数据的转换代码
2010/07/27 Javascript
用Js实现的动态增加表格示例自己写的
2013/10/21 Javascript
JS中的构造函数详细解析
2014/03/10 Javascript
字段太多jquey快速清空表单内容方法
2014/08/21 Javascript
用JavaScript实现PHP的urlencode与urldecode函数
2015/08/13 Javascript
node.js从数据库获取数据
2016/05/08 Javascript
基于BootStrap Metronic开发框架经验小结【九】实现Web页面内容的打印预览和保存操作
2016/05/12 Javascript
JavaScript中的await/async的作用和用法
2016/10/31 Javascript
微信小程序 刷新上拉下拉不会断详细介绍
2017/05/11 Javascript
javascript实现电脑和手机版样式切换
2017/11/10 Javascript
24行JavaScript代码实现Redux的方法实例
2019/11/17 Javascript
原生js实现下拉选项卡
2019/11/27 Javascript
jquery实现直播视频弹幕效果
2020/02/25 jQuery
vue-socket.io接收不到数据问题的解决方法
2020/05/13 Javascript
[00:36]DOTA2上海特级锦标赛 Alliance战队宣传片
2016/03/04 DOTA
在Python中使用matplotlib模块绘制数据图的示例
2015/05/04 Python
Python函数中*args和**kwargs来传递变长参数的用法
2016/01/26 Python
pandas.dataframe按行索引表达式选取方法
2018/10/30 Python
python 读取文件并把矩阵转成numpy的两种方法
2019/02/12 Python
pandas删除行删除列增加行增加列的实现
2019/07/06 Python
python给指定csv表格中的联系人群发邮件(带附件的邮件)
2019/12/31 Python
详解Python模块化编程与装饰器
2021/01/16 Python
借助HTML5 Canvas API制作一个简单的猜字游戏
2016/03/25 HTML / CSS
日本卡普空电视游戏软件公司官方购物网站:e-CAPCOM
2018/07/17 全球购物
沙特阿拉伯家用电器和电子产品购物网站:Sheta and Saif
2020/04/03 全球购物
十佳大学生村官事迹
2014/01/09 职场文书
村干部培训班主持词
2014/03/28 职场文书
新年团拜会主持词
2014/04/02 职场文书
超市促销活动总结
2014/07/01 职场文书
护士节演讲稿开场白
2014/08/25 职场文书
2015年女职工工作总结
2015/05/15 职场文书
五星红旗迎风飘扬观后感
2015/06/17 职场文书
2016大学生暑期社会实践心得体会
2016/01/14 职场文书
Java基础之线程锁相关知识总结
2021/06/30 Java/Android
Java并发编程之原子性-Atomic的使用
2022/03/16 Java/Android