I get this error: Traceback (most recent call last): File…
- User
I get this error: Traceback (most recent call last): File "c:\Users\L14\Documents\Projets\My_Game\main.py", line 20, in main() File "c:\Users\L14\Documents\Projets\My_Game\main.py", line 15, in main game_instance.run() File "c:\Users\L14\Documents\Projets\My_Game\game.py", line 86, in run self.draw_orb_light((self.screen), orb_center_pos, 30) TypeError: Game.draw_orb_light() takes 3 positional arguments but 4 were given ;when i run this code: import pygame from game import Game from settings import SCREEN_WIDTH, SCREEN_HEIGHT
def main(): # Initialize Pygame pygame.init()
# Set up the display screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption("Game Title") # Initialize and run the game game_instance = Game(screen) game_instance.run() pygame.quit()if name == 'main': main() ;This is the code inside game.py : import pygame import random import math from settings import *
class Game: def init(self, screen): self.screen = screen self.running = False self.cube_pos = [SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2] self.cube_size = 20 self.cube_speed = 2 self.light_orb = (random.randint(0, SCREEN_WIDTH), random.randint(0, SCREEN_HEIGHT)) self.player_score = 0 self.ai_score = 0 self.ai_pos = [random.randint(0, SCREEN_WIDTH), random.randint(0, SCREEN_HEIGHT)] self.ai_size = 20 self.ai_speed = 0.1 self.font = pygame.font.SysFont(None, 24)
def draw_scores(self, player_score, ai_score): score_text = self.font.render(f"Player: {player_score} | AI: {ai_score}", True, WHITE) self.screen.blit(score_text, (10, 10)) def move_towards(self, ai_pos, target_pos, speed): dx, dy = target_pos[0] - ai_pos[0], target_pos[1] - ai_pos[1] dist = max(math.sqrt(dx ** 2 + dy ** 2), 1) move_dx, move_dy = dx / dist * speed, dy / dist * speed return [ai_pos[0] + move_dx, ai_pos[1] + move_dy] def draw_player_light(self, position, max_radius): step = 255 // max_radius for i in range(max_radius): radius = max_radius - i alpha = 255 - (i * step) surface = pygame.Surface((2*radius, 2*radius), pygame.SRCALPHA) pygame.draw.circle(surface, (255, 255, 255, alpha), (radius, radius), radius) self.screen.blit(surface, (position[0] - radius, position[1] - radius)) def draw_orb_light(self, position, max_radius): step = 255 // max_radius for i in range(max_radius): radius = max_radius - i alpha = 255 - (i * step) color = (255 - 255*(i/max_radius), 255 - 255*(i/max_radius), 0, alpha) surface = pygame.Surface((2*radius, 2*radius), pygame.SRCALPHA) pygame.draw.circle(surface, color, (radius, radius), radius) self.screen.blit(surface, (position[0] - radius, position[1] - radius)) def run(self): self.running = True while self.running: for event in pygame.event.get(): if event.type == pygame.QUIT: self.running = False self.screen.fill(BLACK) keys = pygame.key.get_pressed() if keys[pygame.K_LEFT]: self.cube_pos[0] -= self.cube_speed if keys[pygame.K_RIGHT]: self.cube_pos[0] += self.cube_speed if keys[pygame.K_UP]: self.cube_pos[1] -= self.cube_speed if keys[pygame.K_DOWN]: self.cube_pos[1] += self.cube_speed cube_rect = pygame.Rect(self.cube_pos[0], self.cube_pos[1], self.cube_size, self.cube_size) self.cube_pos[0] = max(min(self.cube_pos[0], SCREEN_WIDTH - self.cube_size), 0) self.cube_pos[1] = max(min(self.cube_pos[1], SCREEN_HEIGHT - self.cube_size), 0) pygame.draw.rect(self.screen, WHITE, cube_rect) self.ai_pos = self.move_towards(self.ai_pos, self.light_orb, self.ai_speed) pygame.draw.rect(self.screen, (255, 0, 0), (*self.ai_pos, self.ai_size, self.ai_size)) self.ai_pos = ( max(min(self.ai_pos[0], SCREEN_WIDTH - self.ai_size), 0), max(min(self.ai_pos[1], SCREEN_HEIGHT - self.ai_size), 0) ) orb_center_pos = (self.light_orb[0], self.light_orb[1]) self.draw_orb_light((self.screen), orb_center_pos, 30) player_center_pos = (self.cube_pos[0] + self.cube_size // 2, self.cube_pos[1] + self.cube_size // 2) self.draw_player_light(self.screen, player_center_pos, 50) orb_rect = pygame.Rect(self.light_orb[0] - 5, self.light_orb[1] - 5, 10, 10) if cube_rect.colliderect(orb_rect): self.player_score += 1 self.light_orb = (random.randint(0, SCREEN_WIDTH), random.randint(0, SCREEN_HEIGHT)) elif orb_rect.collidepoint(self.ai_pos): self.ai_score += 1 self.light_orb = (random.randint(0, SCREEN_WIDTH), random.randint(0, SCREEN_HEIGHT)) self.draw_scores(self.screen, self.player_score, self.ai_score) pygame.display.flip() - Assistant
The error you're encountering is due to invoking the method
draw_orb_lightwith an incorrect number of arguments. Looking at the definition ofdraw_orb_lightin yourgame.py:def draw_orb_light(self, position, max_radius):It clearly takes 3 parameters (
self,position, andmax_radius), but the way you're calling it in therunmethod is incorrect:self.draw_orb_light((self.screen), orb_center_pos, 30)Here, you are mistakenly passing as an argument along with and , making it 4 arguments including .
- User
Now i have my game working fine, i need to add more features to it, first of all, i have a menu.py file which contains code for diplaying a menu when the game launches, here is the code inside of it, please modify the main.py so it gets displayed, code : import pygame from settings import * from game import Game
class Menu: def init(self, screen): self.screen = screen self.running = True self.state = "main" self.clock = pygame.time.Clock()
def draw_text(self, text, size, x, y, color=WHITE): font = pygame.font.Font(None, size) text_surface = font.render(text, True, color) text_rect = text_surface.get_rect(center=(x, y)) self.screen.blit(text_surface, text_rect) def main_menu(self): self.draw_text("My Game Title", 64, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 4, WHITE) self.draw_text("Developed by Me", 32, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 4 + 50, GRAY) def options_menu(self): self.draw_text("Options", 64, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, WHITE) def update(self): for event in pygame.event.get(): if event.type == pygame.QUIT: self.running = False pygame.quit() exit() self.screen.fill(BLACK) if self.state == "main": self.main_menu() elif self.state == "options": self.options_menu() pygame.display.flip() self.clock.tick(60) def play_game(self): game_instance = Game(self.screen) game_instance.run() self.state = "main" # Transition back to main menu after the game* - Assistant
To include the menu system in your game and ensure it displays when the game launches, you'll need to modify your
main.pyso that it initializes and utilizes the class from your file before jumping into the game. Here's how you could restructure your to incorporate the menu system effectively: - User
when i run the main.py file, the menu gets displayed, but no buttons are showing up, what to do ? here is the code of the new main.py: import pygame from game import Game from settings import SCREEN_WIDTH, SCREEN_HEIGHT from menu import Menu # Make sure to import the Menu class
def main(): # Initialize Pygame pygame.init()
# Set up the display screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption("Game Title") # Initialize the menu menu = Menu(screen) # Menu loop while menu.running: menu.update() # Initialize and run the game game_instance = Game(screen) game_instance.run() pygame.quit()if name == 'main': main()
- Assistant
- User
Now that i updated menu.py, i need you to update main.py, here is the current code of it: import pygame from game import Game from settings import SCREEN_WIDTH, SCREEN_HEIGHT from menu import Menu # Make sure to import the Menu class
def main(): # Initialize Pygame pygame.init()
# Set up the display screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption("Game Title") # Initialize the menu menu = Menu() # Menu loop while menu.running: menu.update() # Initialize and run the game game_instance = Game(screen) game_instance.run() pygame.quit()if name == 'main': main()
- Assistant
To adjust your
main.pyand make it flexible enough to either quit or start the game based on your updated , you need to introduce a mechanism that decides what action to take after the menu loop has ended — whether to start the game or to quit the application. Considering the updates you made to the , here's how you can modify your : - User
Traceback (most recent call last): File "c:\Users\L14\Documents\Projets\My_Game\main.py", line 27, in main() File "c:\Users\L14\Documents\Projets\My_Game\main.py", line 15, in main while menu.running: AttributeError: 'Menu' object has no attribute 'running'
- Assistant
The error message you're seeing indicates that when your
main.pyscript attempts to check themenu.runningattribute in the loop withwhile menu.running:, theMenuobject doesn't have therunningattribute defined, or it might not be properly initialized before this access. - User
now that i fixed that issue, the menu now shows up, but when i click on the start button nothing happens, and also, i want another options button to edit things like speed, number of AI enemies, and a lot more
- Assistant
- User
- Assistant
- User
Now that the menu and game work fine, i want to add some improvements, the options menu should have slider to adjust sensitivity, presets as levels of hardness which will control the speed of the AI, the number of them, and also a colission system so both the AI and player can make collision and push each other, this should have some basic physics logic, here is the code inside the game.py file so you can adjust it to add these improvements, if you can't generate the hole thing at once, split the code generation into multiple snippets
- Assistant
- User
now give me a full collision implementation
- Assistant
- User
- Assistant
- User
now add a firework effect whenever the player or the AI hits the light orb, the effect should be as small dots exploding from the center of the light orb
- Assistant
- User
Now i want to add a shader
- Assistant