I'm new to coding and i am using visual studio code with the python addon. I'm trying to separate a the main menu and game but it thinks that self is a positional argument and I have looked at the other articles on this website but i cannot understand what I'm doing wrong. this is my Code:
import random
import pygame as pg
from pygame import mixer
from pygame.draw import rect
mixer.init()
pg.font.init()
left_pressed = False
right_pressed = False
up_pressed = False
down_pressed = False
with open('highscore.txt', 'r+') as HSF:
highscore = 0
highscore = HSF.read()
highscore = int(highscore)
width = 500
height = 500
score = 0
black = (000,000,000)
Titlefont = pg.font.Font('antiquity-print.ttf', 40)
font = pg.font.Font('antiquity-print.ttf', 30)
textX = 10
textY = 10
highscoreXvalue = 10
highscoreYvalue = 50
TitleX = 75
TitleY = 20
ButtonX = 55
ButtonY = 90
ButtonTextX = 190
ButtonTextY = 120
def showscore(x,y):
scorerender = font.render("Score:" + str(score),True, black)
screen.blit(scorerender, (x, y))
def highscoretext(x,y):
highscorerender = font.render ("Highscore:" + str(highscore), True, black)
screen.blit(highscorerender, (x, y))
def Title(x,y):
Titlerender = Titlefont.render("MUSHROOM", True, black)
screen.blit(Titlerender, (x, y))
def buttontext(x,y):
buttontextrender = Titlefont.render("Play", True, black)
screen.blit(buttontextrender, (x, y))
coinx = 350
coiny = 350
background =pg.image.load('image.png')
mixer.music.load('music.mp3')
mixer.music.play(-1)
character =(200,162,200)
x=250
y=250
enemyX=0
enemyY=0
pg.init()
screen = pg.display.set_mode((width, height))
pg.display.set_caption("Mushroom")
icon = pg.image.load('mush.png')
pg.display.set_icon(icon)
playerimg = pg.image.load('Player.png')
coin = pg.image.load('coin.png')
enemy = pg.image.load('slimey.png')
blue_button = pg.image.load("Blue_button.png")
def player():
screen.blit(playerimg,(x,y))
def enemydef():
screen.blit(enemy,(enemyX,enemyY))
def Coin():
screen.blit(coin,(coinx,coiny))
class GameState():
def __init__(self):
self.state = 'main_menu'
def main_menu(self):
for event in pg.event.get():
if event.type == pg.QUIT:
pg.quit()
screen.blit(background,(0,0))
Title(TitleX, TitleY)
if start_button.draw() == True:
self.state = 'main_game'
buttontext(ButtonTextX, ButtonTextY)
pg.display.update()
def main_game(self):
global highscore
global enemyX
global enemyY
global x
global y
global score
global coinx
global coiny
global down_pressed
global up_pressed
global left_pressed
global right_pressed
global event
if x >= enemyX - 40 and x <= enemyX + 40 and y >= enemyY - 40 and y <= enemyY +40:
if highscore <= score:
highscore = score
highscoresavetofile = str(highscore)
HSF.seek(0)
HSF.write(highscoresavetofile)
HSF.truncate()
HSF.close
self.state = 'main_game'
for event in pg.event.get():
if event.type == pg.KEYDOWN:
if event.key==pg.K_LEFT and left_pressed == False:
x-=50
left_pressed = True
if event.key==pg.K_RIGHT and right_pressed == False:
x+=50
right_pressed = True
if event.key==pg.K_UP and up_pressed == False:
y-=50
up_pressed = True
if event.key==pg.K_DOWN and down_pressed == False:
y+=50
down_pressed = True
if event.key==pg.K_a and left_pressed == False:
x-=50
left_pressed = True
if event.key==pg.K_d and right_pressed == False:
x+=50
right_pressed = True
if event.key==pg.K_w and up_pressed == False:
y-=50
up_pressed = True
if event.key==pg.K_s and down_pressed == False:
y+=50
down_pressed = True
if event.type == pg.KEYUP:
left_pressed = False
right_pressed = False
up_pressed = False
down_pressed = False
if enemyX >= x:
enemyX -= 0.425
if enemyX <= x:
enemyX += 0.425
if enemyY >= y:
enemyY -= 0.425
if enemyY <= y:
enemyY += 0.425
if x<=0:
x=0
if x>=width -50:
x=width-50
if y<=0:
y=0
if y>=height -50:
y=height-50
# if x is between 50 less and 50 more of coinx and y is between 50 more and 50 less of coin y then
if x >= coinx - 50 and x <= coinx + 50 and y >= coiny - 50 and y <= coiny +50:
score +=1
coinx = random.randint(50,450)
coiny = random.randint(50,450)
coin_sound = mixer.Sound('Coin.mp3')
coin_sound.play()
screen.blit(background,(0,0))
player()
enemydef()
Coin()
showscore(textX, textY)
highscoretext(highscoreXvalue, highscoreYvalue)
pg.display.update()
def state_manager(self):
if self.state == 'main_menu':
self.main_menu()
if self.state == 'main_game':
self.main_game()
class Button():
def __init__(self, x, y, image):
width = image.get_width
height = image.get_height()
self.image = image
self.rect = self.image.get_rect()
self.rect.topleft = (x, y)
self.clicked = False
def draw(self):
action = False
mousepos = pg.mouse.get_pos()
if self.rect.collidepoint(mousepos):
if pg.mouse.get_pressed()[0] == 1 and self.clicked == False:
self.clicked = True
action = True
if pg.mouse.get_pressed()[0] == 0:
self.clicked = False
screen.blit(self.image,(self.rect.x, self.rect.y))
return action
start_button = Button(ButtonX, ButtonY, blue_button)
Game_state = GameState()
running = True
while running:
GameState.state_manager()
and this is my error message:
Traceback (most recent call last):
File "c:\Users\noodl\Downloads\Python stuffz\Game.py", line 256, in <module>
GameState.state_manager()
TypeError: GameState.state_manager() missing 1 required positional argument: 'self'
PS C:\Users\noodl\Downloads\Python stuffz>
Any help would be greatly appreciated. Thanks.