[Tutor] Problem handling keyboard commands
Cravan
savageapple850 at gmail.com
Wed Oct 9 05:06:46 EDT 2019
Hi guys,
I fixed the problem I mentioned beforehand, but ran into a new problem with the keyboard commands. In my code, I wanted the food_bar in my game to increase when i press a key, say f, in my game, and also deduct say $10 from my money_bar when i press f. The food bar shows the current amount of food I have, which is supposed to decrease every second. However, it appears that none of my keyboard commands in the event() are working. May I know what is the problem in my code?
def __init__(self):
pygame.init()
self.clock = pygame.time.Clock()
self.living = 1
self.screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption(TITLE)
self.time = pygame.time.get_ticks()
pygame.key.set_repeat(500, 100)
self.all_sprites = pygame.sprite.Group()
self.console = Console(self, 0)
self.player = Player(self, 390, 595)
self.work = Work(self, 450, 250)
self.food_station = Food_Station(self, 750, 200)
self.food = Food(self, 25, 20)
self.education = Education(self, 300, 10)
self.school = School(self, 100, 200)
self.family = Family(self, 600, 10)
self.money = Money(self, 800, 15)
initial_food = 100
self.food_bar = initial_food
initial_money = 0
self.money_bar = initial_money
initial_education = "Student"
self.education_level = initial_education
initial_family = 3
self.family_member = 3
def run(self):
self.playing = True
self.hunger()
while self.playing:
self.dt = self.clock.tick(FPS) / 1000
self.events()
self.draw()
self.update()
def hunger(self):
self.HUNGEREVENT = pygame.USEREVENT + 1
pygame.time.set_timer(self.HUNGEREVENT, 1000)
self.all_sprites.update()
pygame.display.flip()
def food_food(self, x, y, cool):
if cool < 0:
cool = 0
BAR_LENGTH = 100
BAR_HEIGHT = 10
fill = (cool / 100) * BAR_LENGTH
outline_rect = pygame.Rect(x, y, BAR_LENGTH, BAR_HEIGHT)
fill_rect = pygame.Rect(x, y, fill, BAR_HEIGHT)
pygame.draw.rect(screen, GREEN, fill_rect)
pygame.draw.rect(screen, WHITE, outline_rect, 2)
if cool == 0:
self.living = 0
self.quit()
def quit(self):
pygame.quit()
sys.exit()
def update(self):
self.all_sprites.update()
def draw(self):
self.screen.fill(BGCOLOR)
self.all_sprites.draw(self.screen)
font = pygame.font.SysFont('Arial', 15, True, False)
self.food_food(120, 50, self.food_bar)
text = font.render("Number of days:" , True, BLACK)
screen.blit(text, [0, 110])
font = pygame.font.SysFont('Arial', 30, True, False)
text = font.render("= " + str(self.education_level), True, BLACK)
screen.blit(text, [400, 40])
font = pygame.font.SysFont('Arial', 30, True, False)
text = font.render("= " + str(self.family_member), True, BLACK)
screen.blit(text, [700, 40])
font = pygame.font.SysFont('Arial', 30, True, False)
text = font.render("= $" + str(self.money_bar), True, BLACK)
screen.blit(text, [900, 40])
self.all_sprites.update()
pygame.display.flip()
def events(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.quit()
if event.type == self.HUNGEREVENT:
self.food_bar = self.food_bar - 10
self.all_sprites.update()
pygame.display.flip()
if event.type == pygame.K_f:
self.money_bar = self.money_bar - 10
self.food_bar = self.food_bar + 15
self.all_sprites.update()
pygame.display.flip()
if event.type == pygame.K_ESCAPE:
self.quit()
Thanks in advance,
Cravan
More information about the Tutor
mailing list