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
0
投稿
猜你喜欢
- 自定义过滤器就是有一个或两个参数的Python函数: (输入)变量的值  
- 前言首先抛出几个问题:console.log(Boolean({}));console.log(Number([]));console.lo
- 本文实例讲述了Python基于TCP实现会聊天的小机器人功能。分享给大家供大家参考,具体如下:一 代码1、服务端程序import socke
- 移动互联网被称为“第五次科技革命”,而随着iPhone和Android等智能手机的日渐流行和iPad等平板电脑的出现,移动互联网的潜力和趋势
- 这篇文章主要介绍了Python hashlib常见摘要算法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,
- 本文通过实例为大家分享了python实现批量提取指定文件夹下同类型文件,供大家参考,具体内容如下代码import osimport shut
- “你不必严格遵守这些原则,违背它们也不会被处以宗教刑罚。但你应当把这些原则看成警铃,若违背了其中的一条,那么警铃就会响起
- Session 对象 可以使用 Session 对象存储特定用户会话所需的信息。这样,当用户在应用程序的 Web 页之间跳转时,存储在 Se
- 接上一篇终于知道python的装饰器是怎么回事,那在工作中,到底能干吗用呢?尤其对我这个只会写写脚本又不做python开发的小测试/手动无辜
- 本文介绍了asp中 adpbe.stream 的语法,各种参数使用说明,方便大家查阅。更多请看:VBScript 速查手册(语言参考) ch
- # -*- coding: utf-8 -*- import httplibfrom urllib import urlencodeimpo
- 采用双重循环。把图片进行“.”分割后名字问前面部分,那其余数据库中的 图片路径记录进行对比 采用vb的InStr函数 如果存在的话返回值&g
- -------------- 函数检索 --------------trim函数: trim() lTrim() rTrim()校验字符串是
- 很多朋友和我讨论了一些关于网站开发到底使用哪种语言最合适,尤其是到网站一定有一定流量的时候,到底那种web语言更适合做站呢,精灵这里根据自己
- 在一般的MIS应用中,会有大量的报表,此时我们可以在后台数据库编写相应的视图或存储过程,用ASP通过ADO调用以完成报表工作。下面用一个例子
- 核心代码# -*- coding: utf-8 -*-'''python读取英文文件,将每个单词按照空格分开,并将每
- 1、元旦之前受赵晨之邀作为讨论嘉宾参加了ACM组织的“人与信息社会巡讲”。2、去之前赵晨发给了我大致的讨论提纲。咣当了好几下~说实话,我是硬
- 02条件语句和while循环三目运算a = 6#原判断语句if a > 5:print(True)else:print(False)#
- 如果你是一个pandas初学者,那么不知道你会不会像我一样。在学用列表或者数组创建DataFrame时理不清怎样用数据生成以及想要形状的的D
- 这个是JS控制图片滚动的效果,当鼠标结果新闻标题时开始滚动到对应的图片,可以作为图片新闻。效果图:<!DOCTYPE HTML PUB