python - pygame秒表和定义一个盒子
问题描述
我正在制作一个关于抢劫的游戏,听起来很棒:) 但我有问题。我的玩家可以左右移动(没关系),房间的尽头是一个保险箱,但我怎么能提醒 pygame 我在同一区域。这是另一个问题,当我进入时,(我说如果 x>player >= x_safe:(这是一个非常糟糕的解决方案),是我需要在安全区域内停留 3 秒,你会在代码中看到下面我把
if x>player >= x_safe:
now = time.time()
unlocked = now + 3
if unlocked < now: # WHICH WILL NEVER HAPPEN BECOUSE OF WHILE LOOP
这是代码:
import pygame
import time
import random
pygame.init()
display_width = 800
display_height = 600
gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption("Breaking in")
clock = pygame.time.Clock()
walkRight = [pygame.image.load('R1.png'), pygame.image.load('R2.png'), pygame.image.load('R3.png'), pygame.image.load('R4.png'), pygame.image.load('R5.png'), pygame.image.load('R6.png'), pygame.image.load('R7.png'), pygame.image.load('R8.png'), pygame.image.load('R9.png')]
walkLeft = [pygame.image.load('L1.png'), pygame.image.load('L2.png'), pygame.image.load('L3.png'), pygame.image.load('L4.png'), pygame.image.load('L5.png'), pygame.image.load('L6.png'), pygame.image.load('L7.png'), pygame.image.load('L8.png'), pygame.image.load('L9.png')]
#bg = pygame.image.load('bg.jpg')
char = pygame.image.load("standing.png")
safe = pygame.image.load("sef.png")
right = False
left = False
walkCount = 0
playerSkin = pygame.image.load("player.png")
guardSkin = pygame.image.load("guard.png")
#guardSkin_flip = pygame.image.load("guard_fliped.png")
#gajbaImg = pygame.image.load("gajba.png")
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
class Background(pygame.sprite.Sprite):
def __init__(self, image_file, location):
pygame.sprite.Sprite.__init__(self) #call Sprite initializer
self.image = pygame.image.load("background.png")
self.rect = self.image.get_rect()
self.rect.left, self.rect.top = location
def text_objects(text,font):
textSurface = font.render(text,True,black)
return textSurface,textSurface.get_rect()
def sefplace(x,y):
gameDisplay.blit(safe,(x,y))
#560,336
def message_dispaly(text,size):
largeText = pygame.font.Font("freesansbold.ttf",size)
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()
#224,365
def guard(x,y):
gameDisplay.blit(guardSkin,(x,y))
def game_loop():
global walkCount
global left
global right
safe_width = 50
safe_height = 51
sef_x = 560
sef_y = 300
player_width = 64
player_height = 64
BackGround = Background("background.png",[0,0])
guard_range = 100
x_player = (20)
y_player = (display_height/2)
x_guard = (650)
y_guard = (display_height/2)
x_change = 0
guard_speed = 1.3
catched = False
while not catched:
for event in pygame.event.get():
print(event)
if event.type == pygame.QUIT:
catched = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -2.3
left = True
right = False
elif event.key == pygame.K_RIGHT:
x_change = 2.3
left = False
right = True
if event.key == pygame.K_q:
pygame.quit()
quit()
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
right = False
left = False
walkCount = 0
x_change = 0
#458 max
x_guard -= guard_speed
if x_guard < 499:
guard_speed *= -1
guardSkin = pygame.image.load("guard_fliped.png")
elif x_guard > 750:
guard_speed *= -1
if x_player < 5:
x_player = 5
#if x_player > x_guard - guard_range:
#message_dispaly("You got Catched !",65)
gameDisplay.fill([255,255,255])
gameDisplay.blit(BackGround.image,BackGround.rect)
sefplace(sef_x,sef_y)
#guard(x_guard,y_guard)
if left:
gameDisplay.blit(walkLeft[walkCount%3],(x_player,y_player))
walkCount += 1
elif right:
gameDisplay.blit(walkRight[walkCount%3],(x_player,y_player))
walkCount += 1
else:
gameDisplay.blit(char,(x_player,y_player))
x_player += x_change
x_player += x_change
if x_player >= sef_x and x_player:
print("upali smo")
#now = time.time()
#unlocked = now + 4
#print("Ovo je sada " + str(now))
#print("Ovo je otkljucano " + str(unlocked))
#if unlocked < now:
#message_dispaly("Otkljucano",50)
pygame.display.update()
clock.tick(27)
game_loop()
pygame.quit()
quit()
解决方案
您可以使用 定义一个框pygame.Rect(x, y, width, height)
。
一个简单的秒表可以用一个字体对象和一个时间变量来表示。
time = 0
dt = clock.tick(60) / 1000
# In the loop
time += dt
myFont.render(str(round(time, 1)), True, (255, 255, 255))
screen.blit(myFont, (0, 0))
dt 代表增量时间,这是将 fps 转换为实际秒数的好方法。
如果你想在碰撞时检查时间是否为三,这里有一些代码
if myRect.collidepoint(player.pos.x, player.pos.y):
time += dt
if time >= 3:
# Do Stuff
推荐阅读
- list - 在 F# List of List 中查找唯一记录
- c# - 如何将核心类库项目中的服务器路径获取到静态方法中
- java - 使用 JavaFX 时导致“无法找到或加载主类”的 JAR 文件
- javascript - 展平 JSON 数据
- android - 使用 cordova-plugin-media-capture 重新启动视频捕获回调的应用程序
- java - 分区相等子集和自顶向下 TLE
- java - 如何从同一实体的数组列表创建实体的不同实例
- c# - Unity3D Photon调用本地播放器断开前的方法
- javascript - 猫头鹰旋转木马全高给出空白页
- json - 将 xquery 结果存储为 json