Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save iamutkarshtiwari/957a035bdbcd434b17e8628e6bfa6e68 to your computer and use it in GitHub Desktop.
Save iamutkarshtiwari/957a035bdbcd434b17e8628e6bfa6e68 to your computer and use it in GitHub Desktop.
import sys
import pygame
import time
import random
import os
WIDTH = 360
HEIGHT = 360
FPS = 360
ORANGE = (255, 165, 0)
pygame.init()
pygame.mixer.init()
# Centers the game window
os.environ['SDL_VIDEO_CENTERED'] = '1'
infoObject = pygame.display.Info()
width, height = infoObject.current_w, infoObject.current_h
screen = pygame.display.set_mode((infoObject.current_w * 2 / 3, infoObject.current_h * 2 / 3))
pygame.display.set_caption("My Game")
clock = pygame.time.Clock()
# Variables
watercycle = pygame.image.load(os.path.abspath("watercycle.png"))
# pygame.rect.draw(watercycle, RED,/ ing, width=0)
rectangle = pygame.rect.Rect(10, 10, width * 1/5, width* 1/5)
rectangle_draging = False
# Game Loop
running = True
while running:
# keep loop running at the right speed
clock.tick(FPS)
# Process input (events)
for event in pygame.event.get():
# check for closing window
if event.type == pygame.QUIT:
running = False
# Rectange drag
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
if rectangle.collidepoint(event.pos):
rectangle_draging = True
mouse_x, mouse_y = event.pos
offset_x = rectangle.x - mouse_x
offset_y = rectangle.y - mouse_y
elif event.type == pygame.MOUSEBUTTONUP:
if event.button == 1:
rectangle_draging = False
elif event.type == pygame.MOUSEMOTION:
if rectangle_draging:
mouse_x, mouse_y = event.pos
rectangle.x = mouse_x + offset_x
rectangle.y = mouse_y + offset_y
watercycle = pygame.transform.scale(watercycle, (width * 2/3, height * 2/3))
screen.blit(watercycle,(0,0))
pygame.draw.rect(screen, ORANGE, rectangle, 10)
# after drawing everything, flip the display
pygame.display.flip()
pygame.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment