我一直在尝试让敌人每20秒生成一次,以解决敌人在玩家正下方时卡在平台下面的问题。我已经留下了我打算使用的方法,但实施起来并不顺利。
原计划是使用Python内置的timer.tick来计时每20秒,然而我意识到这只是基于帧率的,如你所见,我在顶部使用了start = time.time()来启动计时,然后在20秒后使用end = time.time()来结束计时。
import pygame #导入Pygame
import time #导入计时器,以便使用tick函数使游戏达到60fps
start= time.time()#为每个函数编写这个
end= time.time()
import math #导入数学模块
import sys #导入系统模块
import random
from random import *
from time import * #导入time模块的所有内容
from pygame import * #导入所有Pygame文件
from pygame.math import *
from pygame.mixer import *
print(start)
win_height = 750 #窗口高度为750像素
win_width = 1050 #窗口宽度为1050像素
half_win_width = int(win_width / 2) #用于居中摄像头
half_win_height = int(win_height / 2)
white=(255, 255, 255)
black=(0, 0, 0)
gray=(50, 50, 50)
red=(255, 0, 0)
green=(0, 255, 0)
blue=(0, 0, 255)
yellow=(255, 255, 0)
display = (win_width, win_height) #创建500*500像素的窗口
depth = 32 #防止无限递归
flags = 0 #给Les的信息:我不是很清楚这是做什么用的,但我看到很多地方都在使用,所以我认为这是重要的
camera_slack = 30 #玩家移动多少像素后摄像头会随之移动
pygame.init()
mixer.init()
pygame.mixer.music.load('Caravan Palace - Lone Digger [Clip officiel].mp3') #在我的游戏文件夹中播放音乐
#pygame.mixer.music.load('Toby Fox - Megalovania [Electro Swing Remix].mp3') #在我的游戏文件夹中播放音乐
pygame.mixer.music.play(-1) #无限循环播放音乐
myfont = pygame.font.SysFont('Comic Sans MS', 30)
def main_menu():
pygame.init()
screen = pygame.display.set_mode(display, flags, depth)
pygame.display.set_caption("超级恶魔城人")
timer = pygame.time.Clock()
def main():
#主游戏函数
global cameraX, cameraY
pygame.init()
screen = pygame.display.set_mode(display, flags, depth)
pygame.display.set_caption("超级恶魔城人")
timer = pygame.time.Clock()
move_cameraX = 0
move_cameraY = 0
up = down = left = right = running = False
background = pygame.Surface((32,32)) #背景在屏幕上占用空间
background.convert()
background.fill(pygame.Color("#000000")) #背景为黑色
entities = pygame.sprite.Group()
player = Player_class(32, 32*15) #玩家大小为32*32像素
platforms = []
x = y = 0
blank_level = [
"PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP",
"P P",
"P P",
"P P",
"P P",
"P P",
"P P",
"P P",
"P P",
"P P",
"P P",
"P P",
"P P",
"P P",
"P P",
"P P",
"P P",
"P P",
"P P",
"P P",
"P P",
"P P",
"P P",
"PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP"]
def print_level(level):
for row in level:
print(row)
new_level = blank_level #随机生成关卡
#首先处理平台
#随机选择平台数量(6 - 12)
#对于这些平台,选择没有平台的行(即只有两个P的行),然后选择一个起始单元格和长度(3-7)->确保在墙边截断
#随机选择一个空单元格放置E
for z in range (0,15):
plat_len = (randint(1, 7))
plat_start = (randint(3, 29))
row = (randint(3, 19))
new_level[row] = new_level[row][0:plat_start]+("P"*plat_len)+new_level[row][plat_start+plat_len:]
#随机选择一个空单元格放置E
new_level[5] = new_level[5][0:4]+"E"+new_level[5][5:]
for row in blank_level:
for col in row:
if col == "P":
p = Platform(x, y) #使P成为固体对象
platforms.append(p)
entities.add(p)
if col == "E":
e = Exit_block(x, y)
platforms.append(e)
entities.add(e)
x += 32
y += 32
x = 0
entities.add(player)
enemy = Enemy(60, 200, player) #生成敌人
enemy_list = pygame.sprite.Group() #创建敌人组
enemy_list.add(enemy) #将敌人添加到组中
while 1:
timer.tick(60) #使游戏以60帧每秒运行
for e in pygame.event.get(): #将event缩写为e
if e.type == QUIT:
return
if e.type == KEYDOWN and e.key == K_ESCAPE:
return
if e.type == KEYDOWN and e.key == K_UP:
up = True
move_cameraY = -10
if e.type == KEYDOWN and e.key == K_DOWN:
down = True
move_cameraY = 10
if e.type == KEYDOWN and e.key == K_LEFT:
left = True
move_cameraX = -10
if e.type == KEYDOWN and e.key == K_RIGHT:
right = True
move_cameraX = 10
if e.type == KEYDOWN and e.key == K_SPACE:
running = True
if e.type == KEYUP and e.key == K_UP:
up = False
move_cameraY = 0
if e.type == KEYUP and e.key == K_DOWN:
down = False
move_cameraY = 0
if e.type == KEYUP and e.key == K_RIGHT:
right = False
move_cameraX = 0
if e.type == KEYUP and e.key == K_LEFT:
left = False
move_cameraX = 0
if e.type == KEYUP and e.key == K_RIGHT:
right = False
# 更新游戏。
for e in enemy_list:
e.update(platforms)
player.update(up, down, left, right, running, platforms)
# 绘制所有内容。
for y in range(32): #绘制背景
for x in range(32):
screen.blit(background, (x * 32, y * 32))
entities.draw(screen)
enemy_list.draw(screen)
pygame.display.flip() # 每帧只需要一次flip或update调用。
class Entity(pygame.sprite.Sprite):
#使玩家成为精灵
def __init__(self):
pygame.sprite.Sprite.__init__(self) #设置精灵初始化
class Player_class(Entity):
#定义玩家类
def __init__(self, x, y): #x是玩家的x坐标,y是玩家的y坐标
Entity.__init__(self) #玩家是一个实体
self.xvel = 0 #玩家左右移动的速度
self.yvel = 0 #玩家上下移动的速度
self.onGround = False #假设玩家在空中
self.image = pygame.Surface((32,32)) #玩家大小为32*32像素
self.image.fill(pygame.Color("#0000FF")) #使玩家变蓝
self.rect = pygame.Rect(x, y, 32, 32)
self.x = x
self.y = y
def update(self, up, down, left, right, running, platforms):
if up:
if self.onGround:
self.yvel -= 10 #只有在地面上时才跳跃
if down:
pass
if running:
self.xvel = 12
if left:
self.xvel = -8
if right:
self.xvel = 8
if not self.onGround:
self.yvel += 0.3 #只有在空中时才受到重力加速
if self.yvel > 100: self.yvel = 100 #终端速度=100
if not(left or right):
self.xvel = 0
self.rect.left += self.xvel #下落或跳跃
self.collide(self.xvel, 0, platforms) #在x轴上创建碰撞
self.rect.top += self.yvel #在y轴上创建碰撞
self.onGround = False; #假设玩家在空中
# 进行y轴碰撞检测
self.collide(0, self.yvel, platforms)
def collide(self, xvel, yvel, platforms):
for p in platforms:
if pygame.sprite.collide_rect(self, p):
if isinstance(p, Exit_block):
pygame.quit()
sys.exit()
if xvel > 0:
self.rect.right = p.rect.left
if xvel < 0:
self.rect.left = p.rect.right
if yvel > 0:
self.rect.bottom = p.rect.top
self.onGround = True
self.yvel = 0
if yvel < 0:
self.rect.top = p.rect.bottom
class Platform(Entity):
def __init__(self, x, y):
Entity.__init__(self)
self.image = pygame.Surface((32, 32))
self.image.fill(pygame.Color("#FFFFFF"))
self.rect = pygame.Rect(x, y, 32, 32)
class Exit_block(Platform):
def __init__(self, x, y):
Platform.__init__(self, x, y)
self.image.fill(pygame.Color("#00FF00"))
#出口块是绿色的
class Enemy(Entity):
def __init__(self, x, y, player):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((32, 32))
self.xvel = 0
self.yvel = 0
self.image.fill(pygame.Color("#FF0000")) #敌人是红色的
self.rect = pygame.Rect(250, 50, 32, 32)
#
#
#
#
""" if pygame.time.Clock == 20:
self.rect = pygame.Rect(250, 50, 32, 32)
"""
self.player = player
def collide(self, xvel, yvel, platforms):
# 检查敌人是否与玩家碰撞。
if self.rect.colliderect(self.player.rect):
pygame.quit()
sys.exit()
for p in platforms:
if pygame.sprite.collide_rect(self, p):
if xvel > 0:
self.rect.right = p.rect.left
if xvel < 0:
self.rect.left = p.rect.right
if yvel > 0:
self.rect.bottom = p.rect.top
if yvel < 0:
self.rect.top = p.rect.bottom
def move(self, xvel, platforms, speed=4):
# 追逐移动
for p in platforms: #如果敌人的顶部触碰到平台的底部,将敌人向右移动
if pygame.sprite.collide_rect(self, p):
if xvel == 15:
self.rect.top = p.rect.bottom
if self.rect.x > self.player.rect.x: # 沿x方向移动
self.xvel = -speed
elif self.rect.x < self.player.rect.x:
self.xvel = speed
if self.rect.y < self.player.rect.y: # 沿y方向移动
self.yvel = speed
elif self.rect.y > self.player.rect.y:
self.yvel = -speed
def update(self, platforms):
self.move(self, platforms) # 设置速度。
self.rect.left += self.xvel
self.collide(self.xvel, 0, platforms) #在x轴上创建碰撞
self.rect.top += self.yvel #在y轴上创建碰撞
self.collide(0, self.yvel, platforms)
if __name__ == "__main__":
main()
pygame.quit()
回答:
在while循环之前定义start
,然后从开始时间中减去当前时间(now
)以查看是否已经过了所需的时间,然后将start
设置为now
。
start = pygame.time.get_ticks()
while 1:
# ... 省略的代码。
now = pygame.time.get_ticks()
# 当20000毫秒过去后。
if now - start > 20000:
start = now
enemy = Enemy(60, 200, player)
enemy_list.add(enemy)