[英]Why do these objects bounce around the screen so fast. How do I slow them down
I have these blocks that I have made that are in the Enemy_Block class. 我有这些在Enemy_Block类中创建的块。 When I try to move them they like teleport around the screen.
当我试图移动它们时,他们喜欢在屏幕上传送。 How to I slow these enemy blocks down.
如何减慢这些敌人的速度。 Please help, thanks.
请帮忙,谢谢。
I have tried putting that for-loop that spawns enemy blocks in and outside the loop. 我已经尝试将那个for循环放在循环内外产生敌人块。 That's it.
而已。
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()
The expected result is that the game will create ten enemy blocks that move around the screen kinda slow because my velocity is low. 预期的结果是游戏将创建十个敌人区块,因为我的速度很低,所以在屏幕上移动有点慢。 The result is that the blocks kinda teleport around the screen, because they are moving soooo fast.
结果是这些块有点传送到屏幕周围,因为它们正在快速移动。
There are 2 issues. 有2个问题。 First, in your main game loop you are constantly spawning new enemies, and second, you're not telling your enemies to move
首先,在你的主游戏循环中,你不断产生新的敌人,其次,你不会告诉你的敌人移动
So in your main loop, change: 所以在你的主循环中,改变:
Enemy_List = []
for i in range(10):
Enemy = Enemy_Block()
Enemy_List.append(Enemy)
to: 至:
for e in Enemy_List:
e.move_random()
You have already created your 10 enemies outside of the main loop, so no need to keep respawning them. 你已经在主循环之外创建了10个敌人,所以不需要继续重生它们。 Instead, you can just call
move_random()
on each one to move them around the screen 相反,您可以在每个上调用
move_random()
以在屏幕上移动它们
This seems undesirable: 这似乎不合需要:
Enemy_List = []
for i in range(10):
Enemy = Enemy_Block()
Enemy_List.append(Enemy)
You are producing new randomly initialized enemy blocks each time through the event loop. 每次通过事件循环时,您都会生成新的随机初始化的敌人块。 You want to init just once, before the event loop begins, and then let them
move_random()
within the loop. 你想在事件循环开始之前初始化一次,然后在循环中让它们
move_random()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.