From: Skullheadx <704277@pdsb.net> Date: Wed, 31 Aug 2022 04:13:29 +0000 (-0400) Subject: Basket Ball Created X-Git-Url: http://git.skullheadx.com/about.html?a=commitdiff_plain;h=42959f84b1aa7aa1d454ddbc266ee950170c327c;p=BasketBall.git Basket Ball Created --- diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c0499af --- /dev/null +++ b/.gitignore @@ -0,0 +1,17 @@ + +.idea/BasketBall.iml +.idea/BasketBall.iml +.idea/inspectionProfiles/profiles_settings.xml +.idea/inspectionProfiles/Project_Default.xml +.idea/misc.xml +.idea/modules.xml +.idea/vcs.xml +__pycache__/game.cpython-310.pyc +__pycache__/setup.cpython-39.pyc +__pycache__/setup.cpython-310.pyc +.idea/vcs.xml +.idea/modules.xml +.idea/misc.xml +.idea/inspectionProfiles/Project_Default.xml +.idea/inspectionProfiles/profiles_settings.xml +.idea/BasketBall.iml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/BasketBall.iml b/.idea/BasketBall.iml new file mode 100644 index 0000000..eb88b39 --- /dev/null +++ b/.idea/BasketBall.iml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..38750a3 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,44 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..b3d05c2 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..26d1198 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/game.py b/game.py new file mode 100644 index 0000000..9c15a87 --- /dev/null +++ b/game.py @@ -0,0 +1,266 @@ +import pygame.draw + +from setup import * + + +class Ball: + radius = 15 + gravity = 9.81 / 1000 + + def __init__(self, pos, vel, collide_list): + self.position = pygame.Vector2(pos) + self.velocity = pygame.Vector2(vel) + self.acceleration = pygame.Vector2(0, self.gravity) + + self.angle = 0 + + self.shot_taken = False + self.on_ground = False + + self.collide_list = collide_list + + def get_input(self): + if pygame.mouse.get_pressed(3)[0]: + self.shoot() + + def shoot(self): + if not self.shot_taken: + self.shot_taken = True + + self.velocity, self.angle = self.get_initial() + + def get_initial(self): + mouse_position = pygame.Vector2(pygame.mouse.get_pos()) + velocity = pygame.Vector2( + mouse_position.x - self.position.x, mouse_position.y - self.position.y).normalize() + + distance = math.sqrt(pow(mouse_position.x - self.position.x, 2) + pow(mouse_position.y - self.position.y, 2)) + + # print(distance) + + distance /= 45 + + distance = min(distance, 12) + + velocity.x *= distance + velocity.y *= distance + + # angle = math.atan((mouse_position.y - self.position.y) / (mouse_position.x - self.position.x)) # radians + angle = 0 + return velocity, angle + + def update(self, delta): + + self.get_input() + + if self.shot_taken: + self.position, self.velocity = self.get_movement(delta, self.collide_list) + + def get_movement(self, delta, collide_list): + position, velocity = self.position.copy(), self.velocity.copy() + + self.on_ground = False + position.y += velocity.y + for thing in collide_list: + if thing.get_rect().colliderect(self.get_rect(position.x, position.y)): + if velocity.y > 0: + position.y = thing.position.y - self.radius + self.on_ground = True + else: + position.y = thing.position.y + thing.height + self.radius + velocity.y *= -0.7 + + velocity.y += self.acceleration.y * delta + if self.on_ground and abs(velocity.y) < 0.6: + velocity.y = 0 + + position.x += velocity.x + for thing in collide_list: + if thing.get_rect().colliderect(self.get_rect(position.x, position.y)): + if velocity.x > 0: + position.x = thing.position.x - self.radius + else: + position.x = thing.position.x + thing.width + self.radius + velocity.x *= -0.7 + velocity.x += self.acceleration.x * delta + + if self.on_ground: + velocity.x *= 0.9 + if self.on_ground and abs(velocity.x) < 0.1: + velocity.x = 0 + + return position, velocity + + def get_rect(self, x=None, y=None): + if x is None: + x = self.position.x + if y is None: + y = self.position.y + return pygame.Rect(x - self.radius, y - self.radius, self.radius * 2, self.radius * 2) + + def draw(self, surf): + pygame.draw.circle(surf, Colour.ORANGE, self.position, self.radius) + + +class BallPath(Ball): + num_dots = 5 + # separation_distance = 0 gets too long to calc when bounce on floor + separation_counts = 6 + radius = Ball.radius * 0.85 + + def __init__(self, pos, vel, collide_list): + super().__init__(pos, vel, collide_list) + + self.path = [] + self.initial_position = pygame.Vector2(pos) + + def update(self, delta): + self.position = self.initial_position.copy() + self.velocity, self.angle = self.get_initial() + + self.path = [self.position] + time = (1000 / fps) # for a smoother result + dots = 0 + counter = 0 + while (-self.radius < self.position.x < SCREEN_WIDTH + self.radius and + self.position.y < SCREEN_HEIGHT + self.radius): + + self.position, self.velocity = self.get_movement(time, self.collide_list) + counter += 1 + if counter % self.separation_counts == 0: + # if len(self.path) == 0 or ( + # pow(self.path[-1].y - self.position.y, 2) + pow(self.path[-1].x - self.position.x, 2) > pow( + # self.separation_distance, 2)): + self.path.append(self.position) + dots += 1 + + if dots == self.num_dots: + break + # print(self.path, self.position, self.velocity) + + def draw(self, surf): + for pos in self.path: + pygame.draw.circle(surf, Colour.LIGHT_GRAY, pos, self.radius) + + +class Net: + width, height = Ball.radius * 2 * 2, Ball.radius * 3 + rim_width = 2 + pole_width = 30 + backboard_height = Ball.radius * 5 + + def __init__(self, pos): + self.position = pygame.Vector2(pos) + self.can_score = False + self.left_wall = Wall((self.position.x - self.rim_width / 2, self.position.y), self.rim_width, self.height, + Colour.WHITE) + self.right_wall = Wall((self.position.x + self.width - self.rim_width / 2, self.position.y), self.rim_width, + self.height, Colour.WHITE) + self.pole = Wall((self.right_wall.position.x + self.rim_width, self.position.y - self.backboard_height), + self.pole_width, SCREEN_HEIGHT - self.position.y, Colour.DARK_GRAY) + self.scored = False + + def update(self, ball): + if not self.can_score and ball.position.y < self.position.y: + self.can_score = True + if self.can_score and self.get_rect().colliderect(ball.get_rect()): + self.scored = True + return self.scored + + def get_rect(self): + return pygame.Rect(self.position.x, self.position.y, self.width, self.height) + + def draw(self, surf): + pygame.draw.rect(surf, Colour.WHITE, self.get_rect()) + + +class Wall: + + def __init__(self, pos, width, height, colour): + self.position = pygame.Vector2(pos) + self.width = width + self.height = height + self.colour = colour + + def get_rect(self): + return pygame.Rect(self.position, (self.width, self.height)) + + def draw(self, surf): + pygame.draw.rect(surf, self.colour, self.get_rect()) + + +class Game: + font = pygame.font.Font('gomarice_no_continue.ttf', 64) + small_font = pygame.font.Font('gomarice_no_continue.ttf', 32) + points = 0 + attempts = 0 + + def __init__(self): + self.net = Net((SCREEN_WIDTH * 4 / 5, SCREEN_HEIGHT / 3)) + + self.collision_list = [self.net.left_wall, self.net.right_wall, self.net.pole, + Wall((0, SCREEN_HEIGHT * 4 / 5), SCREEN_WIDTH, SCREEN_HEIGHT / 5, Colour.TAN)] + + self.ball = Ball((random.randint(SCREEN_WIDTH/10, SCREEN_WIDTH/1.5), SCREEN_HEIGHT * 2 / 3), (0, 0), self.collision_list) + self.ball_path = BallPath(self.ball.position, self.ball.velocity, self.collision_list) + + self.scored = False + self.points_added = False + self.attempt_added = False + self.game_over = False + self.timer = 2000 + + def update(self, delta): + self.ball.update(delta) + if not self.ball.shot_taken: + self.ball_path.update(delta) + + self.scored = self.net.update(self.ball) + + if not self.points_added and self.scored: + self.points += 1 + self.points_added = True + + if not self.attempt_added and pygame.mouse.get_pressed(3)[0]: + self.attempts += 1 + self.attempt_added = True + + if not self.game_over and self.ball.shot_taken and (self.ball.velocity == pygame.Vector2(0, 0) or not ( + -self.ball.radius < self.ball.position.x < SCREEN_WIDTH + self.ball.radius and + self.ball.position.y < SCREEN_HEIGHT + self.ball.radius)): + self.game_over = True + if self.game_over: + self.timer -= delta + if self.timer <= 0: + self.__init__() + + def draw(self, surf): + surf.fill(Colour.BLUE) + if not self.ball.shot_taken: + self.ball_path.draw(surf) + + for thing in self.collision_list: + thing.draw(surf) + # self.net.draw(surf) + + self.ball.draw(surf) + + if self.game_over: + if self.scored: + text = self.font.render("YOU SCORED", True, Colour.WHITE) + text_rect = text.get_rect(center=center) + text2 = self.font.render("YOU SCORED", True, Colour.DARK_GRAY) + text_rect2 = text.get_rect(center=center-pygame.Vector2(-10,-10)) + else: + text = self.font.render("YOU MISSED", True, Colour.WHITE) + text_rect = text.get_rect(center=center) + text2 = self.font.render("YOU SCORED", True, Colour.DARK_GRAY) + text_rect2 = text.get_rect(center=center-pygame.Vector2(-10,-10)) + surf.blit(text2, text_rect2) + surf.blit(text, text_rect) + text3 = self.small_font.render(f"POINTS: {self.points}", True, Colour.WHITE) + text_rect3 = text3.get_rect() + surf.blit(text3, text_rect3) + text4 = self.small_font.render(f"ATTEMPT #{self.attempts}", True, Colour.WHITE) + text_rect4 = text4.get_rect(top = text_rect3.height) + surf.blit(text4, text_rect4) diff --git a/gomarice_no_continue.ttf b/gomarice_no_continue.ttf new file mode 100644 index 0000000..c55e446 Binary files /dev/null and b/gomarice_no_continue.ttf differ diff --git a/main.py b/main.py new file mode 100644 index 0000000..6ea3dcb --- /dev/null +++ b/main.py @@ -0,0 +1,20 @@ +from setup import * +from game import Game + +screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT), pygame.RESIZABLE) +delta = 1000/fps +is_running = True + +scene = Game() + +while is_running: + if pygame.event.peek(pygame.QUIT): + is_running = False + + scene.update(delta) + scene.draw(screen) + + pygame.display.update() + delta = clock.tick(fps) + +pygame.quit() diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..410e284 --- /dev/null +++ b/setup.py @@ -0,0 +1,28 @@ +import pygame +import math +import random +pygame.init() + +SCREEN_WIDTH, SCREEN_HEIGHT = 1920/2, 1080/2 +dimensions = (SCREEN_WIDTH, SCREEN_HEIGHT) +center = pygame.Vector2(SCREEN_WIDTH/2, SCREEN_HEIGHT/2) + +pygame.display.set_caption("Basketball") +# icon = pygame.image.load("logo.ico") +# icon = pygame.transform.scale(icon, (32, 32)) +# pygame.display.set_icon(icon) + +clock = pygame.time.Clock() +fps = 120 + + +class Colour: + BLACK = (0, 0, 0) + WHITE = (255, 255, 255) + GRAY = (127, 127, 127) + DARK_GRAY = (40, 40, 40) + LIGHT_GRAY = (200,200,200) + RED = (204, 0, 0) + BLUE = (100, 149, 237) + ORANGE = (244, 187, 68) + TAN = (242, 210, 189)