(Извините, если этот вопрос трудно понять, это мой первый вопрос. Я с удовольствием уточню все, что нужно уточнить. Заранее спасибо)
Я работаю над простым 2048 игра .И пока, похоже, все идет хорошо, проблема, которую я считаю, заключается в том, что мой код масштабирует мою функцию control
так быстро, что не обновляет список.Список начинается с: ['none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none', 'none']
Но когда вы нажимаете кнопку, список не обновляется.Я пытался настроить код функции control
, но я не смог добиться успеха.
import pygame
import sys
import random
pygame.init()
fps = pygame.time.Clock()
screen_size = screen_width, screen_height = 800, 800
display = pygame.display.set_mode(screen_size)
game_state = ['none', 'none', 'none', 'none',
'none', 'none', 'none', 'none',
'none', 'none', 'none', 'none',
'none', 'none', 'none', 'none']
class game_func:
def __init__(self):
self.input = False
self.count = 0
def controls(self):
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
self.input = True
def random(self):
var = random.randrange(0, 16)
if game_state[var] == 'none':
return var
else:
self.random()
def spawn(self):
if self.input:
pos = self.random()
if random.randrange(0, 2) == 1:
num = 'two'
else:
num = 'four'
game_state[pos] = num
self.input = False
def blit(self):
x, y = 0, 0
idx = self.count
pos = game_state[idx]
if 0 < idx < 4:
y = 0
x = idx
elif 3 < idx < 8:
y = 200
x = idx - 4
elif 7 < idx < 12:
y = 400
x = idx - 8
elif 11 < idx < 16:
y = 600
x = idx - 12
x *= 200
cord = x, y
print(idx)
print(cord)
print(game_state)
self.count += 1
if self.count == 16:
self.count = 0
def end():
pygame.quit()
sys.exit()
def main():
game = game_func()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
end()
game.blit()
game.controls()
game.spawn()
pygame.display.update()
fps.tick(30)
main()
end()