Untitled
unknown
plain_text
a year ago
3.2 kB
4
Indexable
import pygame
import math
import time
# Initialize pygame
pygame.init()
# Constants for the screen dimensions and movement
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
MAN_WIDTH = 50
MAN_HEIGHT = 100
WALK_SPEED = 5 # pixels per frame
RUN_SPEED = 8 # pixels per frame
BACKFLIP_DURATION = 30 # frames for backflip
# Colors
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
RED = (255, 0, 0)
# Set up the screen
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption('Man Walking, Flipping, and Running Away')
# Main character (representing the "man")
class Man:
def __init__(self):
self.x = SCREEN_WIDTH // 2
self.y = SCREEN_HEIGHT - MAN_HEIGHT - 20
self.width = MAN_WIDTH
self.height = MAN_HEIGHT
self.color = BLUE
self.flip_angle = 0
self.is_flipping = False
self.direction = 1 # 1 for right, -1 for left
def draw(self, screen):
pygame.draw.rect(screen, self.color, (self.x, self.y, self.width, self.height))
def walk(self):
self.x += WALK_SPEED * self.direction
def backflip(self):
self.is_flipping = True
# Rotate the character (simulate backflip by changing angle)
if self.flip_angle < 360:
self.flip_angle += 12
else:
self.flip_angle = 0
self.is_flipping = False
def run(self):
self.x -= RUN_SPEED * self.direction # Run in the opposite direction
def main():
man = Man()
clock = pygame.time.Clock()
running = True
steps = 0
performing_flip = False
while running:
screen.fill(WHITE)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Walking phase (move 10 feet, let's assume 1 foot = 10 pixels)
if steps < 100 and not performing_flip:
man.walk()
steps += WALK_SPEED
elif steps >= 100 and not performing_flip:
# Start backflip after walking 10 feet
performing_flip = True
man.backflip()
# After backflip, run in opposite direction
if performing_flip and man.flip_angle == 0:
man.direction = -1 # change direction to run away
man.run()
# Draw the man
man.draw(screen)
# If backflipping, rotate the man (simulating a backflip)
if man.is_flipping:
rotated_man = pygame.Surface((man.width, man.height))
rotated_man.fill(RED) # Temporary color for rotation visualization
rotated_man.set_colorkey(RED) # Make red color transparent
pygame.draw.rect(rotated_man, BLUE, (0, 0, man.width, man.height))
rotated_man = pygame.transform.rotate(rotated_man, man.flip_angle)
screen.blit(rotated_man, (man.x, man.y))
pygame.display.update()
clock.tick(60) # Limit the framerate to 60 FPS
# To simulate a small delay after backflip before running away
if performing_flip and man.flip_angle == 0:
time.sleep(0.5) # Pause for a moment before running away
pygame.quit()
if __name__ == "__main__":
main()
Editor is loading...
Leave a Comment