[Tutor] Type error when doing Pygame
Cravan
savageapple850 at gmail.com
Sat Sep 28 03:48:38 EDT 2019
Hi guys,
Recently tried doing a pygame project (which is still half-finished). However I received a TypeError code that 5 positional arguments have been inserted when I have only inserted four in my function. Have no idea why its not working.
Here’s my code:
```
import pygame
import random
import math
import os
import time
import sys
from os import path
from newsettings import *
from spritesdata import *
class Game:
def __init__(self):
pygame.init()
self.clock = pygame.time.Clock()
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, 0, 10)
self.education = Education(self, 300, 10)
self.school = School(self, 100, 200)
self.family = Family(self, 600, 10)
self.money = Money(self, 850, 15)
self.food_bar = 100
def run(self):
self.playing = True
while self.playing:
self.dt = self.clock.tick(FPS) / 1000
self.hunger()
self.events()
self.update()
self.draw()
def hunger(self):
HUNGEREVENT = pygame.USEREVENT + 1
pygame.time.set_timer(HUNGEREVENT, 10000)
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(self, GREEN, fill_rect)
pygame.draw.rect(self, WHITE, outline_rect, 2)
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(screen, 5, 5, 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("=" , True, BLACK)
screen.blit(text, [120, 40])
font = pygame.font.SysFont('Arial', 30, True, False)
text = font.render("=" , True, BLACK)
screen.blit(text, [400, 40])
font = pygame.font.SysFont('Arial', 30, True, False)
text = font.render("=" , True, BLACK)
screen.blit(text, [700, 40])
font = pygame.font.SysFont('Arial', 30, True, False)
text = font.render("=" , True, BLACK)
screen.blit(text, [950, 40])
self.all_sprites.update()
pygame.display.flip()
def events(self):
# catch all events here
HUNGEREVENT = pygame.USEREVENT + 1
pygame.time.set_timer(HUNGEREVENT, 10000)
if pygame.event.get(HUNGEREVENT):
self.food_bar = self.food_bar - 10
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.quit()
if event.key == pygame.K_ESCAPE:
self.quit()
def end_screen(self):
screen.fill(BLUE)
font = pygame.font.SysFont('Arial', 30, True, False)
text = font.render("Life Simulator V2" , True, WHITE)
screen.blit(text, [400, 100])
font = pygame.font.SysFont('Arial', 30, True, False)
text = font.render("Controls: F for food, E for education, W for work", True, WHITE)
screen.blit(text, [175, 250])
font = pygame.font.SysFont('Arial', 30, True, False)
text = font.render("Hard mode: h Easy mode: e Normal mode: m", True, WHITE)
screen.blit(text, [175, 400])
font = pygame.font.SysFont('Arial', 30, True, False)
text = font.render("Start by pressing a key corresponding to that mode", True, WHITE)
screen.blit(text, [150, 600])
pygame.display.flip()
waiting = True
while waiting:
pygame.init()
clock = pygame.time.Clock()
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_h:
waiting = False
initial_money = 50
self.food_bar = 100
if event.key == pygame.K_m:
waiting = False
initial_money = 100
self.food_bar = 100
if event.key == pygame.K_e:
waiting = False
initial_money = 200
self.food_bar = 100
g = Game()
g.end_screen()
g.run()
```
And here’s the Error I’m receiving:
################################
Traceback (most recent call last):
File "final_project.py", line 135, in <module>
g.run()
File "final_project.py", line 37, in run
self.draw()
File "final_project.py", line 65, in draw
self.food_food(screen, 5, 5, self.food_bar)
TypeError: food_food() takes 4 positional arguments but 5 were given
Any help is appreciated. Thanks in advance!
Cravan
More information about the Tutor
mailing list