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通过urllib2爬网页上种子下载示例
Feb 24 Python
Python 分析Nginx访问日志并保存到MySQL数据库实例
Mar 13 Python
python遍历目录的方法小结
Apr 28 Python
Python编程中装饰器的使用示例解析
Jun 20 Python
Python的SQLalchemy模块连接与操作MySQL的基础示例
Jul 11 Python
Python2.7 实现引入自己写的类方法
Apr 29 Python
在unittest中使用 logging 模块记录测试数据的方法
Nov 30 Python
解决python2 绘图title,xlabel,ylabel出现中文乱码的问题
Jan 29 Python
python实现手势识别的示例(入门)
Apr 15 Python
python查看矩阵的行列号以及维数方式
May 22 Python
python em算法的实现
Oct 03 Python
python如何编写类似nmap的扫描工具
Nov 06 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的安全
2006/10/09 PHP
一致性哈希算法以及其PHP实现详细解析
2013/08/24 PHP
利用phpExcel实现Excel数据的导入导出(全步骤详细解析)
2013/11/26 PHP
php分页查询的简单实现代码
2017/03/14 PHP
javascript Zifa FormValid 0.1表单验证 代码打包下载
2007/06/08 Javascript
仿中关村在线首页弹出式广告插件(jQuery版)
2012/05/03 Javascript
jQuery移除tr无效的解决方法(tr是动态添加)
2014/09/22 Javascript
jQuery+PHP实现动态数字展示特效
2015/03/14 Javascript
深入理解JS继承和原型链的问题
2016/12/17 Javascript
利用express启动一个server服务的方法
2017/09/17 Javascript
vue中echarts3.0自适应的方法
2018/02/26 Javascript
Vue.js最佳实践(五招助你成为vuejs大师)
2018/05/04 Javascript
vue代码分割的实现(codesplit)
2018/11/13 Javascript
layui之数据表格--与后台交互获取数据的方法
2019/09/29 Javascript
微信小程序语音同步智能识别的实现案例代码解析
2020/05/29 Javascript
Python3如何解决字符编码问题详解
2017/04/23 Python
python版本坑:md5例子(python2与python3中md5区别)
2017/06/20 Python
将字典转换为DataFrame并进行频次统计的方法
2018/04/08 Python
python读写配置文件操作示例
2019/07/03 Python
python用requests实现http请求代码实例
2019/10/31 Python
python 实现提取log文件中的关键句子,并进行统计分析
2019/12/24 Python
使用jupyter notebook将文件保存为Markdown,HTML等文件格式
2020/04/14 Python
HTML5制作酷炫音频播放器插件图文教程
2014/12/30 HTML / CSS
COACH德国官方网站:纽约现代奢侈品牌,1941年
2018/06/09 全球购物
教师自荐书
2013/10/08 职场文书
音乐系毕业生自荐信
2013/10/27 职场文书
副董事长岗位职责
2014/04/02 职场文书
商业融资计划书
2014/04/29 职场文书
还款承诺书范文
2014/05/20 职场文书
趣味运动会广播稿
2014/09/13 职场文书
个人租房协议书样本
2014/10/01 职场文书
员工工作能力评语
2014/12/31 职场文书
同学聚会通知短信
2015/04/20 职场文书
政协常委会议主持词
2015/07/03 职场文书
2016年党员干部廉政承诺书
2016/03/24 职场文书
Ruby序列化和持久化存储 Marshal和Pstore介绍
2022/04/18 Ruby