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类的专用方法实例分析
Jan 09 Python
wxPython使用系统剪切板的方法
Jun 16 Python
Python的网络编程库Gevent的安装及使用技巧
Jun 24 Python
Python爬虫代理IP池实现方法
Jan 05 Python
Python基础之getpass模块详细介绍
Aug 10 Python
python爬虫爬取快手视频多线程下载功能
Feb 28 Python
python2.7实现爬虫网页数据
May 25 Python
在python中使用with打开多个文件的方法
Jan 07 Python
python飞机大战pygame游戏之敌机出场实现方法详解
Dec 17 Python
Django重设Admin密码过程解析
Feb 10 Python
Python如何通过百度翻译API实现翻译功能
Apr 02 Python
python 遍历磁盘目录的三种方法
Apr 02 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中cookie的作用域
2008/03/27 PHP
一个php Mysql类 可以参考学习熟悉下
2009/06/21 PHP
php使用Smarty的相关注意事项及访问变量的几种方式
2011/12/08 PHP
iis下php mail函数的sendmail配置方法(官方推荐)
2012/04/25 PHP
基于PHP遍历数组的方法汇总分析
2013/06/08 PHP
详解YII关联查询
2016/01/10 PHP
php 文件下载 出现下载文件内容乱码损坏的解决方法(推荐)
2016/11/16 PHP
Mootools 1.2教程 设置和获取样式表属性
2009/09/15 Javascript
javascript插入样式实现代码
2012/02/22 Javascript
JavaScript中的onerror事件概述及使用
2013/04/01 Javascript
jquery js 重置表单 reset()具体实现代码
2013/08/05 Javascript
jquery选择器-根据多个属性选择示例代码
2013/10/21 Javascript
JavaScript实现两个Table固定表头根据页面大小自行调整
2014/01/03 Javascript
jQuery中replaceAll()方法用法实例
2015/01/16 Javascript
JavaScript判断变量是否为空的自定义函数分享
2015/01/31 Javascript
使用window.prompt()实现弹出用户输入的对话框
2015/04/13 Javascript
JS实现放大、缩小及拖拽图片的方法【可兼容IE、火狐】
2016/08/23 Javascript
JS动态计算移动端rem的解决方案
2016/10/14 Javascript
Javascript自定义事件详解
2017/01/13 Javascript
通过学习bootstrop导航条学会修改bootstrop颜色基调
2017/06/11 Javascript
浅谈webpack 构建性能优化策略小结
2018/06/13 Javascript
WEB前端性能优化的7大手段详解
2020/02/04 Javascript
[01:03:59]2018DOTA2亚洲邀请赛3月30日 小组赛B组VGJ.T VS Secret
2018/03/31 DOTA
Python子类继承父类构造函数详解
2019/02/19 Python
菲律宾购物网站:Lazada菲律宾
2018/04/05 全球购物
生物技术专业毕业生求职信范文
2013/12/14 职场文书
工程招投标邀请书
2014/01/30 职场文书
工商治理实习生的自我评价分享
2014/02/20 职场文书
教师个人自我评价范文
2014/04/13 职场文书
助残日活动总结
2014/08/27 职场文书
个人创业事迹材料
2014/12/30 职场文书
2016年教代会开幕词
2016/03/04 职场文书
创业计划书之冷饮店
2019/09/27 职场文书
CocosCreator ScrollView优化系列之分帧加载
2021/04/14 Python
SpringCloud Alibaba 基本开发框架搭建过程
2021/06/13 Java/Android
我们认为中短波广播场强仪的最佳组合
2022/04/05 无线电