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爬虫入门教程之点点美女图片爬虫代码分享
Sep 02 Python
Python中用format函数格式化字符串的用法
Apr 08 Python
Python实现简单多线程任务队列
Feb 27 Python
python实现多线程行情抓取工具的方法
Feb 28 Python
Python基于递归和非递归算法求两个数最大公约数、最小公倍数示例
May 21 Python
浅析Python装饰器以及装饰器模式
May 28 Python
浅谈python常用程序算法
Mar 22 Python
python实现的分层随机抽样案例
Feb 25 Python
python代码xml转txt实例
Mar 10 Python
python爬虫中抓取指数的实例讲解
Dec 01 Python
python 基于opencv实现高斯平滑
Dec 18 Python
关于探究python中sys.argv时遇到的问题详解
Feb 23 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验证码函数的使用示例
2013/05/03 PHP
使用php从身份证号中获取一系列线索(星座、生肖、生日等)
2016/05/11 PHP
PHP实现上传图片到 zimg 服务器
2016/10/19 PHP
javascript 文档的编码问题解决
2009/03/01 Javascript
javascript复制对象使用说明
2011/06/28 Javascript
Extjs中ComboBox加载并赋初值的实现方法
2012/03/22 Javascript
解决wx.onMenuShareTimeline出现的问题
2016/08/16 Javascript
Bootstrap实现提示框和弹出框效果
2017/01/11 Javascript
angularjs ocLazyLoad分步加载js文件实例
2017/01/17 Javascript
jquery dataTable 后台加载数据并分页实例代码
2017/06/07 jQuery
js模拟百度模糊搜索的实例
2017/08/04 Javascript
解析Angular 2+ 样式绑定方式
2018/01/15 Javascript
使用element-ui table expand展开行实现手风琴效果
2019/03/15 Javascript
JS实现拖拽元素时与另一元素碰撞检测
2020/08/27 Javascript
浅析vue中的nextTick
2020/12/28 Vue.js
[47:50]Secret vs VP 2018国际邀请赛小组赛BO2 第二场 8.17
2018/08/20 DOTA
解决Python 遍历字典时删除元素报异常的问题
2016/09/11 Python
微信跳一跳辅助python代码实现
2018/01/05 Python
Python中矩阵创建和矩阵运算方法
2018/08/04 Python
python安装pywin32clipboard的操作方法
2019/01/24 Python
在python中logger setlevel没有生效的解决
2020/02/21 Python
Python基于DB-API操作MySQL数据库过程解析
2020/04/23 Python
tensorflow图像裁剪进行数据增强操作
2020/06/30 Python
PyTorch 中的傅里叶卷积实现示例
2020/12/11 Python
css3 2D图片转动样式可以扩充到Js当中
2014/04/29 HTML / CSS
详解淘宝H5 sign加密算法
2020/08/25 HTML / CSS
尤妮佳moony海外旗舰店:日本殿堂级纸尿裤品牌
2018/02/23 全球购物
Bose美国官网:购买Bose耳机和音箱
2019/03/10 全球购物
迪卡侬中国官网:Decathlon中国
2020/08/10 全球购物
网络事业创业计划书范文
2014/01/09 职场文书
计算机维护专业推荐信
2014/02/27 职场文书
黄金酒广告词
2014/03/21 职场文书
简单租房协议书
2014/04/09 职场文书
北京天坛导游词
2015/02/12 职场文书
4种非常实用的python内置数据结构
2021/04/28 Python
使用HBuilder制作一个简单的HTML5网页
2022/07/07 HTML / CSS