我是python编程的初学者。我有一个课程项目,使用python制作游戏。我尝试使用pygame制作鸡蛋和篮子游戏。游戏在某种程度上可以正常工作。我能够使鸡蛋掉落并在按键时移动篮子,但我无法使鸡蛋连续掉落,即只有一个鸡蛋掉下来,然后它停止掉下来。我预计鸡蛋会像实际比赛中那样逐个掉落。

而且我不知道如何知道鸡蛋何时掉进篮子里,以及如何增加鸡蛋掉进篮子时的得分。

你能帮我吗?

Screenshot of my game

#The egg and basket game

import pygame
from pygame.locals import *
import time
import random
clock = pygame.time.Clock()
x=260
y=500
#Screen initialize
pygame.init()
pygame.font.init()
screen=pygame.display.set_mode((600,600))
pygame.display.set_caption("egg")

#Background
cloud=pygame.image.load("clouds.jpg")
cloud=pygame.transform.scale(cloud,(600,600))
screen.blit(cloud,(0,0))

#Basket
basket=pygame.image.load("basket.jpg")
basket=pygame.transform.scale(basket,(80,80))
screen.blit(basket,(x,y))
pygame.display.update()

#egg
egg=pygame.image.load("egg.jpg")
egg=pygame.transform.scale(egg,(20,20))

#screen.blit(egg,(290,20))
pygame.display.update()

#Movement of basket
ychange=0
xchange=0
exiting=False
for yegg in range(20,550):

#for i in range(0,100):
xegg=random.randrange(50,550)
    while not exiting:
   #xegg=random.randrange(50,550)
    #for yegg in range(20,550):
        if yegg<550:
            ychange+=1
            pygame.display.update()
            clock.tick(60)
            screen.blit(egg,(xegg,ychange))

        else:
            yegg=20
            yegg=yegg+ychange
            pygame.display.update()
            clock.tick(60)
            screen.blit(egg,(xegg,yegg))
        #yegg=20
        pygame.display.update()
        clock.tick(60)
    #yegg=20

        for event in pygame.event.get():
            print(event)
            if(event.type==pygame.QUIT):
                exiting=True
                pygame.quit()
                quit()
            if(event.type==pygame.KEYDOWN):
                if(event.key==pygame.K_LEFT):
                    xchange=-5
                if(event.key==pygame.K_RIGHT):
                    xchange=5
            screen.blit(basket,(x,y))
            if(event.type==pygame.KEYUP):
                if(event.key==pygame.K_LEFT or event.key==pygame.K_RIGHT):
                    xchange=0

            x=x+xchange
            print(x)
            screen.blit(cloud,(0,0))

            screen.blit(basket,(x,y))

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

    i=i+1
    ychange=0
    #random position of eggs

    #MOVEMENT OF egg

最佳答案

首先使用.png图像使图像周围的白色正方形消失。
使用pygame.image.load("myimage.png").convert_alpha()

xegg=random.randrange(50,550)


上一行应在while循环内,以便您在每次迭代中获得随机x值。
我对您的代码做了一些更改,现在鸡蛋从随机位置掉下来了。
要抓住它们,您必须检查篮子和鸡蛋之间是否有碰撞。

#The egg and basket game

import pygame
from pygame.locals import *
import time
import random
clock = pygame.time.Clock()
x=260
y=500
#Screen initialize
pygame.init()
pygame.font.init()
screen=pygame.display.set_mode((600,600))
pygame.display.set_caption("egg")

#Background
cloud=pygame.image.load("clouds.png").convert_alpha()
cloud=pygame.transform.scale(cloud,(600,600))
screen.blit(cloud,(0,0))

#Basket
basket=pygame.image.load("basket.png").convert_alpha()
basket=pygame.transform.scale(basket,(80,80))
screen.blit(basket,(x,y))
pygame.display.update()

#egg
egg=pygame.image.load("eggs.png").convert_alpha()
egg=pygame.transform.scale(egg,(20,20))

#screen.blit(egg,(290,20))
pygame.display.update()

#Movement of basket
ychange=0
xchange=0
exiting=False


xegg = random.randrange(50,550)
yegg = 20
while not exiting:
#xegg=random.randrange(50,550)
#for yegg in range(20,550):
    print yegg
    if yegg<550:
        yegg += 5
        pygame.display.update()
        clock.tick(60)
        screen.blit(egg,(xegg,yegg))

    else:
        yegg=20
        xegg = random.randrange(50,550)
        yegg=yegg+ychange
        pygame.display.update()
        clock.tick(60)
        screen.blit(egg,(xegg,yegg))
    #yegg=20
    pygame.display.update()
    clock.tick(60)
    #yegg=20

    for event in pygame.event.get():
        print(event)
        if(event.type==pygame.QUIT):
            exiting=True
            pygame.quit()
            quit()
        if(event.type==pygame.KEYDOWN):
            if(event.key==pygame.K_LEFT):
                xchange=-5
            if(event.key==pygame.K_RIGHT):
                xchange=5
        screen.blit(basket,(x,y))
        if(event.type==pygame.KEYUP):
            if(event.key==pygame.K_LEFT or event.key==pygame.K_RIGHT):
                xchange=0

        x=x+xchange
        print(x)
        screen.blit(cloud,(0,0))

        screen.blit(basket,(x,y))

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

i=i+1
ychange=0
    #random position of eggs

    #MOVEMENT OF egg


转到pygame collisions并学习collisons。
转到here以获得一些非常好的示例。还要here获得出色的Pygame指南。

关于python - Python中的鸡蛋和篮子游戏,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36871380/

10-11 06:47