pygame游戏之旅 添加游戏界面按键图形


Posted in Python onNovember 20, 2018

本文为大家分享了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()

效果图(高亮图没有截图可以自己试试编译代码):

pygame游戏之旅 添加游戏界面按键图形

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Windows系统下使用flup搭建Nginx和Python环境的方法
Dec 25 Python
Python出现segfault错误解决方法
Apr 16 Python
python web框架学习笔记
May 03 Python
Python的Flask框架标配模板引擎Jinja2的使用教程
Jul 12 Python
用Eclipse写python程序
Feb 10 Python
Python 微信之获取好友昵称并制作wordcloud的实例
Feb 21 Python
django中的图片验证码功能
Sep 18 Python
python GUI库图形界面开发之PyQt5打印控件QPrinter详细使用方法与实例
Feb 28 Python
如何把外网python虚拟环境迁移到内网
May 18 Python
keras实现VGG16方式(预测一张图片)
Jul 07 Python
python exit出错原因整理
Aug 31 Python
Numpy中的数组搜索中np.where方法详细介绍
Jan 08 Python
pygame游戏之旅 添加游戏介绍
Nov 20 #Python
pygame游戏之旅 计算游戏中躲过的障碍数量
Nov 20 #Python
pygame游戏之旅 添加碰撞效果的方法
Nov 20 #Python
pygame游戏之旅 如何制作游戏障碍
Nov 20 #Python
用Python编写一个简单的CS架构后门的方法
Nov 20 #Python
python pygame实现2048游戏
Nov 20 #Python
python pygame模块编写飞机大战
Nov 20 #Python
You might like
使用PHP制作新闻系统的思路
2006/10/09 PHP
php checkdate、getdate等日期时间函数操作详解
2010/03/11 PHP
PHP四大安全策略
2014/03/12 PHP
设定php简写功能的方法
2019/11/28 PHP
PhpStorm2020.1 安装 debug - Postman 调用的详细教程
2020/08/17 PHP
JQuery 初体验(建议学习jquery)
2009/04/25 Javascript
jQuery调用RESTful WCF示例代码(GET方法/POST方法)
2014/01/26 Javascript
解决extjs grid 不随窗口大小自适应的改变问题
2014/01/26 Javascript
用javascript替换URL中的参数值示例代码
2014/01/27 Javascript
Jquery中CSS选择器用法分析
2015/02/10 Javascript
深入解析JavaScript中的数字对象与字符串对象
2015/10/21 Javascript
javascript设置和获取cookie的方法实例详解
2016/01/05 Javascript
利用js编写响应式侧边栏
2016/09/17 Javascript
dul无法加载bootstrap实现unload table/user恢复
2016/09/29 Javascript
vue2.0使用Sortable.js实现的拖拽功能示例
2017/02/21 Javascript
用vue2.0实现点击选中active其他选项互斥的效果
2018/04/12 Javascript
JS获取浏览器地址栏的多个参数值的任意值实例代码
2018/07/24 Javascript
JS实现的RC4加密算法示例
2018/08/16 Javascript
Javascript迭代、递推、穷举、递归常用算法实例讲解
2019/02/01 Javascript
解决layer.confirm快速点击会重复触发事件的问题
2019/09/23 Javascript
Python命令行参数解析模块optparse使用实例
2015/04/13 Python
python魔法方法-自定义序列详解
2016/07/21 Python
Python3 伪装浏览器的方法示例
2017/11/23 Python
django+xadmin+djcelery实现后台管理定时任务
2018/08/14 Python
Python常见的pandas用法demo示例
2019/03/16 Python
Python实现的爬取小说爬虫功能示例
2019/03/30 Python
python批量处理多DNS多域名的nslookup解析实现
2020/06/28 Python
python识别验证码的思路及解决方案
2020/09/13 Python
英国家庭、花园、汽车和移动解决方案:Easylife Group
2018/05/23 全球购物
高校教师思想汇报
2014/01/11 职场文书
三方合作协议书范本
2014/04/18 职场文书
美术课外活动总结
2014/07/08 职场文书
美德少年事迹材料500字
2014/08/19 职场文书
2015圣诞节贺卡寄语
2015/03/24 职场文书
客服专员岗位职责范本
2015/04/07 职场文书
2015年度优秀员工获奖感言
2015/07/31 职场文书