Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

When running code window opens and closes immediately [duplicate]

$
0
0

When running code the window opens and then closes, I have looked through and as people I know in real life what the issue may be. I am new to programming so I honestly have no idea of what it may be.No error shows up when running (using Visual studio code)I don't believe for the code its self to be an issue. However again am not entirely sure the issue(pygame 2.1.3 (SDL 2.0.22, Python 3.11.6)Hello from the pygame community. https://www.pygame.org/contribute.htmlPS C:\Users\kabir\Documents\python work\GAMES\ball game> )again sorry if the question itself doesn't make sense ill try to elaborate if you don't understand

The code itself (as a whole)

import pygameimport syspygame.init()HEIGHT = 640WIDTH = 480fps = 60timer = pygame.time.Clockscreen = pygame.display.set_mode((640, 480))pygame.display.set_caption('Menu System')def draw_menu(screen, menu):    font = pygame.font.Font(None, 36)    screen.fill((255, 255, 255))    for i, option in enumerate(menu):        text = font.render(option, True, (0, 0, 0))        screen.blit(text, (200, 200 + i * 50))# game variableswall_thickness = 10gravity = 0.5bounce_stop = 0.3# track positions of mouse to get movement vectormouse_trajectory = []class Ball:    def __init__(self, x_pos, y_pos, radius, color, mass, retention, y_speed, x_speed, id, friction):        self.x_pos = x_pos        self.y_pos = y_pos        self.radius = radius        self.color = color        self.mass = mass        self.retention = retention        self.y_speed = y_speed        self.x_speed = x_speed        self.id = id        self.circle = ''        self.selected = False        self.friction = friction    def draw(self):        self.circle = pygame.draw.circle(screen, self.color, (self.x_pos, self.y_pos), self.radius)    def check_gravity(self):        if not self.selected:            if self.y_pos < HEIGHT - self.radius - (wall_thickness / 2):                self.y_speed += gravity            else:                if self.y_speed > bounce_stop:                    self.y_speed = self.y_speed * -1 * self.retention                else:                    if abs(self.y_speed) <= bounce_stop:                        self.y_speed = 0            if (self.x_pos < self.radius + (wall_thickness/2) and self.x_speed < 0) or \                    (self.x_pos > WIDTH - self.radius - (wall_thickness/2) and self.x_speed > 0):                self.x_speed *= -1 * self.retention                if abs(self.x_speed) < bounce_stop:                    self.x_speed = 0            if self.y_speed == 0 and self.x_speed != 0:                if self.x_speed > 0:                    self.x_speed -= self.friction                elif self.x_speed < 0:                    self.x_speed += self.friction        else:            self.x_speed = x_push            self.y_speed = y_push        return self.y_speed    def update_pos(self, mouse):        if not self.selected:            self.y_pos += self.y_speed            self.x_pos += self.x_speed        else:            self.x_pos = mouse[0]            self.y_pos = mouse[1]    def check_select(self, pos):        self.selected = False        if self.circle.collidepoint(pos):            self.selected = True        return self.selecteddef draw_walls():    left = pygame.draw.line(screen, 'white', (0, 0), (0, HEIGHT), wall_thickness)    right = pygame.draw.line(screen, 'white', (WIDTH, 0), (WIDTH, HEIGHT), wall_thickness)    top = pygame.draw.line(screen, 'white', (0, 0), (WIDTH, 0), wall_thickness)    bottom = pygame.draw.line(screen, 'white', (0, HEIGHT), (WIDTH, HEIGHT), wall_thickness)    wall_list = [left, right, top, bottom]    return wall_listdef calc_motion_vector():    x_speed = 0    y_speed = 0    if len(mouse_trajectory) > 10:        x_speed = (mouse_trajectory[-1][0] - mouse_trajectory[0][0]) / len(mouse_trajectory)        y_speed = (mouse_trajectory[-1][1] - mouse_trajectory[0][1]) / len(mouse_trajectory)    return x_speed, y_speedball1 = Ball(50, 50, 30, 'blue', 100, .75, 0, 0, 1, 0.02)balls = [ball1]def game_loop():    menu = ['Play', 'Quit']    while True:        draw_menu(screen, menu)        for event in pygame.event.get():            if event.type == pygame.QUIT:                pygame.quit()                sys.exit()            if event.type == pygame.KEYDOWN:                if event.key == pygame.K_UP:                    menu.insert(0, menu.pop())                elif event.key == pygame.K_DOWN:                    menu.append(menu.pop(0))                elif event.key == pygame.K_RETURN:                    if menu[0] == 'Play':                        # main game loop                        while run:                            timer.tick(fps)                            screen.fill('black')                            mouse_coords = pygame.mouse.get_pos()                            mouse_trajectory.append(mouse_coords)                            if len(mouse_trajectory) > 20:                             mouse_trajectory.pop(0)                            x_push, y_push = calc_motion_vector()                            ball1.y_speed = ball1.check_gravity()                            walls = draw_walls()                            ball1.draw()                            ball1.update_pos(mouse_coords)                            for event in pygame.event.get():                                if event.type == pygame.QUIT:                                    run = False                                if event.type == pygame.MOUSEBUTTONDOWN:                                    if event.button == 1:                                        if ball1.check_select(event.pos):                                            active_select = True                                if event.type == pygame.MOUSEBUTTONUP:                                    if event.button == 1:                                        active_select = False                                        for i in range(len(balls)):                                            balls[i].check_select((-1000, -1000))                                elif menu[0] == 'Quit':                                    pygame.quit()                                    sys.exit()                            pygame.display.flip()                            game_loop()

Viewing all articles
Browse latest Browse all 23131

Trending Articles