Untitled
unknown
plain_text
a year ago
4.4 kB
5
Indexable
import pygame
import random
import sys
# Initialize Pygame
pygame.init()
# Game settings
SCREEN_WIDTH = 400
SCREEN_HEIGHT = 600
FPS = 60
GRAVITY = 0.5
FLAP_STRENGTH = -10
PIPE_WIDTH = 60
PIPE_GAP = 150
PIPE_VELOCITY = 3
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
RED = (255, 0, 0)
# Set up screen
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Flappy Dragon")
# Fonts
font = pygame.font.SysFont("Arial", 30)
# Game objects
class Dragon:
def __init__(self):
self.x = 100
self.y = SCREEN_HEIGHT // 2
self.width = 40
self.height = 40
self.velocity = 0
def update(self):
self.velocity += GRAVITY
self.y += self.velocity
if self.y > SCREEN_HEIGHT - self.height:
self.y = SCREEN_HEIGHT - self.height
self.velocity = 0
elif self.y < 0:
self.y = 0
self.velocity = 0
def flap(self):
self.velocity = FLAP_STRENGTH
def draw(self):
pygame.draw.rect(screen, RED, (self.x, self.y, self.width, self.height))
class Pipe:
def __init__(self, x):
self.x = x
self.height = random.randint(100, SCREEN_HEIGHT - PIPE_GAP - 100)
self.gap = PIPE_GAP
self.width = PIPE_WIDTH
def update(self):
self.x -= PIPE_VELOCITY
def draw(self):
pygame.draw.rect(screen, GREEN, (self.x, 0, self.width, self.height))
pygame.draw.rect(screen, GREEN, (self.x, self.height + self.gap, self.width, SCREEN_HEIGHT - self.height - self.gap))
# Game loop
def main():
clock = pygame.time.Clock()
dragon = Dragon()
pipes = [Pipe(SCREEN_WIDTH + 100)]
score = 0
running = True
game_over = False
def show_message(text, x, y, size=30, color=WHITE):
label = font.render(text, True, color)
screen.blit(label, (x, y))
# Title screen
while True:
screen.fill(BLUE)
show_message("Flappy Dragon", SCREEN_WIDTH // 4, SCREEN_HEIGHT // 4, 50, WHITE)
show_message("Press SPACE to Start", SCREEN_WIDTH // 4 + 20, SCREEN_HEIGHT // 2, 30, WHITE)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
return # Start the game
# Main game loop
while running:
screen.fill(BLUE)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
dragon.flap()
# Update game objects
dragon.update()
for pipe in pipes:
pipe.update()
if pipe.x + pipe.width < 0: # Pipe has passed off the screen
pipes.remove(pipe)
pipes.append(Pipe(SCREEN_WIDTH))
score += 1
# Check for collisions
for pipe in pipes:
if (dragon.x + dragon.width > pipe.x and dragon.x < pipe.x + pipe.width):
if not (dragon.y + dragon.height < pipe.height or dragon.y > pipe.height + pipe.gap):
game_over = True
# Draw game objects
dragon.draw()
for pipe in pipes:
pipe.draw()
# Draw score
show_message(f"Score: {score}", 10, 10, 30, WHITE)
# Game over screen
if game_over:
show_message("GAME OVER", SCREEN_WIDTH // 4 + 20, SCREEN_HEIGHT // 2, 50, WHITE)
show_message(f"Final Score: {score}", SCREEN_WIDTH // 4 + 20, SCREEN_HEIGHT // 2 + 50, 30, WHITE)
show_message("Press SPACE to Restart", SCREEN_WIDTH // 4 + 20, SCREEN_HEIGHT // 2 + 100, 30, WHITE)
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
main() # Restart the game
pygame.display.update()
clock.tick(FPS)
if __name__ == "__main__":
main()
Editor is loading...
Leave a Comment