May-18-2021, 06:43 PM
(This post was last modified: May-18-2021, 06:57 PM by Yoriz.
Edit Reason: Added code tags
)
Good day, i was wondering could anybody help me with my code, so i am creating a pong game with a start menu. When start is clicked i want the pong game to start. Now i have multiple .py files that take classes and functions from one to another. I will post the part of the code where i was thinking the pong game code is supose to go. If needed i will post other parts of code. Also if i put the code where the pong game is running into a class so i can call it in another .py file, the pong game wont run on its on.
from menu import * from pong import Pong class Game(): def __init__(self): pygame.init() self.p= Pong() self.running, self.playing = True, False self.UP_KEY, self.DOWN_KEY, self.START_KEY, self.BACK_KEY = False, False, False, False self.DISPLAY_W, self.DISPLAY_H = 1280, 960 self.display = pygame.Surface((self.DISPLAY_W,self.DISPLAY_H)) self.window = pygame.display.set_mode(((self.DISPLAY_W,self.DISPLAY_H))) self.font_name = '8-BIT WONDER.TTF' self.BLACK, self.WHITE = (0, 0, 0), (255, 255, 255) self.main_menu = MainMenu(self) self.options = OptionsMenu(self) self.credits = CreditsMenu(self) self.curr_menu = self.main_menu def game_loop(self): while self.playing: self.check_events() if self.START_KEY: self.playing= False Pong() #self.display.fill(self.BLACK) #self.draw_text('Thanks for Playing', 20, self.DISPLAY_W/2, self.DISPLAY_H/2) ##pygame.display.update() self.reset_keys() def check_events(self): for event in pygame.event.get(): if event.type == pygame.QUIT: self.running, self.playing = False, False self.curr_menu.run_display = False if event.type == pygame.KEYDOWN: if event.key == pygame.K_RETURN: self.START_KEY = True if event.key == pygame.K_BACKSPACE: self.BACK_KEY = True if event.key == pygame.K_DOWN: self.DOWN_KEY = True if event.key == pygame.K_UP: self.UP_KEY = True def reset_keys(self): self.UP_KEY, self.DOWN_KEY, self.START_KEY, self.BACK_KEY = False, False, False, False def draw_text(self, text, size, x, y ): font = pygame.font.Font(self.font_name,size) text_surface = font.render(text, True, self.WHITE) text_rect = text_surface.get_rect() text_rect.center = (x,y) self.display.blit(text_surface,text_rect) import pygame, random, sys class Pong(): def __init__(self): pygame.init() def ball_animation(self): self.ball.x += self.ball_speed_x self.ball.y += self.ball_speed_y if self.ball.top <= 0 or self.ball.bottom >= self.screen_height: self.ball_speed_y *= -1 # Player Score if self.ball.left <= 0: self.score_time = pygame.time.get_ticks() self.player_score += 1 # Opponent Score if self.ball.right >= self.screen_width: self.score_time = pygame.time.get_ticks() self.opponent_score += 1 if self.ball.colliderect(self.player) and self.ball_speed_x > 0: if abs(self.ball.right - self.player.left) < 10: self.ball_speed_x *= -1 elif abs(self.ball.bottom - self.player.top) < 10 and self.ball_speed_y > 0: self.ball_speed_y *= -1 elif abs(self.ball.top - self.player.bottom) < 10 and self.ball_speed_y < 0: self.ball_speed_y *= -1 if self.ball.colliderect(self.opponent) and self.ball_speed_x < 0: if abs(self.ball.left - self.opponent.right) < 10: self.ball_speed_x *= -1 elif abs(self.ball.bottom - self.opponent.top) < 10 and self.ball_speed_y > 0: self.ball_speed_y *= -1 elif abs(self.ball.top - self.opponent.bottom) < 10 and self.ball_speed_y < 0: self.ball_speed_y *= -1 def player_animation(self): self.player.y += self.player_speed if self.player.top <= 0: self.player.top = 0 if self.player.bottom >= self.screen_height: self.player.bottom = self.screen_height def opponent_ai(self): if self.opponent.top < self.ball.y: self.opponent.y += self.opponent_speed if self.opponent.bottom > self.ball.y: self.opponent.y -= self.opponent_speed if self.opponent.top <= 0: self.opponent.top = 0 if self.opponent.bottom >= self.screen_height: self.opponent.bottom = self.screen_height def ball_start(self): self.ball.center = (self.screen_width / 2, self.screen_height / 2) self.current_time = pygame.time.get_ticks() if self.current_time - self.score_time < 700: self.number_three = self.basic_font.render("3", False, self.light_grey) self.screen.blit(self.number_three, (self.screen_width / 2 - 10, self.screen_height / 2 + 20)) if 700 < self.current_time - self.score_time < 1400: self.number_two = self.basic_font.render("2", False, self.light_grey) self.screen.blit(self.number_two, (self.screen_width / 2 - 10, self.screen_height / 2 + 20)) if 1400 < self.current_time - self.score_time < 2100: self.number_one = self.basic_font.render("1", False, self.light_grey) self.screen.blit(self.number_one, (self.screen_width / 2 - 10, self.screen_height / 2 + 20)) if self.current_time - self.score_time < 2100: self.ball_speed_y, self.ball_speed_x = 0, 0 else: self.ball_speed_x = 7 * random.choice((1, -1)) self.ball_speed_y = 7 * random.choice((1, -1)) self.score_time = None # General setup pygame.mixer.pre_init(44100, -16, 1, 1024) pygame.init() self.clock = pygame.time.Clock() # Main Window self.screen_width = 1280 self.screen_height = 960 self.screen = pygame.display.set_mode((self.screen_width, self.screen_height)) pygame.display.set_caption('Pong') # Colors self.light_grey = (200, 200, 200) self.bg_color = pygame.Color('grey12') # Game Rectangles self.ball = pygame.Rect(self.screen_width / 2 - 15, self.screen_height / 2 - 15, 30, 30) self.player = pygame.Rect(self.screen_width - 20, self.screen_height / 2 - 70, 10, 140) self.opponent = pygame.Rect(10, self.screen_height / 2 - 70, 10, 140) # Game Variables self.ball_speed_x = 7 * random.choice((1, -1)) self.ball_speed_y = 7 * random.choice((1, -1)) self.player_speed = 0 self.opponent_speed = 7 self.ball_moving = False self.score_time = True # Score Text self.player_score = 0 self.opponent_score = 0 self.basic_font = pygame.font.Font('freesansbold.ttf', 32) while True: 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: self.player_speed -= 6 if event.key == pygame.K_DOWN: self.player_speed += 6 if event.type == pygame.KEYUP: if event.key == pygame.K_UP: self.player_speed += 6 if event.key == pygame.K_DOWN: self.player_speed -= 6 #Game logic self.ball_animation() self.player_animation() self.opponent_ai() # Visuals self.screen.fill(self.bg_color) pygame.draw.rect(self.screen, self.light_grey, self.player) pygame.draw.rect(self.screen, self.light_grey, self.opponent) pygame.draw.ellipse(self.screen, self.light_grey, self.ball) pygame.draw.aaline(self.screen, self.light_grey, (self.screen_width / 2, 0),(self.screen_width / 2, self.screen_height)) if self.score_time: self.ball_start() self.player_text = self.basic_font.render(f'{self.player_score}', False, self.light_grey) self.screen.blit(self.player_text, (660, 470)) self.opponent_text = self.basic_font.render(f'{self.opponent_score}', False, self.light_grey) self.screen.blit(self.opponent_text, (600, 470)) pygame.display.flip() self.clock.tick(60)Any amount of help is helpfull, thank you.