You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
257 lines
7.4 KiB
Python
257 lines
7.4 KiB
Python
|
|
|
|
# Example file showing a basic pygame "game loop"
|
|
import pygame
|
|
import math
|
|
|
|
# pygame setup
|
|
pygame.init()
|
|
pygame.font.init()
|
|
screen = pygame.display.set_mode((1280, 720))
|
|
clock = pygame.time.Clock()
|
|
running = True
|
|
|
|
|
|
class Ball ( object ) :
|
|
|
|
def __init__ ( self, x, y ) :
|
|
self.x = x
|
|
self.y = y
|
|
self.width = 20
|
|
self.height = 20
|
|
self.x_vel = 200
|
|
self.y_vel = 200
|
|
|
|
def draw ( self ) :
|
|
pygame.draw.rect(screen, pygame.Color(0, 255, 0, 255), pygame.Rect(self.x, self.y, self.width, self.height))
|
|
|
|
def update ( self, dt, width, height, players ) :
|
|
if self.x > width :
|
|
self.x_vel *= -1
|
|
players[0].points += 1
|
|
if self.x < 0 :
|
|
self.x_vel *= -1
|
|
if self.y > height or self.y < 0:
|
|
self.y_vel *= -1
|
|
|
|
for player in players :
|
|
if self.y > player.y and self.y < player.y + player.height :
|
|
if self.x > player.x and self.x < player.x + player.width :
|
|
self.x_vel *= -1
|
|
|
|
self.x += self.x_vel * dt
|
|
self.y += self.y_vel * dt
|
|
|
|
def draw_points ( self, rects, length ) :
|
|
num_points = 360
|
|
size = length
|
|
vertices = []
|
|
|
|
for i in range(num_points) :
|
|
angle = 2 * math.pi * i / num_points
|
|
x = (self.x + self.width * math.cos(angle)) + self.width / 2
|
|
y = (self.y + self.height * math.sin(angle)) + self.height / 2
|
|
end_x = (self.x + size * math.cos(angle)) + self.width / 2
|
|
end_y = (self.y + size * math.sin(angle)) + self.height / 2
|
|
|
|
current_end_x = end_x
|
|
current_end_y = end_y
|
|
|
|
for i2 in range(len(rects)) :
|
|
(ex, ey) = line_rect_intersection(x, y, end_x, end_y, rects[i2], size)
|
|
|
|
if find_line_length(x, y, ex, ey) < find_line_length(x, y, current_end_x, current_end_y):
|
|
current_end_x = ex
|
|
current_end_y = ey
|
|
|
|
vertices.append([current_end_x, current_end_y])
|
|
|
|
#pygame.draw.line(screen, pygame.Color(255, 255, 255, 255), ( x, y ), ( current_end_x, current_end_y ))
|
|
|
|
surface2 = pygame.Surface((screen.get_width(),screen.get_height()))
|
|
surface2.set_colorkey((0,0,0))
|
|
surface2.set_alpha(80)
|
|
pygame.draw.polygon(surface2, pygame.Color(0, 0, 255, a = 255), vertices)
|
|
|
|
|
|
|
|
|
|
return pygame.mask.from_surface(surface2), surface2
|
|
|
|
|
|
def find_line_length(start_x, start_y, end_x, end_y):
|
|
dx = end_x - start_x
|
|
dy = end_y - start_y
|
|
|
|
distance_squared = dx * dx + dy * dy
|
|
|
|
return math.sqrt(distance_squared)
|
|
|
|
|
|
def line_rect_intersection(start_x, start_y, end_x, end_y, rect, max_size):
|
|
# end
|
|
|
|
t_min = 0.0
|
|
t_max = 1.0
|
|
|
|
dx = end_x - start_x
|
|
dy = end_y - start_y
|
|
|
|
p1 = [-dx, dx, -dy, dy]
|
|
p2 = [start_x - rect.x, rect.x + rect.width - start_x, start_y - rect.y, rect.y + rect.height - start_y]
|
|
|
|
for i in range(4):
|
|
if p1[i] == 0.0:
|
|
if p2[i] < 0.0:
|
|
return (end_x, end_y) # Line is outside the rectangle, return the original end point
|
|
else:
|
|
t = p2[i] / p1[i]
|
|
if p1[i] < 0.0:
|
|
t_min = max(t_min, t)
|
|
else:
|
|
t_max = min(t_max, t)
|
|
|
|
if t_min > t_max:
|
|
return (end_x, end_y) # Line is outside the rectangle, return the original end point
|
|
|
|
collision_x = start_x + t_min * dx
|
|
collision_y = start_y + t_min * dy
|
|
return (collision_x, collision_y)
|
|
|
|
|
|
class Player ( object ) :
|
|
|
|
def __init__ ( self, x ) :
|
|
self.x = x
|
|
self.y = 10
|
|
self.height = 100
|
|
self.width = 20
|
|
self.up = False
|
|
self.down = False
|
|
self.movement_speed = 400
|
|
self.points = 0
|
|
|
|
def draw ( self ) :
|
|
pygame.draw.rect(screen, pygame.Color(255, 0, 0, 255), pygame.Rect(self.x, self.y, self.width, self.height))
|
|
|
|
def update ( self, dt ) :
|
|
if self.up :
|
|
self.y -= self.movement_speed * dt
|
|
if self.down :
|
|
self.y += self.movement_speed * dt
|
|
|
|
|
|
|
|
|
|
|
|
getTicksLastFrame = 0
|
|
player = Player(10)
|
|
player2 = Player(screen.get_width() - 30)
|
|
ball = Ball(screen.get_width() / 2, screen.get_height() / 2)
|
|
|
|
while running:
|
|
|
|
t = pygame.time.get_ticks()
|
|
# deltaTime in seconds.
|
|
deltaTime = (t - getTicksLastFrame) / 1000.0
|
|
getTicksLastFrame = t
|
|
|
|
|
|
|
|
|
|
# poll for events
|
|
# pygame.QUIT event means the user clicked X to close your window
|
|
for event in pygame.event.get():
|
|
if event.type == pygame.QUIT:
|
|
running = False
|
|
if event.type == pygame.KEYDOWN:
|
|
if event.dict.get("unicode") == "w" :
|
|
player.up = True
|
|
if event.dict.get("unicode") == "s" :
|
|
player.down = True
|
|
# Up
|
|
if event.dict.get("key") == 1073741906:
|
|
player2.up = True
|
|
if event.dict.get("key") == 1073741905:
|
|
player2.down = True
|
|
if event.type == pygame.KEYUP :
|
|
if event.dict.get("unicode") == "w" :
|
|
player.up = False
|
|
if event.dict.get("unicode") == "s" :
|
|
player.down = False
|
|
# Up
|
|
if event.dict.get("key") == 1073741906:
|
|
player2.up = False
|
|
if event.dict.get("key") == 1073741905:
|
|
player2.down = False
|
|
|
|
|
|
# fill the screen with a color to wipe away anything from last frame
|
|
screen.fill("black")
|
|
|
|
# RENDER YOUR GAME HERE
|
|
|
|
|
|
|
|
player.update(dt=deltaTime)
|
|
player2.update(dt=deltaTime)
|
|
|
|
|
|
ball.update(dt=deltaTime, width=screen.get_width(), height=screen.get_height(), players=[player, player2])
|
|
ball.draw()
|
|
|
|
|
|
|
|
####
|
|
player.draw()
|
|
player2.draw()
|
|
|
|
|
|
|
|
mask, surface5 = ball.draw_points(rects=[pygame.Rect(200, 200, 20, 20)], length=50)
|
|
mask, surface6 = ball.draw_points(rects=[pygame.Rect(200, 200, 20, 20)], length=100)
|
|
mask, surface7 = ball.draw_points(rects=[pygame.Rect(200, 200, 20, 20)], length=150)
|
|
mask, surface8 = ball.draw_points(rects=[pygame.Rect(200, 200, 20, 20)], length=200)
|
|
|
|
|
|
|
|
|
|
|
|
filter = pygame.surface.Surface((screen.get_width(), screen.get_height()))
|
|
filter.fill(pygame.color.Color(200, 200,200))
|
|
filter.blit(surface5, (0, 0))
|
|
filter.blit(surface6, (0, 0))
|
|
filter.blit(surface7, (0, 0))
|
|
filter.blit(surface8, (0, 0))
|
|
screen.blit(filter, (0, 0), special_flags=pygame.BLEND_RGBA_SUB)
|
|
|
|
filter = pygame.surface.Surface((screen.get_width(), screen.get_height()))
|
|
filter.fill(pygame.color.Color(0, 0,0))
|
|
filter.blit(surface5, (0, 0))
|
|
filter.blit(surface6, (0, 0))
|
|
filter.blit(surface7, (0, 0))
|
|
filter.blit(surface8, (0, 0))
|
|
screen.blit(filter, (0, 0), special_flags=pygame.BLEND_RGBA_ADD)
|
|
|
|
|
|
|
|
screen.blit(mask.to_surface(), (0, 0), special_flags=pygame.BLEND_RGBA_MIN)
|
|
|
|
|
|
####
|
|
|
|
pygame.draw.rect(screen, pygame.Color(100, 100, 100), pygame.Rect(200, 200, 20, 20))
|
|
|
|
text_surface = pygame.font.Font(pygame.font.get_default_font(), 48).render(str(player.points), False, pygame.Color(200, 200, 200))
|
|
screen.blit(text_surface, ( 100, 100 ))
|
|
|
|
text_surface = pygame.font.Font(pygame.font.get_default_font(), 48).render(str(int( 1 / deltaTime)), False, pygame.Color(0, 255, 0))
|
|
screen.blit(text_surface, ( screen.get_width() - 200, screen.get_height() - 100 ))
|
|
|
|
# flip() the display to put your work on screen
|
|
pygame.display.flip()
|
|
|
|
clock.tick(60) # limits FPS to 60
|
|
|
|
pygame.quit()
|