我在Pygame开始基于网格的游戏,我已经到了屏幕上有网格的地步,我试图让用户使用箭头键来移动网格。我试图通过向我的网格对象添加一个方法来做到这一点,但它似乎没有做任何事情。帮助将不胜感激
#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()