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 相关文章推荐
合并Excel工作薄中成绩表的VBA代码,非常适合教育一线的朋友
Apr 09 Python
使用python调用浏览器并打开一个网址的例子
Jun 05 Python
python求解水仙花数的方法
May 11 Python
在Django的模板中使用认证数据的方法
Jul 23 Python
深入理解Python装饰器
Jul 27 Python
详解python之简单主机批量管理工具
Jan 27 Python
python清理子进程机制剖析
Nov 23 Python
详解tensorflow训练自己的数据集实现CNN图像分类
Feb 07 Python
Python利用scapy实现ARP欺骗的方法
Jul 23 Python
python如何通过闭包实现计算器的功能
Feb 22 Python
使用gunicorn部署django项目的问题
Dec 30 Python
Python合并多张图片成PDF
Jun 09 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和ACCESS写聊天室(七)
2006/10/09 PHP
PHP 时间日期操作实战
2011/08/26 PHP
php+xml编程之xpath的应用实例
2015/01/24 PHP
PHP版QQ互联OAuth示例代码分享
2015/07/05 PHP
使用Appcan客户端自动更新PHP版本号(全)
2015/07/31 PHP
浅谈PHP中new self()和new static()的区别
2017/08/11 PHP
PHP注释语法规范与命名规范详解篇
2018/01/21 PHP
JS中toFixed()方法引起的问题如何解决
2012/11/20 Javascript
js和jquery使按钮失效为不可用状态的方法
2014/01/26 Javascript
JavaScript实现的图像模糊算法代码分享
2014/04/22 Javascript
js键盘事件的keyCode
2014/07/29 Javascript
浅析Node.js查找字符串功能
2014/09/03 Javascript
Javascript 是你的高阶函数(高级应用)
2015/06/15 Javascript
jQuery+CSS3+Html5实现弹出层效果实例代码(附源码下载)
2016/05/16 Javascript
Bootstrap导航条学习使用(二)
2017/02/08 Javascript
解决ionic和angular上拉加载的问题
2017/08/03 Javascript
图文介绍Vue父组件向子组件传值
2018/02/17 Javascript
详解mpvue小程序中怎么引入iconfont字体图标
2018/10/01 Javascript
[44:41]Fnatic vs Liquid 2018国际邀请赛小组赛BO2 第二场 8.16
2018/08/17 DOTA
Jupyter notebook远程访问服务器的方法
2018/05/24 Python
python自动化测试之DDT数据驱动的实现代码
2019/07/23 Python
Python中字典与恒等运算符的用法分析
2019/08/22 Python
tensorflow生成多个tfrecord文件实例
2020/02/17 Python
python Zmail模块简介与使用示例
2020/12/19 Python
python 将html转换为pdf的几种方法
2020/12/29 Python
上班早退检讨书
2014/01/09 职场文书
《乡愁》教学反思
2014/02/18 职场文书
大学军训感言800字
2014/02/27 职场文书
高考寄语大全
2014/04/08 职场文书
家具商场的活动方案
2014/08/16 职场文书
故意伤害人身损害赔偿协议书
2014/11/19 职场文书
死亡赔偿协议书
2015/01/28 职场文书
党校毕业个人总结
2015/02/28 职场文书
OpenCV-Python 实现两张图片自动拼接成全景图
2021/06/11 Python
如何用Python搭建gRPC服务
2021/06/30 Python
Redis实现主从复制方式(Master&Slave)
2022/06/21 Redis