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的Django框架中创建模板库的方法
Jul 20 Python
python判断输入日期为第几天的实例
Nov 13 Python
selenium python 实现基本自动化测试的示例代码
Feb 25 Python
简单了解Python生成器是什么
Jul 02 Python
Pandas0.25来了千万别错过这10大好用的新功能
Aug 07 Python
python实现生成Word、docx文件的方法分析
Aug 30 Python
python实现简单成绩录入系统
Sep 19 Python
Django中使用haystack+whoosh实现搜索功能
Oct 08 Python
python之pymysql模块简单应用示例代码
Dec 16 Python
使用matlab 判断两个矩阵是否相等的实例
May 11 Python
Python限制内存和CPU使用量的方法(Unix系统适用)
Aug 04 Python
Python - 10行代码集2000张美女图
May 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常用代码
2006/11/23 PHP
PHP 编程请选择正确的文本编辑软件
2006/12/21 PHP
php eval函数用法总结
2012/10/31 PHP
详解PHP如何更好的利用PHPstorm的自动提示
2017/08/18 PHP
php基于环形链表解决约瑟夫环问题示例
2017/11/07 PHP
Laravel5.1 框架表单验证操作实例详解
2020/01/07 PHP
JQuery Tips(4) 一些关于提高JQuery性能的Tips
2009/12/19 Javascript
Moment.js 不容错过的超棒Javascript日期处理类库
2012/04/15 Javascript
NodeJS Web应用监听sock文件实例
2015/02/18 NodeJs
浅谈JavaScript正则表达式分组匹配
2015/04/10 Javascript
Vue axios 中提交表单数据(含上传文件)
2017/07/06 Javascript
webpack4.x CommonJS模块化浅析
2018/11/09 Javascript
Node4-5静态资源服务器实战以及优化压缩文件实例内容
2019/08/29 Javascript
javascript json对象小技巧之键名作为变量用法分析
2019/11/11 Javascript
详解Vue3.0 + TypeScript + Vite初体验
2021/02/22 Vue.js
[48:39]Ti4主赛事胜者组第一天 EG vs NEWBEE 2
2014/07/19 DOTA
在Python的Flask框架中实现单元测试的教程
2015/04/20 Python
python对url格式解析的方法
2015/05/13 Python
python实现简单ftp客户端的方法
2015/06/28 Python
Python中with及contextlib的用法详解
2017/06/08 Python
tensorflow实现tensor中满足某一条件的数值取出组成新的tensor
2020/01/04 Python
Anaconda的安装及其环境变量的配置详解
2020/04/22 Python
python读取excel数据并且画图的实现示例
2021/02/08 Python
CSS3哪些新特性值得称赞
2016/03/02 HTML / CSS
HTML5 语义化结构化规范化
2008/10/17 HTML / CSS
采购部部长岗位职责
2014/02/06 职场文书
抽奖活动主持词
2014/03/31 职场文书
幼儿教师师德师风演讲稿
2014/08/22 职场文书
学校清洁工岗位职责
2015/04/15 职场文书
四群教育工作总结
2015/08/10 职场文书
初中语文教学随笔
2015/08/15 职场文书
员工旷工检讨书
2015/08/15 职场文书
周一问候语大全
2015/11/10 职场文书
2019年共青团工作条例最新版
2019/11/12 职场文书
MySQL提升大量数据查询效率的优化神器
2022/07/07 MySQL
苹果macOS 13开发者预览版Beta 8发布 正式版10月发布
2022/09/23 数码科技