用python 写一个超级玛丽游戏
时间: 2023-05-19 11:06:26 浏览: 163
python超级玛丽项目源码.rar
好的,以下是一个简单的超级玛丽游戏的 Python 代码:
```python
import pygame
pygame.init()
# 设置游戏窗口大小
win = pygame.display.set_mode((500, 480))
# 设置游戏标题
pygame.display.set_caption("Super Mario")
# 加载玛丽图片
walkRight = [pygame.image.load('images/pygame_right_1.png'), pygame.image.load('images/pygame_right_2.png'), pygame.image.load('images/pygame_right_3.png')]
walkLeft = [pygame.image.load('images/pygame_left_1.png'), pygame.image.load('images/pygame_left_2.png'), pygame.image.load('images/pygame_left_3.png')]
bg = pygame.image.load('images/pygame_bg.jpg')
char = pygame.image.load('images/pygame_stand.png')
# 设置玛丽的位置和速度
x = 50
y = 400
width = 64
height = 64
vel = 5
# 设置玛丽的动画
clock = pygame.time.Clock()
# 设置是否面向右边
isRight = True
# 设置是否在移动
isMoving = False
# 绘制游戏界面
def redrawGameWindow():
global isMoving
win.blit(bg, (0,0))
if isMoving:
if isRight:
win.blit(walkRight[walkCount//3], (x,y))
else:
win.blit(walkLeft[walkCount//3], (x,y))
walkCount += 1
if walkCount >= 9:
walkCount = 0
else:
if isRight:
win.blit(walkRight[0], (x,y))
else:
win.blit(walkLeft[0], (x,y))
pygame.display.update()
# 游戏主循环
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
isRight = False
isMoving = True
elif keys[pygame.K_RIGHT] and x < 500 - width - vel:
x += vel
isRight = True
isMoving = True
else:
isMoving = False
walkCount = 0
redrawGameWindow()
pygame.quit()
```
希望这个代码能够帮到你!
阅读全文