鱼C论坛

 找回密码
 立即注册
查看: 511|回复: 3

[作品展示] popocat(pygame)

[复制链接]
发表于 2024-1-21 20:56:37 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x

import pygame
import sys
from pygame.locals import *

pygame.init()

size = width, height = 600, 400
speed = [-2, 2]
bg = (255, 255, 255)
clock = pygame.time.Clock()
screen = pygame.display.set_mode(size)
pygame.display.set_caption('pygame_popocat')
popocat = pygame.image.load('C:/popocat.jpg')
position = popocat.get_rect()
l_head = popocat
r_head = pygame.transform.flip(popocat, True, False)

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

        if event.type == KEYDOWN:
            if event.key == K_a:
                popocat = l_head
                speed = [-1, 0]
            if event.key == K_d:
                popocat = r_head
                speed = [1, 0]
            if event.key == K_w:
                speed = [0, -1]
            if event.key == K_s:
                speed = [0, 1]

    position = position.move(speed)

    if position.left < 0 or position.right > width:
        popocat = pygame.transform.flip(popocat, True, False)
        speed[0] = -speed[0]

    if position.top < 0 or position.bottom > height:
        speed[1] = -speed[1]


    screen.fill(bg)
    screen.blit(popocat, position)

    pygame.display.flip()
    pygame.time.delay(5)

    clock.tick(200)
图片在这(记得改名)

一张图片

一张图片

评分

参与人数 3荣誉 +9 鱼币 +4 贡献 +9 收起 理由
cjjJasonchen + 2 + 1 + 3
liuhongrun2022 + 5 + 3
不二如是 + 2 + 3 + 3 鱼C有你更精彩^_^

查看全部评分

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2024-1-21 21:02:17 | 显示全部楼层
你好!关于你的代码,我注意到有几个地方需要补充完整。首先是速度(speed)的设置,它控制了popocat移动的速度。可以通过定义一个变量来表示速度,例如speed = 5。另外,你需要在键盘事件中设置相应的速度值。

其次,你还需要加载背景图片,可以使用pygame.image.load()函数来加载图片,并使用screen.blit()函数将背景图片绘制到屏幕上。

最后,你需要将popocat图片路径修改为正确的路径,以便程序能够找到它。

下面是修改后的代码示例:

  1. import pygame
  2. import sys
  3. from pygame.locals import *

  4. pygame.init()

  5. size = width, height = 600, 400
  6. speed = 5
  7. bg_color = (255, 255, 255)

  8. clock = pygame.time.Clock()
  9. screen = pygame.display.set_mode(size)
  10. pygame.display.set_caption('pygame_popocat')

  11. background = pygame.image.load('path/to/background.jpg')
  12. popocat = pygame.image.load('path/to/popocat.jpg')
  13. position = popocat.get_rect()
  14. l_head = popocat
  15. r_head = pygame.transform.flip(popocat, True, False)

  16. while True:
  17.     for event in pygame.event.get():
  18.         if event.type == pygame.QUIT:
  19.             sys.exit()

  20.         if event.type == KEYDOWN:
  21.             if event.key == K_a:
  22.                 popocat = l_head
  23.                 speed = -5
  24.             if event.key == K_d:
  25.                 popocat = r_head
  26.                 speed = 5
  27.             if event.key == K_w:
  28.                 speed = -5
  29.             if event.key == K_s:
  30.                 speed = 5

  31.     position = position.move(speed, 0)

  32.     if position.left < 0 or position.right > width:
  33.         popocat = pygame.transform.flip(popocat, True, False)
  34.         speed = -speed

  35.     screen.blit(background, (0, 0))  # 绘制背景图片
  36.     screen.blit(popocat, position)

  37.     pygame.display.flip()
  38.     clock.tick(60)  # 控制帧率
复制代码


记得将`path/to/background.jpg`和`path/to/popocat.jpg`替换为你实际存储图片的路径。

希望这可以帮到你,祝你编程愉快!

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-1-21 22:11:41 | 显示全部楼层
好玩
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2024-1-22 09:58:41 | 显示全部楼层
可爱捏,朴实无华的小游戏
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-7 14:03

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表