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求素数示例分享
Feb 16 Python
从Python程序中访问Java类的简单示例
Apr 20 Python
Python发送email的3种方法
Apr 28 Python
Python字符串格式化输出方法分析
Apr 13 Python
python爬虫实现教程转换成 PDF 电子书
Feb 19 Python
分析经典Python开发工程师面试题
Apr 08 Python
python调用matplotlib模块绘制柱状图
Oct 18 Python
python打印异常信息的两种实现方式
Dec 24 Python
python读写文件write和flush的实现方式
Feb 21 Python
Python 开发工具PyCharm安装教程图文详解(新手必看)
Feb 28 Python
一些关于python 装饰器的个人理解
Aug 31 Python
PyQt5实现多张图片显示并滚动
Jun 11 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中将字符串转化为整数(int) intval() printf() 性能测试
2020/03/20 PHP
php之curl实现http与https请求的方法
2014/10/21 PHP
PHP多线程之内部多线程实例分析
2015/03/09 PHP
学习php设计模式 php实现策略模式(strategy)
2015/12/07 PHP
Extjs Ajax 乱码问题解决方案
2009/04/15 Javascript
js下用层来实现select的title提示属性
2010/02/23 Javascript
JS+JSP checkBox 全选具体实现
2014/01/02 Javascript
js利用数组length属性清空和截短数组的小例子
2014/01/15 Javascript
javascript的propertyIsEnumerable()方法使用介绍
2014/04/09 Javascript
自己使用jquery写的一个无缝滚动的插件
2014/04/30 Javascript
JQuery以JSON方式提交数据到服务端示例代码
2014/05/05 Javascript
jquery加载图片时以淡入方式显示的方法
2015/01/14 Javascript
初步认识JavaScript函数库jQuery
2015/06/18 Javascript
canvas简单快速的实现知乎登录页背景效果
2017/05/08 Javascript
jQuery选择器之表单元素选择器详解
2017/09/19 jQuery
js最实用string(字符串)类型的使用及截取与拼接详解
2019/04/26 Javascript
el-select数据过多懒加载的解决(loadmore)
2019/05/29 Javascript
layui对工具条进行选择性的显示方法
2019/09/19 Javascript
微信小程序实现单个或多个倒计时功能
2020/11/01 Javascript
JavaScript实现图片放大预览效果
2020/11/02 Javascript
浅析python中SQLAlchemy排序的一个坑
2017/02/24 Python
Python书单 不将就
2017/07/11 Python
python获取微信企业号打卡数据并生成windows计划任务
2019/04/30 Python
Django的models中on_delete参数详解
2019/07/16 Python
python通过链接抓取网站详解
2019/11/20 Python
在python中利用dict转json按输入顺序输出内容方式
2020/02/27 Python
django中的数据库迁移的实现
2020/03/16 Python
Python PyQt5模块实现窗口GUI界面代码实例
2020/05/12 Python
浅谈keras.callbacks设置模型保存策略
2020/06/18 Python
html5 利用canvas实现超级玛丽简单动画
2013/09/06 HTML / CSS
楼面部长岗位职责范本
2014/02/14 职场文书
自荐信如何制作?
2014/02/21 职场文书
房展策划方案
2014/06/07 职场文书
2014年电话销售工作总结
2014/12/01 职场文书
2016十一国庆节慰问信
2015/12/01 职场文书
Redis 中使用 list,streams,pub/sub 几种方式实现消息队列的问题
2022/03/16 Redis