I have a rotating square that moves around the screen and am tasked with (1) Setting boundaries to keep it within the screen and (2) Collision detection with a similar rotating square. I require help with the first issue.
P.S. To my teacher (if you're reading this), I have been going at this specific problem for over 6 hours already and must resort to consulting others for help. Some problems can sustain the assault of sustained thinking, failure is not delayed success - it's just failure, skill is not 99% effort + 1% talent - it's the other way around, etc. etc.
Note: Sorry for the poor formatting. It's my first time using Stack Overflow; was using Y!A before but there aren't many people in the programming section there
import pygame
import random
pygame.init() # Initialize py-game
screen = pygame.display.set_mode((800, 500)) # Create screen
color = (255, 240, 245)
# Initial Coordinates
objectX, objectY = random.randint(100, 700), random.randint(100, 400)
fps = pygame.time.Clock()
velocity = [8, 6] # speed
velocity2 = [6, 8]
size = 0 # size
drag = False
# Rotating the object
sat_orig = pygame.Surface((70, 70))
sat_orig.set_colorkey((0, 0, 0))
sat_orig.fill(color)
sat = sat_orig.copy()
sat.set_colorkey((0, 0, 0))
rect = sat.get_rect()
rect.center = (800 // 2, 500 // 2)
rotation = 0
rotation_speed = 2
# --------------------------------------------------------------------------
running = True
while running: # Game Loop
screen.fill((0, 0, 0))
pygame.display.set_caption('Collision Interactions ' + str(pygame.mouse.get_pos())) # caption
# exit button
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONUP:
if event.button == 1:
drag = False
rotation_speed = 2
elif event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
drag = True
rotation_speed = 0
size = 70
old_center = rect.center
rotation = (rotation + rotation_speed) % 360
new_image = pygame.transform.rotate(sat_orig, rotation)
rect = new_image.get_rect()
rect.center = old_center
screen.blit(new_image, (objectX, objectY))
objectArea = rect
############
# Movement #
############
if drag: # Manual
if objectArea.collidepoint(event.pos):
objectX, objectY = pygame.mouse.get_pos()
objectX -= size // 2
objectY -= size // 2
else: # Automatic
objectX += velocity[0]
objectY += velocity[1]
##############
# Boundaries #
##############
if objectX + size > 800 or objectX + size < 0:
velocity[0] = -velocity[0]
if objectY + size > 500 or objectY + size < 0:
velocity[1] = -velocity[1]
fps.tick(80)
pygame.display.update() # Displays updates