猫史档案馆


【Python作品分享】玩家

用户:梅S3pt梅S3pt查看:0 回复:0 评论:0 创建时间:2023-08-21T20:08:19


【作品展示】

center_image

 

【作品介绍】

??????????

 

【作品源代码】

import pygame
from pygame.locals import *

# 游戏窗口大小
WIDTH = 800
HEIGHT = 600

# 初始化Pygame
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))

# 加载玩家和僵尸的图片
player_image = pygame.image.load('player.png')
zombie_image = pygame.image.load('zombie.png')

# 玩家初始位置和移动速度
player_x = WIDTH // 2
player_y = HEIGHT // 2
player_speed = 2

# 僵尸初始位置和移动速度
zombie_x = 100
zombie_y = 100
zombie_speed = 1

# 主游戏循环
running = True
while running:
    for event in pygame.event.get():
        if event.type == QUIT:
            running = False

    # 获取所有按下的键
    keys = pygame.key.get_pressed()

    # 玩家移动
    if keys[K_LEFT]:
        player_x -= player_speed
        if player_x < 0:
            player_x = 0
    if keys[K_RIGHT]:
        player_x += player_speed
        if player_x > WIDTH - 50:
            player_x = WIDTH - 50
    if keys[K_UP]:
        player_y -= player_speed
        if player_y < 0:
            player_y = 0
    if keys[K_DOWN]:
        player_y += player_speed
        if player_y > HEIGHT - 50:
            player_y = HEIGHT - 50

    # 绘制游戏窗口
    screen.fill((0, 0, 0))
    screen.blit(player_image, (player_x, player_y))
    screen.blit(zombie_image, (zombie_x, zombie_y))

    # 刷新屏幕
    pygame.display.flip()

# 退出游戏
pygame.quit()

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 0下一页