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解析并修改XML文档的方法
Oct 15 Python
Python使用win32com实现的模拟浏览器功能示例
Jul 13 Python
对python插入数据库和生成插入sql的示例讲解
Nov 14 Python
python自动化之Ansible的安装教程
Jun 13 Python
Python计算一个点到所有点的欧式距离实现方法
Jul 04 Python
python opencv捕获摄像头并显示内容的实现
Jul 11 Python
Python3 使用map()批量的转换数据类型,如str转float的实现
Nov 29 Python
Python面向对象之继承原理与用法案例分析
Dec 31 Python
Python连接Impala实现步骤解析
Aug 04 Python
python eventlet绿化和patch原理
Nov 21 Python
python读取excel数据并且画图的实现示例
Feb 08 Python
Python如何快速找到多个字典中的公共键(key)
Apr 29 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
phpize的深入理解
2013/06/03 PHP
PHP使用SWOOLE扩展实现定时同步 MySQL 数据
2017/04/09 PHP
PHP类与对象后期静态绑定操作实例详解
2018/12/20 PHP
laravel实现按时间日期进行分组统计方法示例
2019/03/23 PHP
JavaScript实际应用:innerHTMl和确认提示的使用
2006/06/22 Javascript
JavaScript 获得选中文本内容的方法
2009/02/15 Javascript
FileUpload 控件 禁止手动输入或粘贴的实现代码
2010/04/07 Javascript
JavaScript 通过模式匹配实现重载
2010/08/12 Javascript
有关javascript的性能优化 (repaint和reflow)
2013/04/12 Javascript
浅谈js图片前端预览之filereader和window.URL.createObjectURL
2016/06/30 Javascript
js实现图片切换(动画版)
2016/12/25 Javascript
Bootstrap源码解读模态弹出框(11)
2016/12/28 Javascript
js将字符串中的每一个单词的首字母变为大写其余均为小写
2017/01/05 Javascript
微信小程序 图片上传实例详解
2017/05/05 Javascript
vue中如何实现变量和字符串拼接
2017/06/19 Javascript
JavaScript高级函数应用之分时函数实例分析
2018/08/03 Javascript
解决Vue项目打包后打开index.html页面显示空白以及图片路径错误的问题
2019/10/25 Javascript
关于vue表单提交防双/多击的例子
2019/10/31 Javascript
vue学习笔记之过滤器的基本使用方法实例分析
2020/02/01 Javascript
在Python中操作字符串之rstrip()方法的使用
2015/05/19 Python
详解PyTorch批训练及优化器比较
2018/04/28 Python
python爬虫框架scrapy实现模拟登录操作示例
2018/08/02 Python
情人节快乐! python绘制漂亮玫瑰
2020/08/18 Python
python读取文件指定行内容实例讲解
2020/03/02 Python
python实现录屏功能(亲测好用)
2020/03/02 Python
Python urlencode和unquote函数使用实例解析
2020/03/31 Python
Tensorflow中批量读取数据的案列分析及TFRecord文件的打包与读取
2020/06/30 Python
HTML5 新旧语法标记对我们有什么好处
2012/12/13 HTML / CSS
写出一个方法实现冒泡排序
2016/07/08 面试题
工程部主管岗位职责
2013/11/17 职场文书
《浅水洼里的小鱼》听课反思
2014/02/28 职场文书
组工干部演讲稿
2014/09/02 职场文书
病假证明模板
2015/06/19 职场文书
小学四年级作文之最感动的一件事
2019/11/01 职场文书
Vue Mint UI mt-swipe的使用方式
2022/06/05 Vue.js
MySQL慢查询中的commit慢和binlog中慢事务的区别
2022/06/16 MySQL