游戏
import pygame
import random
import sys
初始化pygame
pygame.init()
定义颜色
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
BROWN = (139, 69, 19)
游戏设置
WIDTH, HEIGHT = 800, 400
FPS = 60
GRAVITY = 0.5
JUMP_STRENGTH = -10
MOVE_SPEED = 5
创建游戏窗口
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('平台跳跃爬虫游戏')
clock = pygame.time.Clock()
class Crawler:
def init(self):
self.x = 100
self.y = HEIGHT - 100
self.width = 30
self.height = 30
self.color = GREEN
self.vel_y = 0
self.on_ground = False
self.score = 0
def jump(self):
if self.on_ground:
self.vel_y = JUMP_STRENGTH
self.on_ground = False
def move_left(self):
self.x -= MOVE_SPEED
def move_right(self):
self.x += MOVE_SPEED
def update(self, platforms, coins):
# 应用重力
self.vel_y += GRAVITY
self.y += self.vel_y
# 防止爬虫移出屏幕左右边界
if self.x < 0:
self.x = 0
if self.x + self.width > WIDTH:
self.x = WIDTH - self.width
# 检查是否落地
self.on_ground = False
for platform in platforms:
if (self.y + self.height > platform.y and
self.y + self.height < platform.y + 10 and # 10是平台高度
self.x + self.width > platform.x and
self.x < platform.x + platform.width):
self.y = platform.y - self.height
self.vel_y = 0
self.on_ground = True
# 防止掉出屏幕底部
if self.y > HEIGHT:
self.y = HEIGHT
self.vel_y = 0
self.on_ground = True
# 检查是否吃到金币
for coin in coins[:]: # 使用切片避免在迭代时修改列表
if (self.x < coin.x + coin.radius and
self.x + self.width > coin.x and
self.y < coin.y + coin.radius and
self.y + self.height > coin.y):
coins.remove(coin)
self.score += 1
def render(self, surface):
pygame.draw.rect(surface, self.color, (self.x, self.y, self.width, self.height))
class Platform:
def init(self, x, y, width):
self.x = x
self.y = y
self.width = width
self.height = 10
self.color = BROWN
def render(self, surface):
pygame.draw.rect(surface, self.color, (self.x, self.y, self.width, self.height))
class Coin:
def init(self, x, y):
self.x = x
self.y = y
self.radius = 8
self.color = YELLOW = (255, 215, 0)
def render(self, surface):
pygame.draw.circle(surface, self.color, (int(self.x), int(self.y)), self.radius)
def generate_platforms(num_platforms):
platforms = []
# 第一个平台
platforms.append(Platform(0, HEIGHT - 30, WIDTH))
# 随机生成其他平台
for _ in range(num_platforms - 1):
x = random.randint(0, WIDTH - 100)
y = random.randint(100, HEIGHT - 100)
width = random.randint(50, 200)
# 确保平台不会重叠太多
overlap = True
while overlap:
overlap = False
for platform in platforms:
if (abs(x - platform.x) < 100 and
abs(y - platform.y) < 50):
x = random.randint(0, WIDTH - 100)
y = random.randint(100, HEIGHT - 100)
overlap = True
break
platforms.append(Platform(x, y, width))
return platforms
def generate_coins(num_coins, platforms):
coins = []
for _ in range(num_coins):
platform = random.choice(platforms)
x = random.randint(platform.x + 10, platform.x + platform.width - 10)
y = platform.y - 20 # 放在平台上方
coins.append(Coin(x, y))
return coins
def draw_text(surface, text, size, x, y):
font = pygame.font.SysFont('arial', size)
text_surface = font.render(text, True, WHITE)
text_rect = text_surface.get_rect()
text_rect.topleft = (x, y)
surface.blit(text_surface, text_rect)
def main():
crawler = Crawler()
platforms = generate_platforms(5)
coins = generate_coins(10, platforms)
game_over = False
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
crawler.jump()
if event.key == pygame.K_LEFT:
crawler.move_left()
if event.key == pygame.K_RIGHT:
crawler.move_right()
if event.key == pygame.K_r and game_over:
# 重新开始游戏
crawler = Crawler()
platforms = generate_platforms(5)
coins = generate_coins(10, platforms)
game_over = False
if not game_over:
# 更新游戏状态
crawler.update(platforms, coins)
# 检查是否掉出屏幕底部
if crawler.y > HEIGHT:
game_over = True
# 绘制
screen.fill(BLACK)
for platform in platforms:
platform.render(screen)
for coin in coins:
coin.render(screen)
crawler.render(screen)
draw_text(screen, f'得分: {crawler.score}', 20, 10, 10)
if game_over:
draw_text(screen, '游戏结束!', 36, WIDTH // 2 - 80, HEIGHT // 2 - 50)
draw_text(screen, f'最终得分: {crawler.score}', 24, WIDTH // 2 - 100, HEIGHT // 2)
draw_text(screen, '按R键重新开始', 20, WIDTH // 2 - 80, HEIGHT // 2 + 50)
pygame.display.update()
clock.tick(FPS)
if name == "main":
main()
浙公网安备 33010602011771号