0

works for a second then says x is referenced before assignment. Not suprised considing of how badly optizmed my code is. Problematic part is the grid() part, basically im just testing if it can display a red sqaure over the sqaures i loaded in. I tried doing it with a extra function but it stopped working? Moving the display.blit forward one made it work fine for like 2 seconds. heres my code

import pygame
import random
import time
import sys

pygame.init()
background_colour = (255,255,255)
(width, height) = (400, 500)

display = pygame.display.set_mode((width, height))
FPS = pygame.time.Clock()
pygame.display.set_caption("Game")
pygame.display.flip()

# Land
gras = pygame.image.load("assets/land/grass.png")
attack = pygame.image.load("assets/land/attac.png")
# UI
Tool = pygame.image.load("assets/UIs/Toolbar.png")
# rifle
riflesigil = pygame.image.load("assets/Rifle/helm1.png")
rifleforward = pygame.image.load("assets/Rifle/Rifle.png")
rifleleft = pygame.image.load("assets/Rifle/Rifle2.png")
rifledown = pygame.image.load("assets/Rifle/Rifle3.png")
rifleright = pygame.image.load("assets/Rifle/Rifle4.png")

class toolbar(pygame.sprite.Sprite):
  def __init__(self, image):
    self.image = image
    
class MAP(pygame.sprite.Sprite):
  def __init__(self, grass, red, display):
    self.count = 5
    self.red = pygame.transform.scale(red, (100, 100))
    self.gras = grass
    self.grass = pygame.transform.scale(self.gras, (100, 100))
  def gen(self, display):
    row = 0
    row2 = 0
    increase = 76
    for x in range(1, 6):
      for x in range(1, 6):
        display.blit(self.grass, (increase * row, row2 * increase))
        row += 1
        if row == 5:
          row = 0
      row2 += 1
  def grid(self, display, pos1, pos2):
    self.red = pygame.transform.scale(attack, (100, 100))
    if pos1 in range(0, 75):
      x = 0
    elif pos1 in range(76, 151):
      x = 76
    elif pos1 in range(152, 227):
      x = 152
    elif pos1 in range(228, 303):
      x = 228
    elif pos1 in range(304, 379):
      x = 304
    if pos2 in range(0, 75):
      y = 0
    elif pos2 in range(76, 151):
      y = 76
    elif pos2 in range(152, 227):
      y = 152
    elif pos2 in range(228, 303):
      y = 228
    elif pos2 in range(304, 379):
      y = 304
    display.blit(self.red, (x, y))
map = MAP(gras, attack, display)

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            exit()
    display.fill((255, 255, 255))
    (mouseX, mouseY) = pygame.mouse.get_pos()
    map.gen(display)
    map.grid(display, mouseX, mouseY)
    pygame.display.update()
# 152, 228, 304, 380
Stoat
  • 13
  • 3

0 Answers0