def load_data(self): self.font = pygame.font.Font(None, 34) self.paddleA = Paddle(WHITE, 20, 100) self.paddleA.rect.x = 10 self.paddleA.rect.y = 200 self.paddleB = Paddle(WHITE, 20, 100) self.paddleB.rect.x = 670 self.paddleB.rect.y = 200 self.ball = Ball(WHITE, 10, 10) self.ball.rect.x = 345 self.ball.rect.y = 195 # This will be a list that will contain all the sprites we intend to use in our game. self.all_sprites_list = pygame.sprite.Group() self.liste1 = pygame.sprite.Group() self.liste2 = pygame.sprite.Group() self.liste1.add(self.paddleA) self.liste2.add(self.paddleB) # Add the car to the list of objects self.all_sprites_list.add(self.paddleA) self.all_sprites_list.add(self.paddleB) self.all_sprites_list.add(self.ball)
def load_data(self): try: self.score = SCORE["player"] except: SCORE["player"] = 0 self.score = SCORE["player"] self.score_box = Box(self.screen, 10, (100, 10, 400, 50), RED, 4) self.font = pygame.font.Font(None, 34) self.paddleA = Paddle(WHITE, 20, 100) self.paddleA.rect.x = 10 self.paddleA.rect.y = 200 self.paddleB = Paddle(WHITE, 20, 100) self.paddleB.rect.x = 670 self.paddleB.rect.y = 200 self.ball = Ball(WHITE, 10, 10) self.ball.rect.x = 345 self.ball.rect.y = 195 # This will be a list that will contain all the sprites we intend to use in our game. self.all_sprites_list = pygame.sprite.Group() self.liste1 = pygame.sprite.Group() self.liste2 = pygame.sprite.Group() self.liste1.add(self.paddleA) self.liste2.add(self.paddleB) # Add the car to the list of objects self.all_sprites_list.add(self.paddleA) self.all_sprites_list.add(self.paddleB) self.all_sprites_list.add(self.ball)
def main(self): # Create bricks bricks = [] for i in range(60, 460, 45): x1 = i y1 = 400 y2 = 385 y3 = 370 bricks.append(Brick(x1, y1, "#99FF99")) bricks.append(Brick(x1, y2, "#FF6600")) bricks.append(Brick(x1, y3, "#6699FF")) for brick in bricks: brick.rect.draw(self.win) # Create paddle # Paddle size is 40 wide by 10 high paddle = Paddle() paddle.rect.draw(self.win) # Create circle (ball x, y and radius) self.ball = Ball(self.x, self.y, 8) self.ball.circle.draw(self.win) # The direction values determine amount of movement of ball direction = [BALL_SPEED, BALL_SPEED] while True: if self.ball.is_below_paddle(paddle): self.reset() self.ball.hits_wall(self.gamearea, direction) self.ball.hits_paddle(paddle, direction) for item, brick in enumerate(bricks): self.ball.hits_brick(brick, direction, bricks, item) # Move ball to new location self.ball.move(direction[0], direction[1]) # Check for mouse click on play area if self.win.checkMouse(): # pause for click in window break user_event = self.win.checkKey() if user_event == "Left": paddle.move(-10) elif user_event == "Right": paddle.move(10) # Wait for next loop time.sleep(.003) win.close()
from classes import Paddle, Iobject from random import randint import time # Most game rules are included inside this file ball = Paddle(10) screenX = 1080 screenY = 720 def walls(x, y): # Ball wall handling if x.y < (-1 + x.height): x.y += 17 x.yvelocity *= -1 if x.y > (screenY + (1 - x.height)): x.y -= 17 x.yvelocity *= -1 if x.x >= 1080 - x.height: x.xvelocity *= -1 y.score2 += 1 if x.x <= 0: x.x = 1080 x.y = randint(1, 719) y.score += 1 ball.yvelocity = 1
from classes import Iobject, Paddle paddle = Paddle(20) ball = Iobject() # The paddle moves down constantly. def bot1(x, y): y.y += y.velocity # If the ball is above the paddle then the paddle # goes up. If the ball is bellow the paddle then the # paddle moves down. def bot2(x, y): if x.y > y.y: y.y += y.velocity if x.y < y.y: y.y -= y.velocity # If the ball is bellow a certain height # and the ball is above a certain height then # the ball moves down to. Vise versa and if its # in the middle then it just follows the ball. def bot3(x, y): if x.y <= 180 and y.y >= 540: y.y += y.velocity elif x.y >= 540 and y.y <= 180:
class PvP: def __init__(self, screen): self.screen = screen self.clock = pygame.time.Clock() self.running = True self.scoreA = 0 self.scoreB = 0 self.load_data() def draw_text(self, text, font_name, size, color, x, y, align="nw"): font = pygame.font.Font(font_name, size) text_surface = font.render(text, True, color) text_rect = text_surface.get_rect() if align == "nw": text_rect.topleft = (x, y) if align == "ne": text_rect.topright = (x, y) if align == "sw": text_rect.bottomleft = (x, y) if align == "se": text_rect.bottomright = (x, y) if align == "n": text_rect.midtop = (x, y) if align == "s": text_rect.midbottom = (x, y) if align == "e": text_rect.midright = (x, y) if align == "w": text_rect.midleft = (x, y) if align == "center": text_rect.center = (x, y) self.screen.blit(text_surface, text_rect) def load_data(self): self.font = pygame.font.Font(None, 34) self.paddleA = Paddle(WHITE, 20, 100) self.paddleA.rect.x = 10 self.paddleA.rect.y = 200 self.paddleB = Paddle(WHITE, 20, 100) self.paddleB.rect.x = 670 self.paddleB.rect.y = 200 self.ball = Ball(WHITE, 10, 10) self.ball.rect.x = 345 self.ball.rect.y = 195 # This will be a list that will contain all the sprites we intend to use in our game. self.all_sprites_list = pygame.sprite.Group() self.liste1 = pygame.sprite.Group() self.liste2 = pygame.sprite.Group() self.liste1.add(self.paddleA) self.liste2.add(self.paddleB) # Add the car to the list of objects self.all_sprites_list.add(self.paddleA) self.all_sprites_list.add(self.paddleB) self.all_sprites_list.add(self.ball) def events(self): for event in pygame.event.get(): if event.type == pygame.QUIT: self.running = False if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: self.running = False keys = pygame.key.get_pressed() if keys[pygame.K_w]: self.paddleA.moveUp(5) if keys[pygame.K_s]: self.paddleA.moveDown(5) if keys[pygame.K_UP]: self.paddleB.moveUp(5) if keys[pygame.K_DOWN]: self.paddleB.moveDown(5) def update(self): self.all_sprites_list.update() if self.ball.rect.x >= 680: self.scoreA += 1 self.ball.velocity[0] = -self.ball.velocity[0] if self.ball.rect.x <= 10: self.scoreB += 1 self.ball.velocity[0] = -self.ball.velocity[0] if self.ball.rect.y > 490: self.ball.velocity[1] = -self.ball.velocity[1] if self.ball.rect.y < 0: self.ball.velocity[1] = -self.ball.velocity[1] # Detect collisions between the ball and the paddles if pygame.sprite.spritecollide(self.ball, self.liste1, False) and self.ball.velocity[0] < 0: print("LEFT --", self.ball.velocity) self.ball.bounce() if pygame.sprite.spritecollide(self.ball, self.liste2, False) and self.ball.velocity[0] > 0: print("RIGHT --", self.ball.velocity) self.ball.bounce() def draw(self): self.screen.fill(DARKBLUE) pygame.draw.line(self.screen, WHITE, [349, 0], [349, 500], 5) self.all_sprites_list.draw(self.screen) self.draw_text(f"{self.scoreA}", None, 50, WHITE, 250, 10) self.draw_text(f"{self.scoreB}", None, 50, WHITE, 420, 10) pygame.display.update() def run(self): while self.running: self.dt = self.clock.tick(FPS) / 1000.0 self.events() self.update() self.draw()
class PvE: def __init__(self, screen): self.screen = screen self.clock = pygame.time.Clock() self.running = True self.countdown = True self.end = False self.timer_color = WHITE self.count = 4 self.time = 5 self.scoreA = 0 self.scoreB = 0 self.load_data() def draw_text(self, text, font_name, size, color, x, y, align="nw"): font = pygame.font.Font(font_name, size) text_surface = font.render(text, True, color) text_rect = text_surface.get_rect() if align == "nw": text_rect.topleft = (x, y) if align == "ne": text_rect.topright = (x, y) if align == "sw": text_rect.bottomleft = (x, y) if align == "se": text_rect.bottomright = (x, y) if align == "n": text_rect.midtop = (x, y) if align == "s": text_rect.midbottom = (x, y) if align == "e": text_rect.midright = (x, y) if align == "w": text_rect.midleft = (x, y) if align == "center": text_rect.center = (x, y) self.screen.blit(text_surface, text_rect) def load_data(self): try: self.score = SCORE["player"] except: SCORE["player"] = 0 self.score = SCORE["player"] self.score_box = Box(self.screen, 10, (100, 10, 400, 50), RED, 4) self.font = pygame.font.Font(None, 34) self.paddleA = Paddle(WHITE, 20, 100) self.paddleA.rect.x = 10 self.paddleA.rect.y = 200 self.paddleB = Paddle(WHITE, 20, 100) self.paddleB.rect.x = 670 self.paddleB.rect.y = 200 self.ball = Ball(WHITE, 10, 10) self.ball.rect.x = 345 self.ball.rect.y = 195 # This will be a list that will contain all the sprites we intend to use in our game. self.all_sprites_list = pygame.sprite.Group() self.liste1 = pygame.sprite.Group() self.liste2 = pygame.sprite.Group() self.liste1.add(self.paddleA) self.liste2.add(self.paddleB) # Add the car to the list of objects self.all_sprites_list.add(self.paddleA) self.all_sprites_list.add(self.paddleB) self.all_sprites_list.add(self.ball) def events(self): for event in pygame.event.get(): if event.type == pygame.QUIT: self.running = False if event.type == pygame.KEYDOWN: if event.key == pygame.K_ESCAPE: self.running = False if not self.end: keys = pygame.key.get_pressed() if keys[pygame.K_w]: self.paddleA.moveUp(5) if keys[pygame.K_s]: self.paddleA.moveDown(5) else: SCORE["player"] = self.scoreA, self.scoreB def update(self): self.all_sprites_list.update() if self.ball.rect.x >= 680: self.scoreA += 1 self.ball.velocity[0] = -self.ball.velocity[0] if self.ball.rect.x <= 10: self.scoreB += 1 self.ball.velocity[0] = -self.ball.velocity[0] if self.ball.rect.y > 490: self.ball.velocity[1] = -self.ball.velocity[1] if self.ball.rect.y < 0: self.ball.velocity[1] = -self.ball.velocity[1] # Here Comes the Bot if self.paddleB.rect.y >= self.ball.rect.y: self.paddleB.moveUp(5) elif self.paddleB.rect.y <= self.ball.rect.y: self.paddleB.moveDown(5) # Detect collisions between the ball and the paddles if pygame.sprite.spritecollide(self.ball, self.liste1, False) and self.ball.velocity[0] < 0: print("LEFT --", self.ball.velocity) self.ball.bounce() if pygame.sprite.spritecollide(self.ball, self.liste2, False) and self.ball.velocity[0] > 0: print("RIGHT --", self.ball.velocity) self.ball.bounce() def draw(self): self.screen.fill(DARKBLUE) if not self.countdown: self.draw_text( f"{int((self.time-self.time_remaining)/60)} : {int(self.time-self.time_remaining)%60}", None, 70, self.timer_color, 300, 20) if not self.end: pygame.draw.line(self.screen, WHITE, [349, 80], [349, 500], 5) if not self.end: self.all_sprites_list.draw(self.screen) if self.countdown: self.draw_text(f"{int(self.count-self.seconds)} ", None, 100, WHITE, 330, 100) self.draw_text(f"{self.scoreA}", None, 50, WHITE, 220, 10) self.draw_text(f"{self.scoreB}", None, 50, WHITE, 450, 10) if self.end: self.score_box.draw() pygame.display.update() def run(self): self.start_ticks = pygame.time.get_ticks() while self.running: self.dt = self.clock.tick(FPS) / 1000.0 self.events() if not self.countdown and not self.end: self.update() else: self.seconds = (pygame.time.get_ticks() - self.start_ticks) / 1000 if self.seconds > self.count: self.countdown = False if not self.end: self.time_remaining = (pygame.time.get_ticks() - self.start_ticks) / 1000 if self.time - self.time_remaining < 0: self.end = True if (self.time - self.time_remaining - 10) <= 0: self.timer_color = RED self.draw()
start_time = time.time() screenX = 1080 screenY = 720 win = pygame.display.set_mode((screenX,screenY)) pygame.display.set_caption("The effect of bot algorithm on score in pong.") myFont = pygame.font.SysFont('Comic Sans MS', 30) myFont2 = pygame.font.SysFont('Comic Sans MS', 32) clock = pygame.time.Clock() # Importing images bg = pygame.image.load('static/img/bg.png') paddleDis = pygame.image.load('static/img/p.png') ballDis = pygame.image.load('static/img/ball.png') # Initializing paddle and ball paddle = Paddle(30) ball = Iobject() def redrawGameWindow(): # Redraw loop win.blit(bg, (0,0)) paddle.draw(win) ball.draw(win) # Drawing info boxes manually win.blit(infoBox, (850,20)) win.blit(infoBox2, (850,45)) win.blit(infoBox7, (850,70)) win.blit(infoBox8, (850,95)) win.blit(infoBox3, (850,120)) win.blit(infoBox4, (850,145)) win.blit(infoBox5, (850,170)) win.blit(infoBox6, (850,195))