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实现抓取HTML网页并以PDF文件形式保存的方法
May 08 Python
python画折线图的程序
Jul 26 Python
python特性语法之遍历、公共方法、引用
Aug 08 Python
python绘制热力图heatmap
Mar 23 Python
python字典的setdefault的巧妙用法
Aug 07 Python
Python中list循环遍历删除数据的正确方法
Sep 02 Python
python列表返回重复数据的下标
Feb 10 Python
python中tkinter窗口位置\坐标\大小等实现示例
Jul 09 Python
python中sys模块是做什么用的
Aug 16 Python
Python 程序员必须掌握的日志记录
Aug 17 Python
解决python3中os.popen()出错的问题
Nov 19 Python
python eventlet绿化和patch原理
Nov 21 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制作新闻系统的思路
2006/10/09 PHP
php集成环境xampp中apache无法启动问题解决方案
2014/11/18 PHP
PHP正则表达式过滤html标签属性(DEMO)
2016/05/04 PHP
浅析jQuery的链式调用之each函数
2010/12/03 Javascript
遍历DOM对象内的元素属性示例代码
2014/02/08 Javascript
ExtJS中设置下拉列表框不可编辑的方法
2014/05/07 Javascript
JavaScript中数据结构与算法(五):经典KMP算法
2015/06/19 Javascript
JavaScript函数的一些注意要点小结及js匿名函数
2015/11/10 Javascript
javascript生成img标签的3种实现方法(对象、方法、html)
2015/12/25 Javascript
Javascript之Math对象详解
2016/06/07 Javascript
js 判断各种数据类型的简单方法(推荐)
2016/08/29 Javascript
JS正则RegExp.test()使用注意事项(不具有重复性)
2016/12/28 Javascript
用JavaScript做简易的购物车的代码示例
2017/10/20 Javascript
vue超时计算的组件实例代码
2018/07/09 Javascript
JS实现DOM节点插入操作之子节点与兄弟节点插入操作示例
2018/07/30 Javascript
Angular7中创建组件/自定义指令/管道的方法实例详解
2019/04/02 Javascript
详解vuex之store源码简单解析
2019/06/13 Javascript
详解vue 自定义组件使用v-model 及探究其中原理
2019/10/11 Javascript
微信小程序云开发获取文件夹下所有文件(推荐)
2019/11/14 Javascript
[01:04]DOTA2:伟大的Roshan雕塑震撼来临
2015/01/30 DOTA
[02:22]完美世界DOTA2联赛PWL S3 集锦第一期
2020/12/15 DOTA
Python简单实现安全开关文件的两种方式
2016/09/19 Python
python 日期操作类代码
2018/05/05 Python
Python中的十大图像处理工具(小结)
2019/06/10 Python
Django如何自定义model创建数据库索引的顺序
2019/06/20 Python
python创建ArcGIS shape文件的实现
2019/12/06 Python
Python模拟登入的N种方式(建议收藏)
2020/05/31 Python
BISSELL官网:北美吸尘器第一品牌
2019/03/14 全球购物
生物科学专业个人求职信范文
2013/12/07 职场文书
初一学生评语大全
2014/04/24 职场文书
销售个人求职信范文
2014/04/28 职场文书
励志演讲稿300字
2014/08/21 职场文书
支部书记四风对照材料
2014/08/28 职场文书
班主任高考寄语
2015/02/26 职场文书
2016年党员公开承诺书范文
2016/03/24 职场文书
Python办公自动化之教你用Python批量识别发票并录入到Excel表格中
2021/06/26 Python