从 Pygame 中的地图收集硬币

发布于 2025-01-17 00:57:26 字数 6010 浏览 0 评论 0原文

我正在尝试制作 pygame 游戏,但遇到了问题。我希望当角色收集硬币时,分数会改变一,硬币会消失。我还希望硬币出现在玩家周围的随机位置。 我已经尝试了一周了,但我仍然不知道该怎么做,而且我的时间很短:/

有什么帮助吗?

这是我的代码:

import pygame
import sys
from pygame import mixer
import random

pygame.init()


display = pygame.display.set_mode((1280, 720))
clock = pygame.time.Clock()


pygame.display.set_caption("Testowa nazwa bez pomysłu :/")
program_ico = pygame.image.load("icon.jpg")
pygame.display.set_icon(program_ico)

mixer.music.load('music.mp3')
mixer.music.play(-1)

bg = pygame.image.load('bg3.png')


player_walk_images = [pygame.image.load("pp1.png"), pygame.image.load("pp2.png"), pygame.image.load("pp3.png"), pygame.image.load("pp4.png")]
player_walk_up = [pygame.image.load("pb1.png"), pygame.image.load("pb2.png"), pygame.image.load("pb3.png"), pygame.image.load("pb4.png")]
player_walk_down = [pygame.image.load("pf1.png"), pygame.image.load("pf2.png"), pygame.image.load("pf3.png"), pygame.image.load("pf4.png")]
player_stay = [pygame.image.load("ps1.png"),pygame.image.load("ps2.png"),pygame.image.load("ps3.png"),pygame.image.load("ps4.png")]

txt_coin = [pygame.image.load("coin1.png"),pygame.image.load("coin2.png"),pygame.image.load("coin3.png"),pygame.image.load("coin4.png"),pygame.image.load("coin5.png"),pygame.image.load("coin6.png"),pygame.image.load("coin7.png"),pygame.image.load("coin8.png"),pygame.image.load("coin9.png"),pygame.image.load("coin10.png"),pygame.image.load("coin11.png"),pygame.image.load("coin12.png"),pygame.image.load("coin13.png"),pygame.image.load("coin14.png"),pygame.image.load("coin15.png"),pygame.image.load("coin16.png"),pygame.image.load("coin17.png"),pygame.image.load("coin18.png"),pygame.image.load("coin19.png"),pygame.image.load("coin20.png"),pygame.image.load("coin21.png")]




borderN = -840
borderE = 1085
borderS = 1050
borderW = -870



