首页 > 解决方案 > 为什么这些物体在屏幕上反弹得如此之快。我如何减慢他们的速度

问题描述

我有这些我在 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()

预期的结果是游戏将创建十个敌人块,它们在屏幕上移动有点慢,因为我的速度很低。结果是方块在屏幕上有点传送,因为它们移动得太快了。

标签: pythonpython-3.xpygame

解决方案


有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()每个人在屏幕上移动它们


推荐阅读