0

I'm starting a grid based game in Pygame, and I've gotten to the point where I have a grid on the screen, and I'm trying to let the user use the arrow keys to move the grid around. I tried to do it by adding a method to my Grid Object, but it doesn't seem to be doing anything. Help would be appreciated

#IMPORTING MODULES
import pygame
from pygame.locals import *
import random
import sys
white = (250,250,250)
black = (0,0,0)
#SETTING UP STUFF
pygame.init()
#ADDING FONTS AND TEXT
myfont = pygame.font.SysFont("monospace", 25)
title = myfont.render("X-COM: UFO DEFENSE", 1, (black))
#MORE SETTING UP
display_width = 800
display_height = 600
SIZE = 25
keys = [False,False,False,False]
scene = "start"
screen = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('X-COM UFO DEFENSE')
#SETTING UP BUTTON CLASS
class Button(object):
    def __init__(self, x, y, width, height,text,touching,clicked):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.text = text
        self.rect = pygame.Rect([int(self.x),int(self.y),int(self.width),int(self.height)])
        self.touching = touching
        self.clicked = clicked
    def draw(self):
        #DRAW FUNCTION(MAY NEED WORK ON THE TEXT FORMULA)
        text1 = myfont.render(str(self.text), 1, (black))
        pygame.draw.rect(screen,[255,255,255],self.rect,0)
        screen.blit(text1, (self.x + self.width/3,self.y + self.height/3))
    def click(self):
        #EASY CLICK DETECTION
        if self.rect.collidepoint(pygame.mouse.get_pos()):
            self.touching = True
        else:
            self.touching = False
#Creating GRID class
class GridSquare(object):
    def __init__(self, internal_row, internal_column, touching,clicked):
        self.internal_row = internal_row
        self.internal_column = internal_column
        self.touching = touching
        self.clicked = clicked
        self.right = 0
        self.up = 0
        self.rect = pygame.Rect([int(self.internal_row),int(self.internal_column),25,25])
    def draw(self):
        pygame.draw.rect(screen,[255,0,255],self.rect,0)
    def camera(self):
        self.internal_row = self.right+self.internal_row
        self.internal_column = self.up+self.internal_column
        if keys[0] == True:
            self.up += 1
        elif keys[1] == True:
            self.right -= 1
        elif keys[2] == True:
            self.up -= 1
        elif keys[3] == True:
             self.right += 1
#BUTTONS
grid = []
for i in range(100,200,26):
    for k in range(100,200,26):
        grid.append(GridSquare(i,k,False,False))    
titlescreen = Button(300,300,200,100,"Start",False,False)
#MAIN LOOP STARTS HERE
while True:
    if scene == "start":
      screen.fill(white)
      screen.blit(title, (300,50))
      titlescreen.draw()
      titlescreen.click()
      #EVENTS
    for event in pygame.event.get():
        if titlescreen.touching == True and event.type == pygame.MOUSEBUTTONDOWN:
            titlescreen.clicked = True
        else:
            titlescreen.clicked = False
        if event.type == KEYDOWN:
            if event.key == pygame.K_w:
                keys[0] = True
            elif event.key == pygame.K_a:
                keys[1] = True
            elif event.key == pygame.K_s:
                keys[2] = True
            elif event.key == pygame.K_d:
                keys[3] = True

    if titlescreen.clicked == True:
        scene = "game"
    if scene == "game":
        screen.fill(black)
        for grids in grid:
           grids.draw()
    for grids in grid:
        grids.camera()

    pygame.display.flip()
Phix
  • 9,364
  • 4
  • 35
  • 62
  • The variables you are changing in response to key presses have no effect on the visible appearance of your grid squares. Specifically, `self.rect` is set once in the `.__init__()` method, and never changed again. – jasonharper Jan 10 '18 at 20:35
  • @jasonharper What do you think the best way to fix this is? – Hyperdrive Enthusiast Jan 11 '18 at 02:13
  • Take a look at other questions about cameras. Here's an [answer](https://stackoverflow.com/a/45404355/6220679) which demonstrates how you can use a camera to adjust the blit positions of the sprites. – skrx Jan 11 '18 at 06:59
  • @skrx the problem is, I'm manually drawing rectangles, and not using Surface.blit() – Hyperdrive Enthusiast Jan 11 '18 at 14:08
  • You just need to add the camera offset to the coords of your rects when you draw them. – skrx Jan 11 '18 at 14:10

0 Answers0