I have these blocks that I have made that are in the Enemy_Block class. 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. 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 so fast.
There are 2 issues. 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. Instead, you can just call move_random()
on each one to move them around the screen