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使用cookie库操保存cookie详解
Mar 03 Python
python错误处理详解
Sep 28 Python
Python入门_浅谈for循环、while循环
May 16 Python
Python异常的检测和处理方法
Oct 26 Python
对python 通过ssh访问数据库的实例详解
Feb 19 Python
python使用SQLAlchemy操作MySQL
Jan 02 Python
Pycharm 2020年最新激活码(亲测有效)
Sep 18 Python
Tensorflow不支持AVX2指令集的解决方法
Feb 03 Python
python tkinter之 复选、文本、下拉的实现
Mar 04 Python
Python类及获取对象属性方法解析
Jun 15 Python
matplotlib教程——强大的python作图工具库
Oct 15 Python
Python list去重且保持原顺序不变的方法
Apr 03 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进行MySQL删除记录操作代码
2008/06/07 PHP
php笔记之:AOP的应用
2013/04/24 PHP
php使用explode()函数将字符串拆分成数组的方法
2015/02/17 PHP
PHP PDOStatement::setFetchMode讲解
2019/02/03 PHP
javascript html 静态页面传参数
2009/04/10 Javascript
动态创建script标签实现跨域资源访问的方法介绍
2014/02/28 Javascript
js实现文字跟随鼠标移动而移动的方法
2015/02/28 Javascript
jQuery xml字符串的解析、读取及查找方法
2016/03/01 Javascript
第九篇Bootstrap导航菜单创建步骤详解
2016/06/21 Javascript
在Web项目中引入Jquery插件报错的完美解决方案(图解)
2016/09/19 Javascript
详解vee-validate的使用个人小结
2017/06/07 Javascript
详解微信第三方小程序代开发
2017/06/23 Javascript
Angular学习教程之RouterLink花式跳转
2018/05/03 Javascript
基于VSCode调试网页JavaScript代码过程详解
2020/07/20 Javascript
python 从远程服务器下载东西的代码
2013/02/10 Python
python实现得到一个给定类的虚函数
2014/09/28 Python
Python3中使用PyMongo的方法详解
2017/07/28 Python
wxPython实现带颜色的进度条
2019/11/19 Python
Python如何基于selenium实现自动登录博客园
2019/12/16 Python
keras topN显示,自编写代码案例
2020/07/03 Python
Python DES加密实现原理及实例解析
2020/07/17 Python
俄罗斯在线购买飞机票、火车票、巴士票网站:Tutu.ru
2020/03/16 全球购物
请说出以下代码输出什么
2013/08/30 面试题
python+selenium小米商城红米K40手机自动抢购的示例代码
2021/03/24 Python
《梅花魂》教学反思
2014/04/30 职场文书
优秀实习生主要事迹
2014/05/29 职场文书
禁止高声喧哗的标语
2014/06/11 职场文书
社区安全生产月活动总结
2014/07/05 职场文书
学校端午节活动方案
2014/08/23 职场文书
2015年图书馆个人工作总结
2015/05/26 职场文书
MySQL pt-slave-restart工具的使用简介
2021/04/07 MySQL
Python代码,能玩30多款童年游戏!这些有几个是你玩过的
2021/04/27 Python
基于Golang 高并发问题的解决方案
2021/05/08 Golang
vue基于Teleport实现Modal组件
2021/05/31 Vue.js
详解OpenCV获取高动态范围(HDR)成像
2022/04/29 Python
SQL Server数据库的三种创建方法汇总
2023/05/08 MySQL