给我用python写一个超级玛丽
时间: 2024-05-29 17:04:10 浏览: 97
Python版超级玛丽源码
很高兴能接到您的问题!为您提供代码如下:
```python
import pygame
pygame.init()
screen = pygame.display.set_mode((800, 600))
# 设置窗口标题
pygame.display.set_caption("Super Mario")
# 加载图片
bg_img = pygame.image.load("bg.png")
mario_img = pygame.image.load("mario.png")
mushroom_img = pygame.image.load("mushroom.png")
# 设置初始位置和速度
mario_x, mario_y = 50, 450
mario_vel = 5
mushroom_x, mushroom_y = 700, 450
mushroom_vel = 3
# 游戏循环
finished = False
while not finished:
for event in pygame.event.get():
if event.type == pygame.QUIT:
finished = True
# 绘制背景和角色
screen.blit(bg_img, (0, 0))
screen.blit(mario_img, (mario_x, mario_y))
screen.blit(mushroom_img, (mushroom_x, mushroom_y))
# 判断是否按下键盘
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
mario_x -= mario_vel
if keys[pygame.K_RIGHT]:
mario_x += mario_vel
# 蘑菇移动
if mushroom_x <= 0 or mushroom_x >= 750:
mushroom_vel = -mushroom_vel
mushroom_x -= mushroom_vel
# 判断是否碰撞
if mario_x + 50 >= mushroom_x and mario_x <= mushroom_x + 50:
if mario_y + 50 >= mushroom_y and mario_y <= mushroom_y + 50:
mushroom_x = 700
mushroom_y = 450
# 更新窗口
pygame.display.update()
pygame.quit()
```
希望这个小游戏能让您玩得开心!如果还有其它问题,欢迎随时跟我交流哦!
阅读全文