大家好,欢迎来到IT知识分享网。
利用python实现”接水果“游戏,通过键盘上的箭头控制篮子左右移动,以接住屏幕上方随机掉落的水果,若没有接住水果则游戏结束,屏幕显示”Game Over! Press R to restart“,任意位置输入R以继续游戏
代码实现:
# !/usr/bin/env python # -*- coding:utf-8 -*- """ @file: fruit_game.py @author: czx @time: 2024/4/23 17:00 """ import pygame import random # 初始化pygame pygame.init() # 设置常量 SCREEN_WIDTH = 800 SCREEN_HEIGHT = 600 FRUIT_SIZE = 50 GRAVITY = 0.25 BASKET_SPEED = 5 DROP_FRUIT_CHANCE = 20 FONT_SIZE = 35 # 设置字体大小 # 设置屏幕 screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption('Catch the Fruit') # 定义颜色 WHITE = (255, 255, 255) RED = (255, 0, 0) GREEN = (0, 255, 0) BLUE = (0, 0, 255) BLACK = (0, 0, 0) # 创建水果列表 fruit_list = [] # 设置篮子 basket = {
'x': SCREEN_WIDTH // 2 - 50, 'y': SCREEN_HEIGHT - 50, 'width': 100, 'height': 50} # 设置时钟 clock = pygame.time.Clock() # 添加字体设置 font = pygame.font.Font(None, FONT_SIZE) def drop_fruit(): # 只在列表为空时添加一个水果 if not fruit_list: fruit = {
'color': random.choice([RED, GREEN, BLUE]), 'x': random.randint(0, SCREEN_WIDTH - FRUIT_SIZE), 'y': -FRUIT_SIZE, # 初始位置在屏幕上方 'speed': random.randint(1, 4) } fruit_list.append(fruit) def draw_basket(screen): pygame.draw.rect(screen, WHITE, (basket['x'], basket['y'], basket['width'], basket['height'])) def draw_fruits(screen): for fruit in fruit_list: pygame.draw.rect(screen, fruit['color'], (fruit['x'], fruit['y'], FRUIT_SIZE, FRUIT_SIZE)) def update_fruit_positions(): for fruit in fruit_list: fruit['y'] += fruit['speed'] + GRAVITY def check_collision(fruit, basket): return (fruit['x'] + FRUIT_SIZE > basket['x'] and fruit['x'] < basket['x'] + basket['width'] and fruit['y'] + FRUIT_SIZE > basket['y']) def game_over_screen(): text = font.render('Game Over! Press R to restart', True, RED) screen_rect = screen.get_rect() text_rect = text.get_rect(center=screen_rect.center) screen.blit(text, text_rect) def main(): frame_count = 0 # 用于控制水果掉落的计数器 is_game_over = False # 游戏是否结束的标记 while True: # 主循环将一直运行,直到程序退出 for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() return if event.type == pygame.KEYDOWN: if event.key == pygame.K_r and is_game_over: fruit_list.clear() frame_count = 0 is_game_over = False basket['x'] = SCREEN_WIDTH // 2 - 50 screen.fill(BLACK) keys = pygame.key.get_pressed() if keys[pygame.K_LEFT] and basket['x'] > 0: basket['x'] -= BASKET_SPEED if keys[pygame.K_RIGHT] and basket['x'] < SCREEN_WIDTH - basket['width']: basket['x'] += BASKET_SPEED update_fruit_positions() # 绘制所有对象 screen.fill(BLACK) # 用黑色填充屏幕 draw_basket(screen) draw_fruits(screen) # 检查是否接到水果或水果是否掉落 for fruit in fruit_list[::]: if check_collision(fruit, basket): fruit_list.remove(fruit) elif fruit['y'] + FRUIT_SIZE > SCREEN_HEIGHT: fruit_list.remove(fruit) is_game_over = True break # 一旦有水果掉落屏幕底部,就结束当前循环显示游戏结束界面 # 每隔一定时间尝试掉落一个水果 frame_count += 1 if frame_count % DROP_FRUIT_CHANCE == 0 and not is_game_over: drop_fruit() if is_game_over: game_over_screen() pygame.display.flip() clock.tick(30) # 设置帧率为30 if __name__ == '__main__': main()
如果还没下载pygame,可以通过以下命令下载安装:
pip install pygame -i https://pypi.tuna.tsinghua.edu.cn/simple
效果:
以上是一个简易版的接水果游戏,你也可以通过设置背景以及篮子、水果的图片样式或其他使得该游戏更加精美
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/154730.html