python pygame游戏我的苹果

2024-04-16 20:44:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我在看SentDex,youtuber,关于pygame教程制作一个“简单”的游戏。。。 所以我决定翻修它,做我自己的,但和他的公寓一样。 当你玩的时候,苹果从树上掉下来,你必须用手推车抓住它们,然而,当我想抓住它的时候,苹果会跌到谷底,但是当苹果撞到边缘的时候,它不会跌到谷底。代码如下:

import pygame
import time
import random

pygame.init()
width = 800
height = 600
gameDisplay = pygame.display.set_mode((width,height))
pygame.display.set_caption("Catching apples")
clock = pygame.time.Clock()
apple = pygame.image.load("jabuka.png")

cartImg = pygame.image.load("gajba.png")
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
def apples(x,y):
    gameDisplay.blit(apple,(x,y))
def text_objects(text,font):
    textSurface = font.render(text,True,black)
    return textSurface,textSurface.get_rect()




def message_dispaly(text):
    largeText = pygame.font.Font("freesansbold.ttf",90)
    TextSurf, TextRect = text_objects(text,largeText)
    TextRect.center = ((sirina/2),(visina/2))
    gameDisplay.blit(TextSurf,TextRect)
    pygame.display.update()

    time.sleep(2)

    game_loop()


def n_uhvatili():
    message_dispaly("You did no catch the apple!")

def gajba(x,y):
    gameDisplay.blit(cartImg,(x,y))

def game_loop():
    #About apple.
    width_apple = 50
    height_apple = 82
    apple_startx = random.randrange(0,width-apple_width)
    apple_starty = -600
    apple_speed = 8
    x_cart = (width * 0.45)
    y_cart = (height * 0.8)
    x_change = 0
    cart_width = 193
    car_height = 105

    gameExit = False

    while not gameExit:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                gameEXit = True
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    x_change = -8
                elif event.key == pygame.K_RIGHT:
                    x_change = 8
                elif event.tyoe == pygame.K_q:
                    pygame.quit()
                    quit()

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                    x_change = 0



        x_cart += x_change

        gameDisplay.fill(white)
        jabuke(apple_startx,apple_starty)
        apple_starty += apple_speed

        if apple_starty > height:
            n_uhvatili()



        gajba(x_gajbe,y_gajbe)
        if x_cart > width - cart_width:
            x_cart = width - cart_width
        if x_cart < 0:
            x_cart = 0
#Note that problem must be in this statement.
        if y_cart < apple_starty + apple_height:
            print("y crossover")
            if x_cart > apple_startx and x_cart < apple_starty + apple_width or x_cart +cart_width > apple_startx and x_cart + cart_width < apple_startx + apple_height:

                print("You catched an apple")
                apple_startx = random.randrange(0, width - apple_width)
                apple_starty = -50


        pygame.display.update()
        clock.tick(59)
game_loop()
pygame.quit()
quit()

我把问题标在哪里了。我不擅长编程,我认为不是每个人都能做到,我的逻辑不好。谢谢你的帮助!你知道吗


Tags: keytexteventappleifdefdisplaychange
1条回答
网友
1楼 · 发布于 2024-04-16 20:44:56

你在x坐标上的情况有问题。要测试是否处于该配置中:

apple_startx    apple_startx+apple_width
     |           |

        ____()__
       /        (
      (        (
      (         (
       \________/
|                                   | 
|                 -| 
x_cart                             x_cart+cart_width 

所以应该是:

if apple_startx > x_cart and apple_startx + apple_width < x_cart + cart_width:

相关问题 更多 >