我已经在Enemy_Block类中创建了这些块。当我尝试移动它们时,它们就像在屏幕上传送一样。我如何减慢这些敌人的速度。请帮助,谢谢。
我曾尝试将for循环放入循环内外的敌方方块。就是这样。
from random import randint
from pygame.locals import *
import pygame
import sys
# intalize Pygame
pygame.init()
# Making User Controled Block
class User_Block:
def __init__(self):
self.x_cor = 300
self.y_cor = 300
self.length = 20
self.width = 20
self.color = GREEN
self.move_x = 0
self.move_y = 0
self.rect = pygame.draw.rect(screen,self.color,[self.x_cor,self.y_cor,self.length,self.width],0)
def draw(self):
pygame.draw.rect(screen,self.color,[self.x_cor,self.y_cor,self.length,self.width],0)
self.y_cor += self.move_y
self.x_cor += self.move_x
if self.x_cor == x_size - self.width:
self.x_cor = 0
self.move_x = 0
elif self.x_cor == 0 - self.length:
self.x_cor = x_size
self.move_x = 0
elif self.y_cor == y_size - self.width:
self.y_cor = 0
self.move_y = 0
elif self.y_cor == 0 - self.length:
self.y_cor = y_size
self.move_y = 0
self.rect = pygame.draw.rect(screen,self.color,[self.x_cor,self.y_cor,self.length,self.width],0)
# Making Enemys
class Enemy_Block:
def __init__(self):
self.x_cor = randint(100,500)
self.y_cor = randint(100,500)
self.length = randint(10,100)
self.width = randint(10,100)
self.color = (255,0,255)
self.x_vel = randint(-5,5)
self.y_vel = randint(-5,5)
pygame.draw.rect(screen,self.color,[self.x_cor,self.y_cor,self.length,self.width],5)
def move_random(self):
if self.y_cor > y_size or self.y_cor < 0:
self.y_vel = -self.y_vel
elif self.x_cor > x_size or self.x_cor < 0:
self.x_vel = -self.x_vel
self.y_cor += self.y_vel
self.x_cor += self.x_vel
pygame.draw.rect(screen,self.color,[self.x_cor,self.y_cor,self.length,self.width],5)
# Set Up Screen
x_size = 1200
y_size = 700
screen = pygame.display.set_mode((x_size, y_size))
# Varible Used "while" Loop
done = False
# Setting Caption of Pygame Tab
pygame.display.set_caption("Block Rush Game")
# Colors
WHITE = (255,255,255)
BLACK = (0,0,0)
RED = (255,0,0)
GREEN = (0,255,0)
# User Controled Block
Block = User_Block()
# Enemys
Enemy_List = []
for i in range(10):
Enemy = Enemy_Block()
Enemy_List.append(Enemy)
# Most important code here
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
#Moving Character
if event.type == pygame.KEYDOWN:
if event.key == K_w:
Block.move_y = -5
elif event.key == K_s:
Block.move_y = 5
elif event.key == K_a:
Block.move_x = -5
elif event.key == K_d:
Block.move_x = 5
elif event.type == pygame.KEYUP:
if event.key == K_w or event.key == K_s:
Block.move_y = 0
elif event.key == K_a or event.key == K_d:
Block.move_x = 0
# Fill the Screen Black
screen.fill(BLACK)
# Activate draw function in Block
Block.draw()
#Spawn Enemy Blocks
Enemy_List = []
for i in range(10):
Enemy = Enemy_Block()
Enemy_List.append(Enemy)
# FPS
Clock = pygame.time.Clock()
Clock.tick(60)
# Keep Updating the Screen
pygame.display.update()
pygame.quit()
预期结果是该游戏将创建十个敌人方块,因为我的速度太慢,它们会在屏幕上移动得有些慢。结果是,这些块有点在屏幕上传送,因为它们移动得很快。
答案 0 :(得分:3)
有2个问题。首先,在游戏的主要循环中,您不断产生新的敌人,其次,您并没有告诉敌人移动。
因此,在您的主循环中,更改:
Enemy_List = []
for i in range(10):
Enemy = Enemy_Block()
Enemy_List.append(Enemy)
收件人:
for e in Enemy_List:
e.move_random()
您已经在主循环之外创建了10个敌人,因此无需继续重生它们。取而代之的是,您只需在每个电话上拨打move_random()
,即可在屏幕上移动
答案 1 :(得分:1)
这似乎是不可取的:
Enemy_List = []
for i in range(10):
Enemy = Enemy_Block()
Enemy_List.append(Enemy)
您每次通过事件循环都将生成新的随机初始化的敌人方块。
您只想在事件循环开始之前初始化一次,
然后让它们move_random()
进入循环。