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 03 Python
Python实现的读取电脑硬件信息功能示例
May 30 Python
python实现自主查询实时天气
Jun 22 Python
Scrapy框架使用的基本知识
Oct 21 Python
django使用haystack调用Elasticsearch实现索引搜索
Jul 24 Python
python性能测量工具cProfile使用解析
Sep 26 Python
使用PyCharm进行远程开发和调试的实现
Nov 04 Python
Python list与NumPy array 区分详解
Nov 06 Python
PyCharm第一次安装及使用教程
Jan 08 Python
将matplotlib绘图嵌入pyqt的方法示例
Jan 08 Python
Python配置pip国内镜像源的实现
Aug 20 Python
python在package下继续嵌套一个package
Apr 14 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无法实现多线程的问题
2015/09/25 PHP
php微信支付接口开发程序
2016/08/02 PHP
javascript 无提示关闭窗口脚本
2009/08/17 Javascript
JavaScript DOM学习第四章 getElementByTagNames
2010/02/19 Javascript
ajax 缓存 问题 requestheader
2010/08/01 Javascript
extjs每个组件要设置唯一的ID否则会出错
2014/06/15 Javascript
JavaScript 开发工具webstrom使用指南
2014/12/09 Javascript
JavaScript中的splice()方法使用详解
2015/06/09 Javascript
js实现汉字排序的方法
2015/07/23 Javascript
通用无限极下拉菜单的实现代码
2016/05/31 Javascript
NodeJs——入门必看攻略
2016/06/27 NodeJs
javascript简单实现跟随滚动条漂浮的返回顶部按钮效果
2016/08/19 Javascript
Vue input控件通过value绑定动态属性及修饰符的方法
2017/05/03 Javascript
vue-resource调用promise取数据方式详解
2017/07/21 Javascript
用node-webkit把web应用打包成桌面应用(windows环境)
2018/02/01 Javascript
webpack组织模块打包Library的原理及实现
2018/03/10 Javascript
Vue3.0中的monorepo管理模式的实现
2019/10/14 Javascript
使用PreloadJS加载图片资源的基础方法详解
2020/02/03 Javascript
JS绘图Flot如何实现动态可刷新曲线图
2020/10/16 Javascript
[52:27]2018DOTA2亚洲邀请赛 3.31 小组赛B组 paiN vs Secret
2018/04/01 DOTA
python实现复制整个目录的方法
2015/05/12 Python
浅述python中argsort()函数的实例用法
2017/03/30 Python
Python人脸识别初探
2017/12/21 Python
Numpy掩码式数组详解
2018/04/17 Python
pycharm在调试python时执行其他语句的方法
2018/11/29 Python
Pycharm配置远程调试的方法步骤
2018/12/17 Python
python GUI库图形界面开发之PyQt5线程类QThread详细使用方法
2020/02/26 Python
在python3.9下如何安装scrapy的方法
2021/02/03 Python
意大利拉斐尔时尚购物网:Raffaello Network(支持中文)
2018/11/09 全球购物
一个精品风格的世界:Atterley
2019/05/01 全球购物
化工专业个人的求职信范文
2013/11/28 职场文书
毕业自荐信
2013/12/16 职场文书
竞聘上岗演讲稿范文
2014/01/10 职场文书
党员干部廉洁承诺书
2014/05/28 职场文书
小组口号大全
2014/06/09 职场文书
预备党员考察表党小组意见
2015/06/01 职场文书