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中捕捉详细异常信息的代码示例
Sep 18 Python
Python中几种操作字符串的方法的介绍
Apr 09 Python
python中尾递归用法实例详解
Apr 28 Python
Python中的getopt函数使用详解
Jul 28 Python
python安装mysql-python简明笔记(ubuntu环境)
Jun 25 Python
python生成器,可迭代对象,迭代器区别和联系
Feb 04 Python
django加载本地html的方法
May 27 Python
python中的句柄操作的方法示例
Jun 20 Python
对pytorch中的梯度更新方法详解
Aug 20 Python
Python Web静态服务器非堵塞模式实现方法示例
Nov 21 Python
Pytorch基本变量类型FloatTensor与Variable用法
Jan 08 Python
Python如何读取文件中图片格式
Jan 13 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 ? EasyUI DataGrid 资料取的方式介绍
2012/11/07 PHP
使用php方法curl抓取AJAX异步内容思路分析及代码分享
2014/08/25 PHP
ThinkPHP实现ajax仿官网搜索功能实例
2014/12/02 PHP
PHP中使用file_get_contents抓取网页中文乱码问题解决方法
2014/12/17 PHP
Laravel 5框架学习之表单
2015/04/08 PHP
thinkphp中多表查询中防止数据重复的sql语句(必看)
2016/09/22 PHP
jquery批量设置属性readonly和disabled的方法
2014/01/24 Javascript
Javascript MVC框架Backbone.js详解
2014/09/18 Javascript
实例详解jQuery结合GridView控件的使用方法
2016/01/04 Javascript
jQuery利用sort对DOM元素进行排序操作
2016/11/07 Javascript
JavaScript登录记住密码操作(超简单代码)
2017/03/22 Javascript
nodejs结合Socket.IO实现的即时通讯功能详解
2018/01/12 NodeJs
详解Vue内部怎样处理props选项的多种写法
2018/11/06 Javascript
35个最好用的Vue开源库(史上最全)
2019/01/03 Javascript
解决vue v-for src 图片路径问题 404
2019/11/12 Javascript
JavaScript This指向问题详解
2019/11/25 Javascript
JavaScript数组去重实现方法小结
2020/01/17 Javascript
Element PageHeader页头的使用方法
2020/07/26 Javascript
python装饰器使用方法实例
2013/11/21 Python
Python机器学习之决策树算法实例详解
2017/12/06 Python
Python Django 页面上展示固定的页码数实现代码
2019/08/21 Python
python BlockingScheduler定时任务及其他方式的实现
2019/09/19 Python
Django 构建模板form表单的两种方法
2020/06/14 Python
django haystack实现全文检索的示例代码
2020/06/24 Python
利用python进行文件操作
2020/12/04 Python
美国在线购买和出售礼品卡网站:EJ Gift Cards
2019/06/09 全球购物
Noon埃及:埃及在线购物
2019/11/26 全球购物
俄罗斯金苹果网上化妆品和香水商店:Goldapple
2019/12/01 全球购物
审计主管岗位职责
2014/01/31 职场文书
安全技术说明书
2014/05/09 职场文书
党的群众教育实践活动实施方案
2014/06/12 职场文书
家具公司总经理岗位职责
2014/07/08 职场文书
开除通知书范本
2015/04/25 职场文书
创建文明城市倡议书
2015/04/28 职场文书
实习感想范文
2015/08/10 职场文书
2019年消防宣传标语集锦
2019/11/21 职场文书