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中实现两个字典(dict)合并的方法
Sep 23 Python
尝试使用Python多线程抓取代理服务器IP地址的示例
Nov 09 Python
Python学习小技巧之列表项的推导式与过滤操作
May 20 Python
python实现ID3决策树算法
Dec 20 Python
对python中的高效迭代器函数详解
Oct 18 Python
对python中数据集划分函数StratifiedShuffleSplit的使用详解
Dec 11 Python
简单了解python变量的作用域
Jul 30 Python
python内存管理机制原理详解
Aug 12 Python
SELENIUM自动化模拟键盘快捷键操作实现解析
Oct 28 Python
Python单元测试及unittest框架用法实例解析
Jul 09 Python
如何在scrapy中集成selenium爬取网页的方法
Nov 18 Python
Python+Appium自动化测试的实战
Jun 30 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中=赋值操作符对不同数据类型的不同行为
2011/01/02 PHP
php去除换行(回车换行)的三种方法
2014/03/26 PHP
通过php删除xml文档内容的方法
2015/01/23 PHP
利用PHPExcel实现Excel文件的写入和读取
2017/04/26 PHP
Nginx下ThinkPHP5的配置方法详解
2017/08/01 PHP
jQuery中文入门指南,翻译加实例,jQuery的起点教程
2007/02/09 Javascript
Easyui form combobox省市区三级联动
2016/01/13 Javascript
Webpack打包慢问题的完美解决方法
2017/03/16 Javascript
JavaScrpt的面向对象全面解析
2017/05/09 Javascript
基于js粘贴事件paste简单解析以及遇到的坑
2017/09/07 Javascript
详解从Vue.js源码看异步更新DOM策略及nextTick
2017/10/11 Javascript
AngularJS中下拉框的高级用法示例
2017/10/11 Javascript
基于Vue-cli快速搭建项目的完整步骤
2018/11/03 Javascript
使用vue开发移动端管理后台的注意事项
2019/03/07 Javascript
vue实现菜单切换功能
2019/05/08 Javascript
详解微信小程序支付流程与梳理
2019/07/16 Javascript
解决vue项目F5刷新mounted里的函数不执行问题
2019/11/05 Javascript
vue 关闭浏览器窗口的时候,清空localStorage的数据示例
2019/11/06 Javascript
Vue数据双向绑定底层实现原理
2019/11/22 Javascript
Python内置的字符串处理函数整理
2013/01/29 Python
使用python提取html文件中的特定数据的实现代码
2013/03/24 Python
使用11行Python代码盗取了室友的U盘内容
2018/10/23 Python
对python生成业务报表的实例详解
2019/02/03 Python
Python Flask上下文管理机制实例解析
2020/03/16 Python
python实现贪吃蛇游戏源码
2020/03/21 Python
阿联酋网上花店:Ferns N Petals
2018/02/14 全球购物
Dr. Martens马汀博士法国官网:马丁靴鼻祖
2020/01/15 全球购物
什么时候用assert
2015/05/08 面试题
令人啧啧称赞的经理推荐信
2013/11/07 职场文书
应届毕业生应聘自荐信
2013/12/07 职场文书
生日邀请函范文
2014/01/13 职场文书
查摆剖析材料范文
2014/09/30 职场文书
查摆问题自查报告范文
2014/10/13 职场文书
诚信考试承诺书范文
2015/04/29 职场文书
2016国培学习心得体会
2016/01/08 职场文书
浅谈Redis的事件驱动模型
2022/05/30 Redis