pygame游戏之旅 如何制作游戏障碍
作者:观月执白 发布时间:2021-03-05 00:16:28
标签:pygame,游戏障碍
本文为大家分享了pygame游戏之旅的第6篇,供大家参考,具体内容如下
定义一个障碍模型函数:
def things(thingx, thingy, thingw, thingh, color):
pygame.draw.rect(gameDisplay, color, [thingx, thingy, thingw, thingh])
在游戏循环中调用:
things(thing_startx, thing_starty, thing_width, thing_height, black)
thing_starty += thing_speed
障碍消失之后修改x值:
if thing_starty > display_height:
thing_starty = 0 - thing_height
thing_startx = random.randrange(0, display_width)
全部代码:
import pygame
import time
import random
pygame.init()
white = (255,255,255)
black = (0,0,0)
gray = (128,128,128)
red = (255,0,0)
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(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, white)
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_loop():
x = display_width * 0.45
y = display_height * 0.8
x_change = 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:
gameExit = True
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)
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)
pygame.display.update()
clock.tick(60)
crash()
#game_loop()
pygame.quit()
quit()
结果图:
来源:https://blog.csdn.net/pianzang5201/article/details/78306344


猜你喜欢
- PDO::queryPDO::query — 执行 SQL 语句,返回PDOStatement对象,可以理解为结果集(PHP 5 >=
- 在python中,通过内嵌集成re模块,程序媛们可以直接调用来实现正则匹配。本文重点给大家介绍python中正则表达式 re.findall
- 原文:Creating a Star Rater using CSS链接:http://komodomedia.com/blog/index
- 这篇文章主要介绍了python修改文件内容的3种方法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的
- 由于服务器无法上网,不得不采用离线方式安装。IDE=pycharm-community-2019.2.3,python=3.5.4。1 安装
- 本文分享了php结合ajax实现无刷新上传图片的实例代码,分享给大家,希望大家可以和小编一起学习学习,共同进步。1.引入文件<!--图
- 今天给一个客户巡检的情况下发从库没有业务的情况mysqld的cpu的一个core占用100%.查主库慢查询也没有关于写的SQL.可以说是典的
- 楔子在 TensorFlow 中,可以给一个 tensor 增加一个维度、删除一个维度,那么在 Numpy 中该怎么呢?删除维度、增加维度先
- 在我的使用SQL Server2005的新函数构造分页存储过程中,我提到了使用ROW_NUMBER()函数来代替top实现分页存储过程。 但
- 刚在网上查了一圈,好家伙,全都是那一篇文章,而且用的pycharm是老版本的,下边介绍的是pycharm2019专业版的,直接切入正题:(1
- 看了两天 go 语言,是时候练练手了。go 的 routine(例程) 和 chan(通道) 简直是神器,实现多线程(在 go 里准确的来说
- 天我们看看import的有关内容。编程时总是用到import导入,动不动就导入,很简单,但import到底是个什么功能,它的本质是什么?一.
- 大家好,我们的git专题已经更新结束了,所以开始继续给大家写一点设计模式的内容。今天给大家介绍的设计模式非常简单,叫做iterator,也就
- 使用export default导出class类首先我们要创建一个类并导出class win { getProcessInfo(
- 方法说明: 同步版的 stat() 。方法返回一个stat数组对象,包含以下信息:(以下信息为案例中读取的文件信息,非默认值){
- 涉及概念并发安全Map分段锁sync.MapCAS ( Compare And Swap )双检查分断锁type SimpleCache s
- 任务1、编写程序实现乐手弹奏乐器多态class Instrument(): def make_sound(self
- 需要用到的包:beautifulsoup4 html5lib image requests redis PyMySQLpip安装所有依赖包:
- 整理字符串输入整理用户输入的问题在编程过程中极为常见。通常情况下,将字符转换为小写或大写就够了,有时你可以使用正则表达式模块「Regex」完
- 说明:本文内容都是从Google上搜索来的,本想上http://www.alexa.com/查官方数据,访问非常慢暂且没查。使用本接口将返回