pygame游戏之旅 按钮上添加文字的方法
作者:观月执白 发布时间:2022-09-28 23:41:39
标签:pygame,按钮,添加文字
本文为大家分享了pygame游戏之旅的第11篇,供大家参考,具体内容如下
定义一个button函数,将文字,颜色等作为参数。
def button (msg, x, y, w, h, ic, ac):
mouse =pygame.mouse.get_pos()
if x + w > mouse[0] > x and y + h > mouse[1] > y:
pygame.draw.rect(gameDisplay, ac, (x,y,w,h))
else:
pygame.draw.rect(gameDisplay, ic, (x,y,w,h))
smallText = pygame.font.Font("freesansbold.ttf", 20)
textSurf, textRect = text_objects(msg, smallText)
textRect.center = ( (x+(w/2)), (y+(h/2)))
gameDisplay.blit(textSurf, textRect)
全部代码为:
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.Font('freesansbold.ttf',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):
mouse =pygame.mouse.get_pos()
if x + w > mouse[0] > x and y + h > mouse[1] > y:
pygame.draw.rect(gameDisplay, ac, (x,y,w,h))
else:
pygame.draw.rect(gameDisplay, ic, (x,y,w,h))
smallText = pygame.font.Font("freesansbold.ttf", 20)
textSurf, textRect = text_objects(msg, smallText)
textRect.center = ( (x+(w/2)), (y+(h/2)))
gameDisplay.blit(textSurf, textRect)
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.Font('freesansbold.ttf',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)
button("Quit",550, 450, 100, 50, red, bright_red)
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()
结果图:
来源:https://blog.csdn.net/pianzang5201/article/details/78404137
0
投稿
猜你喜欢
- 本文为大家分享了php运行环境搭建安装图文教程,供大家参考,具体内容如下安装apache:1,不要安装到有中文的目录中:2,尽量将apach
- 1.文件的读取操作文件的第一步就是得打开要操作的文件,然后进行读取文件,最后关闭文件。在python中我们可以使用open函数来打开一个文件
- 目录1. 警告不是异常2. 警告能捕获吗3. 捕获警告方法一4. 捕获警告方法二5. 捕获警告方法三1. 警告不是异常你是不是经常在使用一些
- <%DIM LinkArray(4,2)' 定义链接数组LinkArray(0,1)=&quo
- 我们可以用DataFrame的apply函数实现对多列,多行的操作。需要记住的是,参数axis设为1是对列进行操作,参数axis设为0是对行
- 本文实例讲述了Python使用回溯法解决迷宫问题。分享给大家供大家参考,具体如下:问题给定一个迷宫,入口已知。问是否有路径从入口到出口,若有
- 1.不要放过任何一个看上去很简单的小编程问题——他们往往并不那么简单,或者可以引伸出很多知识点;2.会用asp,并不说明你会asp;3.看a
- 序列化是将对象的状态信息转换为可以存储或传输的形式的过程。在序列化期间,对象将其当前状态(存在内存中)写入到临时或持久性存储区(硬盘)。以后
- 1. 背景 Scrapy 是一个通用的爬虫框架,但是不支持分布式,Scrapy-redis是为了更方便地实现Scrapy分布式爬取,而提供了
- 很久之前就对jQuery.animate的实现非常感兴趣,不过前段时间很忙,直到前几天端午假期才有时间去研究。jQuery.animate的
- 初级的图像拼接为将两幅图像简单的粘贴在一起,仅仅是图像几何空间的转移与合成,与图像内容无关。高级图像拼接也叫作基于特征匹配的图像拼接,拼接时
- import requestsimport reimport jsonimport ossession = requests.session
- 一、图像噪声图像噪声是图像在获取或者传输过程中受到随机信号干扰,妨碍人们对图像理解及分析处理的信号。很多时候将图像看作随机过程,因而描述噪声
- 原文地址:30 Days of Mootools 1.2 Tutorials - Day 4 - Functions函数和MooTools
- 以下所描述无理论依据,纯属经验谈。MySQL使用4.1以上版本,管他是什么字符集,一律使用默认。不用去设置MySQL。然后举个使用GB231
- 本文实例为大家分享了Bootstrap实现渐变顶部固定自适应导航栏的具体代码,供大家参考,具体内容如下具体代码如下所示:<!DOCTY
- 本文实例讲述了Python文件去除注释的方法。分享给大家供大家参考。具体实现方法如下:#!/usr/bin/python # -*- cod
- <%''调用例子'Dim int_RPP,int_Start,int_showNumberLink
- Python 输出 "Hello, World!",英文没有问题,但是如果你输出中文字符"你好,世界"
- 写在前面ufunc是universal function的缩写,意思是这些函数能够作用于narray对象的每一个元素上,而不是针对narra