超级玛丽python代码
时间: 2023-09-26 08:08:51 浏览: 53
以下是一个简单的 Python 超级玛丽游戏代码:
```python
import pygame
# 初始化 pygame
pygame.init()
# 设置游戏窗口大小
win = pygame.display.set_mode((500, 480))
# 设置游戏标题
pygame.display.set_caption("Super Mario Bros")
# 加载游戏角色图片
walkRight = [pygame.image.load('images/1.png'), pygame.image.load('images/2.png'), pygame.image.load('images/3.png')]
walkLeft = [pygame.image.load('images/4.png'), pygame.image.load('images/5.png'), pygame.image.load('images/6.png')]
bg = pygame.image.load('images/bg.jpg')
char = pygame.image.load('images/standing.png')
# 设置游戏角色位置和大小
x = 50
y = 400
width = 64
height = 64
vel = 5
# 设置游戏时钟
clock = pygame.time.Clock()
# 角色是否面向左右
isJump = False
jumpCount = 10
left = False
right = False
walkCount = 0
# 游戏循环
run = True
while run:
clock.tick(27)
# 循环监听游戏事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
# 获取键盘按键状态
keys = pygame.key.get_pressed()
# 根据按键状态更新角色位置
if keys[pygame.K_LEFT] and x > vel:
x -= vel
left = True
right = False
elif keys[pygame.K_RIGHT] and x < 500 - width - vel:
x += vel
left = False
right = True
else:
left = False
right = False
walkCount = 0
if not(isJump):
if keys[pygame.K_SPACE]:
isJump = True
left = False
right = False
walkCount = 0
else:
if jumpCount >= -10:
neg = 1
if jumpCount < 0:
neg = -1
y -= (jumpCount ** 2) * 0.5 * neg
jumpCount -= 1
else:
isJump = False
jumpCount = 10
# 绘制游戏画面
win.blit(bg, (0, 0))
if walkCount + 1 >= 9:
walkCount = 0
if left:
win.blit(walkLeft[walkCount // 3], (x, y))
walkCount += 1
elif right:
win.blit(walkRight[walkCount // 3], (x, y))
walkCount += 1
else:
win.blit(char, (x, y))
pygame.display.update()
# 退出 pygame
pygame.quit()
```