fly bird游戏
import pygame
import random
pygame.init()
WIDTH = 288
HEIGHT = 512
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('Flappy Bird')
background_img = pygame.image.load('background.png').convert()
bird_img = pygame.image.load('bird.png').convert_alpha()
pipe_img = pygame.image.load('pipe.png').convert_alpha()
game_over_img = pygame.image.load('gameover.png').convert_alpha()
flap_sound = pygame.mixer.Sound('_aigei_com.mp3')
hit_sound = pygame.mixer.Sound('_aigei_com2.mp3')
FPS = 60
GRAVITY = 0.25
JUMP_SPEED = -4
PIPE_GAP = 100
PIPE_VELOCITY = -2
INITIAL_PIPE_X = WIDTH + 20
SCORE_FONT = pygame.font.Font(None, 40)
class Bird:
def __init__(self):
self.x = 50
self.y = HEIGHT // 2
self.velocity = 0
self.image = bird_img
self.rect = self.image.get_rect()
self.rect.center = (self.x, self.y)
def update(self):
self.velocity += GRAVITY
self.y += self.velocity
self.rect.center = (self.x, self.y)
def jump(self):
self.velocity = JUMP_SPEED
flap_sound.play()
def draw(self):
screen.blit(self.image, self.rect)
class Pipe:
def __init__(self, x):
self.x = x
self.y = random.randint(HEIGHT // 4, HEIGHT // 2)
self.image = pipe_img
self.rect_top = self.image.get_rect(topleft=(self.x, self.y - PIPE_GAP))
self.rect_bottom = self.image.get_rect(topleft=(self.x, self.y + PIPE_GAP))
def update(self):
self.x += PIPE_VELOCITY
self.rect_top.topleft = (self.x, self.y - PIPE_GAP)
self.rect_bottom.topleft = (self.x, self.y + PIPE_GAP)
def offscreen(self):
return self.x < -self.rect_top.width
def draw(self):
screen.blit(self.image, self.rect_top)
screen.blit(pygame.transform.flip(self.image, False, True), self.rect_bottom)
class Score:
def __init__(self):
self.value = 0
self.font = SCORE_FONT
self.color = pygame.Color('white')
self.x = WIDTH // 2
self.y = 50
def increment(self):
self.value += 1
def draw(self):
score_text = self.font.render(str(self.value), True, self.color)
score_rect = score_text.get_rect(center=(self.x, self.y))
screen.blit(score_text, score_rect)
bird = Bird()
pipes = [Pipe(INITIAL_PIPE_X)]
score = Score()
clock = pygame.time.Clock()
game_running = True
game_over = False
while game_running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_running = False
elif event.type == pygame.KEYDOWN:
if not game_over and (event.key == pygame.K_SPACE or event.key == pygame.K_UP):
bird.jump()
if not game_over:
bird.update()
score.draw()
score.increment()
pipes_inside_screen = [pipe for pipe in pipes if not pipe.offscreen()]
for pipe in pipes_inside_screen:
pipe.update()
if len(pipes) < 2 or pipes[-1].x < WIDTH - INITIAL_PIPE_X:
pipes.append(Pipe(INITIAL_PIPE_X))
top_pipe_collider = bird.rect.colliderect(pipes[0].rect_top)
bottom_pipe_collider = bird.rect.colliderect(pipes[0].rect_bottom)
if top_pipe_collider or bottom_pipe_collider or bird.y > HEIGHT:
hit_sound.play()
game_over = True
screen.blit(background_img, (0, 0))
bird.draw()
for pipe in pipes:
pipe.draw()
if game_over:
screen.blit(game_over_img, ((WIDTH - game_over_img.get_width()) // 2, (HEIGHT - game_over_img.get_height()) // 2))
pygame.display.flip()
clock.tick(FPS)
pygame.quit()
会动的球游戏
import pygame
import sys
pygame.init()
width, height = 800, 600
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Simple Game")
white = (255, 255, 255)
blue = (0, 0, 255)
ball_radius = 50
ball_x, ball_y = width // 2, height // 2
ball_dx, ball_dy = 5, 5
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
ball_x += ball_dx
ball_y += ball_dy
if ball_x + ball_radius > width or ball_x - ball_radius < 0:
ball_dx *= -1
if ball_y + ball_radius > height or ball_y - ball_radius < 0:
ball_dy *= -1
screen.fill(white)
pygame.draw.circle(screen, blue, (ball_x, ball_y), ball_radius)
pygame.display.flip()
pygame.time.delay(30)
pygame.quit()
sys.exit()