class Player:
    def __init__(self, x, y, width, height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.animation_count = 0
        self.moving_right = False
        self.moving_left = False
        self.moving_up = False
        self.moving_down = False
        self.staing = True
        self.hitbox = pygame.Rect(self.x, self.y,self.width, self.height)

    def main(self, display):
        if self.animation_count + 1 >= 16:
            self.animation_count = 0

        self.animation_count += 1

        if self.moving_right:
            display.blit(pygame.transform.scale(player_walk_images[self.animation_count//4], (128, 128)), (self.x, self.y))
        elif self.moving_left:
            display.blit(pygame.transform.scale(pygame.transform.flip(player_walk_images[self.animation_count//4], True, False), (128, 128)), (self.x, self.y))
        elif self.moving_up:
            display.blit(pygame.transform.scale(player_walk_up[self.animation_count//4], (128, 128)), (self.x, self.y))
        elif self.moving_down:
            display.blit(pygame.transform.scale(player_walk_down[self.animation_count//4], (128, 128)), (self.x, self.y))

        elif self.staing:
            display.blit(pygame.transform.scale(player_stay[self.animation_count//4], (128, 128)), (self.x, self.y))

        self.moving_right = False
        self.moving_left = False

player = Player(560,300, 32, 32)

display_scroll = [0,0]


class Cash:
    def __init__(self,x,y,width,height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.hitbox = pygame.Rect(self.x, self.y,self.width,self.height)
        self.animation_count = 0

    def tick(self):
        self.hitbox = pygame.Rect(self.x, self.y)

    def draw(self,display):
        if self.animation_count + 1 >= 16:
            self.animation_count = 0

        self.animation_count += 1
        if True:
            display.blit(pygame.transform.scale(txt_coin[self.animation_count//21], (128, 128)), (self.x, self.y))



rx = random.randint(-870, 1085)
ry = random.randint(-840, 1050)




clocker = 0
score = 0
coins = []
while True:
    display.fill((24,164,86))
    #display.blit(bg)



    for coin in coins:
        if player.hitbox.colliderect(coin.hitbox):
            coins.remove(coin)
            score += 1

    mouse_x, mouse_y = pygame.mouse.get_pos()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
            sys.exit(0)

    keys = pygame.key.get_pressed()

    #pygame.draw.rect(display, (255,255,255), (100-display_scroll[0], 100-display_scroll[1], 16, 16))
    display.blit(bg,((-940-display_scroll[0], -1200-display_scroll[1], 16, 16)))

    if keys[pygame.K_a]:
        display_scroll[0] -= 5
        staing = False
        player.moving_left = True

    elif keys[pygame.K_d]:
        display_scroll[0] += 5
        staing = False
        player.moving_right = True

    elif keys[pygame.K_w]:
        display_scroll[1] -= 5
        staing = False
        player.moving_up = True
        player.moving_down = False

    elif keys[pygame.K_s]:
        display_scroll[1] += 5
        staing = False
        player.moving_down = True
        player.moving_up = False

    else:
        player.moving_down = False
        player.moving_up = False
        staing = True

    if display_scroll[0] < borderW:
        display_scroll[0] = borderW
    if display_scroll[0] > borderE:
        display_scroll[0] = borderE
    if display_scroll[1] < borderN:
        display_scroll[1] = borderN
    if display_scroll[1] > borderS:
        display_scroll[1] = borderS

    cash = Cash(-display_scroll[0], -display_scroll[1], 16, 16)

    clocker += pygame.time.Clock().tick(60) / 1000
    if clocker >= 2:
        clocker = 0
        coins.append(cash)

    player.main(display)
    cash.draw(display)

    clock.tick(60)
    pygame.display.update()


希望快速帮助

大多数情况下,当我尝试解决方案时,程序没有显示任何错误,并且硬币在任何地方都看不到://

如果您需要其他资源(例如纹理),请写

I'm trying to make a pygame game but I'm having a problem. I want when a character collects a coin, the score will change by one and the coin will disappear. I would also like the coins to appear in random places around the player.
I've been trying to do it for a week and I still don't know how to do it, and my time is short :/

Any help?

This is my code:

import pygame
import sys
from pygame import mixer
import random

pygame.init()


display = pygame.display.set_mode((1280, 720))
clock = pygame.time.Clock()


pygame.display.set_caption("Testowa nazwa bez pomysłu :/")
program_ico = pygame.image.load("icon.jpg")
pygame.display.set_icon(program_ico)

mixer.music.load('music.mp3')
mixer.music.play(-1)

bg = pygame.image.load('bg3.png')


player_walk_images = [pygame.image.load("pp1.png"), pygame.image.load("pp2.png"), pygame.image.load("pp3.png"), pygame.image.load("pp4.png")]
player_walk_up = [pygame.image.load("pb1.png"), pygame.image.load("pb2.png"), pygame.image.load("pb3.png"), pygame.image.load("pb4.png")]
player_walk_down = [pygame.image.load("pf1.png"), pygame.image.load("pf2.png"), pygame.image.load("pf3.png"), pygame.image.load("pf4.png")]
player_stay = [pygame.image.load("ps1.png"),pygame.image.load("ps2.png"),pygame.image.load("ps3.png"),pygame.image.load("ps4.png")]

txt_coin = [pygame.image.load("coin1.png"),pygame.image.load("coin2.png"),pygame.image.load("coin3.png"),pygame.image.load("coin4.png"),pygame.image.load("coin5.png"),pygame.image.load("coin6.png"),pygame.image.load("coin7.png"),pygame.image.load("coin8.png"),pygame.image.load("coin9.png"),pygame.image.load("coin10.png"),pygame.image.load("coin11.png"),pygame.image.load("coin12.png"),pygame.image.load("coin13.png"),pygame.image.load("coin14.png"),pygame.image.load("coin15.png"),pygame.image.load("coin16.png"),pygame.image.load("coin17.png"),pygame.image.load("coin18.png"),pygame.image.load("coin19.png"),pygame.image.load("coin20.png"),pygame.image.load("coin21.png")]




borderN = -840
borderE = 1085
borderS = 1050
borderW = -870



class Player:
    def __init__(self, x, y, width, height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.animation_count = 0
        self.moving_right = False
        self.moving_left = False
        self.moving_up = False
        self.moving_down = False
        self.staing = True
        self.hitbox = pygame.Rect(self.x, self.y,self.width, self.height)

    def main(self, display):
        if self.animation_count + 1 >= 16:
            self.animation_count = 0

        self.animation_count += 1

        if self.moving_right:
            display.blit(pygame.transform.scale(player_walk_images[self.animation_count//4], (128, 128)), (self.x, self.y))
        elif self.moving_left:
            display.blit(pygame.transform.scale(pygame.transform.flip(player_walk_images[self.animation_count//4], True, False), (128, 128)), (self.x, self.y))
        elif self.moving_up:
            display.blit(pygame.transform.scale(player_walk_up[self.animation_count//4], (128, 128)), (self.x, self.y))
        elif self.moving_down:
            display.blit(pygame.transform.scale(player_walk_down[self.animation_count//4], (128, 128)), (self.x, self.y))

        elif self.staing:
            display.blit(pygame.transform.scale(player_stay[self.animation_count//4], (128, 128)), (self.x, self.y))

        self.moving_right = False
        self.moving_left = False

player = Player(560,300, 32, 32)

display_scroll = [0,0]


class Cash:
    def __init__(self,x,y,width,height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.hitbox = pygame.Rect(self.x, self.y,self.width,self.height)
        self.animation_count = 0

    def tick(self):
        self.hitbox = pygame.Rect(self.x, self.y)

    def draw(self,display):
        if self.animation_count + 1 >= 16:
            self.animation_count = 0

        self.animation_count += 1
        if True:
            display.blit(pygame.transform.scale(txt_coin[self.animation_count//21], (128, 128)), (self.x, self.y))



rx = random.randint(-870, 1085)
ry = random.randint(-840, 1050)




clocker = 0
score = 0
coins = []
while True:
    display.fill((24,164,86))
    #display.blit(bg)



    for coin in coins:
        if player.hitbox.colliderect(coin.hitbox):
            coins.remove(coin)
            score += 1

    mouse_x, mouse_y = pygame.mouse.get_pos()

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
            sys.exit(0)

    keys = pygame.key.get_pressed()

    #pygame.draw.rect(display, (255,255,255), (100-display_scroll[0], 100-display_scroll[1], 16, 16))
    display.blit(bg,((-940-display_scroll[0], -1200-display_scroll[1], 16, 16)))

    if keys[pygame.K_a]:
        display_scroll[0] -= 5
        staing = False
        player.moving_left = True

    elif keys[pygame.K_d]:
        display_scroll[0] += 5
        staing = False
        player.moving_right = True

    elif keys[pygame.K_w]:
        display_scroll[1] -= 5
        staing = False
        player.moving_up = True
        player.moving_down = False

    elif keys[pygame.K_s]:
        display_scroll[1] += 5
        staing = False
        player.moving_down = True
        player.moving_up = False

    else:
        player.moving_down = False
        player.moving_up = False
        staing = True

    if display_scroll[0] < borderW:
        display_scroll[0] = borderW
    if display_scroll[0] > borderE:
        display_scroll[0] = borderE
    if display_scroll[1] < borderN:
        display_scroll[1] = borderN
    if display_scroll[1] > borderS:
        display_scroll[1] = borderS

    cash = Cash(-display_scroll[0], -display_scroll[1], 16, 16)

    clocker += pygame.time.Clock().tick(60) / 1000
    if clocker >= 2:
        clocker = 0
        coins.append(cash)

    player.main(display)
    cash.draw(display)

    clock.tick(60)
    pygame.display.update()


Hope for quick help

Most often, when I tried a solution, the program did not show any errors, and the coin was not visible anywhere ://

If you need other resources like textures please write

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

难如初 2025-01-24 00:57:26

所以基本上你可以做一个 def 来检查你是否靠近硬币,如下所示:

collected = False
def collectCheck(x2, x1, y2, y1):
global collected
distance = math.sqrt(math.pow(x2 - x1, 2) + (math.pow(y2 - y1, 2)))
    
# if the distance is smaller than the distance u set then the apple is collected
if distance < 70:
    return True
else:
    return False

然后你可以做一个 if 语句来检查它是否为真然后增加点并删除硬币

score = 1
if collected:
        score += 1
        display.blit('image you want', (random coords that is out of the screen))

最后制作一个随机坐标对于你的硬币,你可以这样做:

coinX = random.randint(0, screen width)
coinY = random.randint(0, screen height)

So basically you can make a def to check if you are close to the coin like this:

collected = False
def collectCheck(x2, x1, y2, y1):
global collected
distance = math.sqrt(math.pow(x2 - x1, 2) + (math.pow(y2 - y1, 2)))
    
# if the distance is smaller than the distance u set then the apple is collected
if distance < 70:
    return True
else:
    return False

and then you can make an if statement to check if it is true or not then increase the point and also remove the coin

score = 1
if collected:
        score += 1
        display.blit('image you want', (random coords that is out of the screen))

Finally to make a random coordinate for your coins, you can do this:

coinX = random.randint(0, screen width)
coinY = random.randint(0, screen height)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文