I spent a long time trying to make the diagonal movement in the form of code but I don’t know how to write it. the code I have so far is almost complete but it is missing the pixelart that I will add later
import pygame
class game():
def __init__(self):
self.screen = pygame.display.set_mode((600, 600))
self.square = pygame.Rect((250, 250), (100, 100))
self.leftframe = pygame.Rect((40, 40), (10, 510))
self.rightframe = pygame.Rect((550, 40), (10, 520))
self.frameup = pygame.Rect((40, 40), (520, 10))
self.framedown = pygame.Rect((40, 550), (510, 10))
self.enemy = pygame.Rect((100, 100), (50, 50))
self.enemy2 = pygame.Rect((450, 300), (50, 50))
self.enemy3 = pygame.Rect((300, 500), (50, 50))
self.directionenemy = 1
self.directionememy2 = 1
self.directionenemy3 = 1
def play(self):
self.clean_screen()
self.draw_borders()
self.hear_events()
self.draw_square()
self.move_and_draw_enemy()
self.move_and_draw_enemy2()
self.move_and_draw_enemy3()
self.check_for_contact()
self.check_for_contact2()
self.check_for_contact3()
self.update_screen()
def move_and_draw_enemy(self):
self.enemy.move_ip(self.directionenemy, 0)
if self.enemy.colliderect(self.rightframe):
self.directionenemy = -1
if self.enemy.colliderect(self.leftframe):
self.directionenemy = 1
pygame.draw.rect(self.screen, (255, 0, 0), self.enemy)
def move_and_draw_enemy2(self):
self.enemy2.move_ip(0, self.directionememy2)
if self.enemy2.colliderect(self.frameup):
self.directionememy2 = 1
if self.enemy2.colliderect(self.framedown):
self.directionememy2 = -1
pygame.draw.rect(self.screen, (10, 50, 255), self.enemy2)
def move_and_draw_enemy3(self):
pygame.draw.rect(self.screen, (50, 50, 50), self.enemy3)
def check_for_contact(self):
if self.square.colliderect(self.enemy):
self.directionenemy = 0
elif self.directionenemy == 0 and not self.square.colliderect(self.enemy):
self.directionenemy = 1
def check_for_contact2(self):
if self.square.colliderect(self.enemy2):
self.directionememy2 = 0
elif self.directionememy2 == 0 and not self.square.colliderect(self.enemy2):
self.directionememy2 = 1
def check_for_contact3(self):
if self.square.colliderect(self.enemy3):
self.directionenemy3 = 0
elif self.directionenemy3 == 0 and not self.square.colliderect(self.enemy3):
self.directionenemy3 = 1
def clean_screen(self):
self.screen.fill((255, 255, 255))
def draw_borders(self):
pygame.draw.rect(self.screen, (72, 47, 125), self.leftframe)
pygame.draw.rect(self.screen, (72, 47, 125), self.rightframe)
pygame.draw.rect(self.screen, (72, 47, 125), self.framedown)
pygame.draw.rect(self.screen, (72, 47, 125), self.frameup)
def hear_events(self):
for e in pygame.event.get():
if e.type == pygame.KEYDOWN:
self.move_and_draw_square(e.key)
def move_and_draw_square(self, key):
if key == pygame.K_LEFT:
self.move_square(-1, 0)
if self.square.colliderect(self.leftframe):
self.move_square(1, 0)
elif key == pygame.K_RIGHT:
self.move_square(1, 0)
if self.square.colliderect(self.rightframe):
self.move_square(-1, 0)
elif key == pygame.K_UP:
self.move_square(0, -1)
if self.square.colliderect(self.frameup):
self.move_square(0, 1)
elif key == pygame.K_DOWN:
self.move_square(0, 1)
if self.square.colliderect(self.framedown):
self.move_square(0, -1)
elif key == pygame.K_ESCAPE:
pygame.quit()
def move_square(self, x, y):
self.square.move_ip(x * 10, y * 10)
def draw_square(self):
pygame.draw.rect(self.screen, (0, 204, 0), self.square)
def update_screen(self):
pygame.display.update()
pygame.init()
game = game()
while True:
game.play()
```