网络编程
位置:首页>> 网络编程>> Python编程>> pygame游戏之旅 调用按钮实现游戏开始功能

pygame游戏之旅 调用按钮实现游戏开始功能

作者:观月执白  发布时间:2023-04-19 06:36:44 

标签:pygame,按钮,游戏

本文为大家分享了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游戏之旅 调用按钮实现游戏开始功能

来源:https://blog.csdn.net/pianzang5201/article/details/78404711

0
投稿

猜你喜欢

  • 我们或多或少都使用过各式各样的富文本编辑器,其中有一个很方便功能,复制一张图片然后粘贴进文本框,这张图片就被上传了,那么这个方便的功能是如何
  • 本文实例讲述了Python贪心算法。分享给大家供大家参考,具体如下:1. 找零钱问题:假设只有 1 分、 2 分、五分、 1 角、二角、 五
  • 本文的目的是讨论Python中 __new__ 和 __ini___ 的用法。 __new__ 和 __init__ 的区别主要表现在:1.
  • 1. 游戏是更注重于体验的产品,所以应该将游戏本省做得更加炫动和增加参与感觉。2. 网络游戏和单击游戏的区别在于社会化的添加,所以运用好这样
  • Celery 简介除了redis,还可以使用另外一个神器---Celery。Celery是一个异步任务的调度工具。Celery 是 Dist
  • 本文主要向大家介绍的是正确优化SQL Server数据库的经验总结,其中包括在对其进行优化的实际操作中值得大家注意的地方描述,以及对SQL语
  • 有部分老项目是在Eclipse环境开发的,最近公司要求应用瘦身,老项目也在其中。如果在 AS 下开发就不会有这样的问题,但是在 Eclips
  • 在 Web 2.0 时代,开发人员和用户都对基于 Web 的应用程序的可用性和响应性抱有很高的期望。除非是在过去的两年内一直没有关注过这个领
  • 一个更易读的网站意味着网站使用性的改良以及提供愉悦的阅读体验。我们希望浏览者们能或者这些好处不是吗?这篇文章我们将介绍5个简单的方法让你能提
  • 不管是哪个版本的Ubuntu,安装mysql数据库基本上都是大同小异。下面介绍一下具体的安装步骤:1、打开终端,并取得root权限2、在终端
  • 下学期就要学习MySQL了,没事先在家搞一搞,没想到光安装就费了半天劲,所以我决定整理下,供大家参考。第一步 下载安装包:官网毕竟是甲骨文公
  • 本文实例为大家分享了vue实现价格日历效果的具体代码,供大家参考,具体内容如下1、效果图2、下载全局安装:npm install ele-c
  • 大部分语言,例如c语言,交换两个变量的值需要使用中间变量。例如交换a,b伪代码:tmp = aa = bb = tmppython里面可以实
  • 特点:不需要另外加个清除DIV:after(伪对象)--设置在对象后发生的内容,通常和content配合使用,IE不支持此伪对象,非Ie 浏
  • JavaScript中没有Trim函数,VBScript语言中才有这个函数,就是去掉字符串头和尾的空格。您可以访问这篇文章:《增加 java
  • 从一段指定的字符串中,取得期望的数据,正常人都会想到正则表达式吧?写过正则表达式的人都知道,正则表达式入门不难,写起来也容易。但是正则表达式
  • 思路复原魔方困难问题的分解:1、用合适的数据结构表示出三阶魔方的六个面以及每一面的颜色2、每一次不同旋转操作的实现3、复原时如何判断当前魔方
  • 从MySQL支持Unicode后,为了与时俱进,我们的web程序也开始考虑用UTF8了。其实UTF8也用了好几年了,程序基本能跑,没什么大问
  • 本文实例分析了python开发之str.format()用法。分享给大家供大家参考,具体如下:格式化一个字符串的输出结果,我们在很多地方都可
  • gRPCgRPC远程过程调用框架是基于动作的模式,类似远程调用微服务。这使得gRPC成为一种围绕Protobufs构建的进程间通信(IPC)
手机版 网络编程 asp之家 www.aspxhome.com