pygame游戏之旅 添加游戏界面按键图形
作者:观月执白 发布时间:2023-06-30 21:48:38
标签:pygame,游戏,按键
本文为大家分享了pygame游戏之旅的第10篇,供大家参考,具体内容如下
通过获取鼠标的位置然后进行高亮显示:
mouse =pygame.mouse.get_pos()
if 150 + 100 > mouse[0] > 150 and 450 + 50 > mouse[1] > 450:
pygame.draw.rect(gameDisplay, bright_green, (150,450,100,50))
else:
pygame.draw.rect(gameDisplay, green, (150,450,100,50))
if 550 + 100 > mouse[0] > 550 and 450 + 50 > mouse[1] > 450:
pygame.draw.rect(gameDisplay, bright_red, (550,450,100,50))
else:
pygame.draw.rect(gameDisplay, red, (550,450,100,50))
全部代码:
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 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)
mouse =pygame.mouse.get_pos()
if 150 + 100 > mouse[0] > 150 and 450 + 50 > mouse[1] > 450:
pygame.draw.rect(gameDisplay, bright_green, (150,450,100,50))
else:
pygame.draw.rect(gameDisplay, green, (150,450,100,50))
if 550 + 100 > mouse[0] > 550 and 450 + 50 > mouse[1] > 450:
pygame.draw.rect(gameDisplay, bright_red, (550,450,100,50))
else:
pygame.draw.rect(gameDisplay, red, (550,450,100,50))
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/78307571
0
投稿
猜你喜欢
- 在DreamWeaver中编写CSS,这种编写习惯本站(twocity.cn)并不提倡,不过由于"可视化"和操作简便,使
- 今天搭了个“发短信”的页面,找朋友测试,没想到一位大侠直接弄了本长篇小说发我手机上……为了我的宝贝手机能继续健康澎湃,给文本区域(texta
- 控制资源访问前文提到threading库在多线程时,对同一资源的访问容易导致破坏与丢失数据。为了保证安全的访问一个资源对象,我们需要创建锁。
- 来由:最近的工作一直是学校设计教程的开发,都是自己根据理论知识加一些实际工作经验相结合写的教程,所以不会咬文嚼字,文笔也不一定非常专业和流畅
- 项目需求:将kafka解析来的日志获取到数据库的变更记录,按照订单的级别和订单明细级别写入数据库,一条订单的所有信息包括各种维度信息均保存在
- #!/usr/bin/env python#coding=utf-8import osfrom pyinotify import Watch
- 本文实例讲述了Python实现获取汉字偏旁部首的方法。分享给大家供大家参考,具体如下:功能介绍传入一个汉字,返回其偏旁部首字典分为本地字典与
- 简介这里通过python的绘图工具Matplotlib包可视化实现机器学习中的KNN算法。 需要提前安装python的Numpy和Matpl
- 用Python实现点对点的聊天,2个程序,一个是client.py,一个是server.py,通过本机地址127.0.0.1连接进行通信,利
- python中的列表和元组# 1.列表的格式# [数据1,数据2,数据3,···]# 列表 可变数据类型# 列表可以存储多个数据,数据之间的
- 1 包简介1.1 工作空间go语言的工作空间必须由 bin、pkg、src三个目录组成,可以在GOPATH环境变量中添加多个工作空间,但不能
- 二分类问题可能是应用最广泛的机器学习问题。今天我们将学习根据电影评论的文字内容将其划分为正面或负面。一、数据集来源我们使用的是IMDB数据集
- 作为设计主管,Peter Stern 已经领导 microsoft.com 重新设计了主页并且开发了五个不同的交互工具,这些工具被用于下载中
- 全选、全不选、反选这几个功能我们经常会用到,如我们可以用在文章列表管理页面,也可以用在音乐播放页面,使用全选我们可以很方便的进行批量操作,如
- 英国著名学者李约瑟早在100多年前就疑惑:为什么中国的科技在唐宋时代就已领先西方,却在明清时代落后下来。对此,人们一向认为这主要是儒家思想的
- http通过StreamingHttpResponse完成连续的数据传输长链接问题http服务之间传递结果流一个由flask封装起来的算法,
- 好久没有更新过PyQt5相关的专题了,今天正好有空就做一个文件、文件内容检索的小工具。目的是为了能够很快的完成的在任意磁盘路径下找到我们需要
- 1. 背景 Scrapy 是一个通用的爬虫框架,但是不支持分布式,Scrapy-redis是为了更方便地实现Scrapy分布式爬取,而提供了
- 1:创建用户 create temporary tablespace user_temp tempfile 'D:\app\topw
- 发一个数字拼图游戏,有点小疑问前几天写得,其中一段代码还要感谢“簡簡單單愛妳”的提示,不过我还是不太明白, ,有点笨。 $(&qu