pygame游戏之旅 调用按钮实现游戏开始功能


Posted in Python onNovember 21, 2018

本文为大家分享了pygame游戏之旅的第12篇,供大家参考,具体内容如下

实现点击功能:

click = pygame.mouse.get_pressed()
print(click)
if x + w > mouse[0] > x and y + h > mouse[1] > y:
 pygame.draw.rect(gameDisplay, ac, (x,y,w,h))
 if click[0] == 1 and action != None:
 action()

修改显示文字:

pygame.font.SysFont('comicsansms',115)

源代码:

import pygame
import time
import random
 
pygame.init()
 
white = (255,255,255)
black = (0,0,0)
gray = (128,128,128)
red = (200,0,0)
green = (0,200,0)
bright_red = (255,0,0)
bright_green = (0,255,0)
blue = (0,0,255)
 
 
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.SysFont('comicsansms',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 button (msg, x, y, w, h, ic, ac, action=None):
 mouse =pygame.mouse.get_pos()
 click = pygame.mouse.get_pressed()
 print(click)
 if x + w > mouse[0] > x and y + h > mouse[1] > y:
  pygame.draw.rect(gameDisplay, ac, (x,y,w,h))
  if click[0] == 1 and action != None:
  action()
##  if action == "play":
##   action()
##  if action == "quit":
##   pygame.quit()
##   quit()
 else:
  pygame.draw.rect(gameDisplay, ic, (x,y,w,h))
 smallText = pygame.font.SysFont('comicsansms', 20)
 textSurf, textRect = text_objects(msg, smallText)
 textRect.center = ( (x+(w/2)), (y+(h/2)))
 gameDisplay.blit(textSurf, textRect)
 
def quitgame():
 pygame.quit()
 quit()
 
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.SysFont('comicsansms',115)
 TextSurf, TextRect = text_objects('A bit Racey', largeText)
 TextRect.center = ((display_width/2),(display_height/2))
 gameDisplay.blit(TextSurf, TextRect)
 button("GO", 150, 450, 100, 50, green, bright_green,game_loop)
 button("Quit",550, 450, 100, 50, red, bright_red,quitgame)
 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判断一个集合是否包含了另外一个集合中所有项的方法
Jun 30 Python
Python实现两个list对应元素相减操作示例
Jun 09 Python
解决python3 网络请求路径包含中文的问题
May 10 Python
wxPython实现绘图小例子
Nov 19 Python
Python读取表格类型文件代码实例
Feb 17 Python
jupyter notebook的安装与使用详解
May 18 Python
python3.6使用SMTP协议发送邮件
May 20 Python
浅析Python __name__ 是什么
Jul 07 Python
Anaconda详细安装步骤图文教程
Nov 12 Python
利用python+ffmpeg合并B站视频及格式转换的实例代码
Nov 24 Python
详解pycharm的python包opencv(cv2)无代码提示问题的解决
Jan 29 Python
Pillow图像处理库安装及使用
Apr 12 Python
pygame游戏之旅 按钮上添加文字的方法
Nov 21 #Python
Face++ API实现手势识别系统设计
Nov 21 #Python
详解django自定义中间件处理
Nov 21 #Python
pygame游戏之旅 添加游戏界面按键图形
Nov 20 #Python
pygame游戏之旅 添加游戏介绍
Nov 20 #Python
pygame游戏之旅 计算游戏中躲过的障碍数量
Nov 20 #Python
pygame游戏之旅 添加碰撞效果的方法
Nov 20 #Python
You might like
php 网上商城促销设计实例代码
2012/02/17 PHP
php去除数组中重复数据
2014/11/18 PHP
Zend Framework教程之Zend_Db_Table_Row用法实例分析
2016/03/21 PHP
thinkphp框架实现路由重定义简化url访问地址的方法分析
2020/04/04 PHP
使用dynatrace-ajax跟踪JavaScript的性能
2010/04/12 Javascript
最佳6款用于移动网站开发的jQuery 图片滑块插件小结
2012/07/20 Javascript
treepanel动态加载数据实现代码
2012/12/15 Javascript
java与javascript之间json格式数据互转介绍
2013/10/29 Javascript
js读取csv文件并使用json显示出来
2015/01/09 Javascript
js中this用法实例详解
2015/05/05 Javascript
易操作的jQuery表单提示插件
2015/12/01 Javascript
jquery ztree实现模糊搜索功能
2016/02/25 Javascript
浅谈JavaScript的内置对象和浏览器对象
2016/06/03 Javascript
bootstrap响应式工具使用详解
2017/11/29 Javascript
实例详解BootStrap的动态模态框及静态模态框
2018/08/13 Javascript
微信小程序当前时间时段选择器插件使用方法详解
2018/12/28 Javascript
vue2 v-model/v-text 中使用过滤器的方法示例
2019/05/09 Javascript
jquery+css实现Tab栏切换的代码实例
2019/05/14 jQuery
js简单的分页器插件代码实例
2019/09/11 Javascript
file-loader打包图片文件时路径错误输出为[object-module]的解决方法
2020/01/03 Javascript
深入解析Python编程中JSON模块的使用
2015/10/15 Python
Python脚本实现Web漏洞扫描工具
2016/10/25 Python
利用python获取某年中每个月的第一天和最后一天
2016/12/15 Python
Python虚拟环境virtualenv的安装与使用详解
2017/05/28 Python
Python3 实现随机生成一组不重复数并按行写入文件
2018/04/09 Python
利用Django模版生成树状结构实例代码
2019/05/19 Python
使用python接入微信聊天机器人
2020/03/31 Python
Tensorflow tf.dynamic_partition矩阵拆分示例(Python3)
2020/02/07 Python
移动端html5判断是否滚动到底部并且下拉加载
2019/11/19 HTML / CSS
介绍一下常见的木马种类
2014/11/15 面试题
买房子个人收入证明
2014/01/16 职场文书
开业典礼主持词
2014/03/21 职场文书
科技之星事迹材料
2014/06/02 职场文书
新兵入伍心得体会
2014/09/04 职场文书
2014最新离职证明范本
2014/09/12 职场文书
鸡毛信观后感
2015/06/11 职场文书