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 相关文章推荐
Python 实现简单的电话本功能
Aug 09 Python
Python使用pygame模块编写俄罗斯方块游戏的代码实例
Dec 08 Python
JSON Web Tokens的实现原理
Apr 02 Python
Python多线程threading和multiprocessing模块实例解析
Jan 29 Python
详解Python3 基本数据类型
Apr 19 Python
python高斯分布概率密度函数的使用详解
Jul 10 Python
python 随机森林算法及其优化详解
Jul 11 Python
如何爬取通过ajax加载数据的网站
Aug 15 Python
python pip安装包出现:Failed building wheel for xxx错误的解决
Dec 25 Python
Python迭代器Iterable判断方法解析
Mar 16 Python
Python叠加矩形框图层2种方法及效果
Jun 18 Python
Python 实现定积分与二重定积分的操作
May 26 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 CURL获取cookies模拟登录的方法
2013/11/04 PHP
PHP有序表查找之二分查找(折半查找)算法示例
2018/02/09 PHP
PHP实现微信支付(jsapi支付)流程步骤详解
2018/03/15 PHP
Swoole4.4协程抢占式调度器详解
2019/05/23 PHP
javascript的函数
2007/01/31 Javascript
jquery 简单的进度条实现代码
2010/03/11 Javascript
Prototype源码浅析 String部分(四)之补充
2012/01/16 Javascript
输入密码检测大写是否锁定js实现代码
2012/12/03 Javascript
原生js实现查找/添加/删除/指定元素的class
2013/04/12 Javascript
创建js对象和js类的方法汇总
2014/12/24 Javascript
聊一聊JS中this的指向问题
2016/06/17 Javascript
JS中动态创建元素的三种方法总结(推荐)
2016/10/20 Javascript
Javascript使用function创建类的两种方法(推荐)
2016/11/19 Javascript
JS去除字符串中空格的方法
2017/02/14 Javascript
nodeJS实现简单网页爬虫功能的实例(分享)
2017/06/08 NodeJs
结合mint-ui移动端下拉加载实践方法总结
2017/11/08 Javascript
微信小程序实现获取自己所处位置的经纬度坐标功能示例
2017/11/30 Javascript
微信小程序实现换肤功能
2018/03/14 Javascript
vue-cli3环境变量与分环境打包的方法示例
2019/02/18 Javascript
Smartour 让网页导览变得更简单(推荐)
2019/07/19 Javascript
[55:47]DOTA2上海特级锦标赛C组小组赛#2 LGD VS Newbee第三局
2016/02/27 DOTA
在Linux下调试Python代码的各种方法
2015/04/17 Python
Python的collections模块中的OrderedDict有序字典
2016/07/07 Python
python脚本实现验证码识别
2018/06/07 Python
Python八皇后问题解答过程详解
2019/07/29 Python
python单向链表的基本实现与使用方法【定义、遍历、添加、删除、查找等】
2019/10/24 Python
Python实现RabbitMQ6种消息模型的示例代码
2020/03/30 Python
瑜伽国际:Yoga International
2018/04/18 全球购物
Android面试宝典
2013/08/06 面试题
介绍一下grep命令的使用
2012/06/28 面试题
《天安门广场》教学反思
2014/04/23 职场文书
材料成型及控制工程专业求职信
2014/06/19 职场文书
试用期转正后的自我评价
2014/09/21 职场文书
干部外出学习心得体会
2016/01/18 职场文书
小学生优秀作文范文(六篇)
2019/07/10 职场文书
Python包管理工具pip的15 个使用小技巧
2021/05/17 Python