篮子联系人没有重新启动

时间:2018-04-24 22:52:08

标签: python-3.x

我正在尝试制作一款名为Egg Catcher的游戏,出于个人原因(只是为了好玩)而且我对我的代码有点困惑。我希望我的鸡蛋每次被篮子抓住或者到达底部时重置,但它只检查一次接触然后停止检查..请帮忙。

import pygame
import random
caught_egg = False
a = 0
egg_radius = 15
x_egg = 0
y_egg = 5 + egg_radius
x_star = []
y_star = []
x_pos = 200
y_pos = 650
x_speed = 5
x_size = 100
y_size = 70
red = [255, 0, 0]
white = [255, 255, 255]
black = [0,0,0]
yellow = [255, 255, 0]
cyan = [0, 255, 255]
magenta = [255, 0, 255]
beige = [245,245,220]
wheat = [245,222,179]
egg_color_choices = [beige,wheat]

WINDOW_HEIGHT = 800
WINDOW_WIDTH = 500
pygame.init() # initializes the graphics module
window = pygame.display.set_mode((WINDOW_WIDTH,WINDOW_HEIGHT)) # define window size
pygame.display.set_caption('Egg Catcher') # title of program that
# appears on window
# frame
def InitSky(amount):
    for i in range (0, amount):
                x_star.append(random.randint(2, 495))
                y_star.append(random.randint(2, 795))

def DrawSky(amount):
    for i in range(0, amount):
        pygame.draw.circle(window, white, (x_star[i], y_star[i]), 2, )
        y_star[i] = y_star[i] + 1
        if y_star[i] > WINDOW_HEIGHT:
            y_star[i] = 0
def InitEgg():
    x_egg = random.randint(1 + egg_radius, WINDOW_WIDTH - egg_radius)
    return(x_egg)

def EggColor():
    egg_color = random.choice(egg_color_choices)
    return(egg_color)
def DrawEgg():
    pygame.draw.circle(window, egg_color, (x_egg, y_egg), egg_radius,)
x_egg = InitEgg()
egg_color = EggColor()
# your code that draws to the window goes here

clock = pygame.time.Clock() # used to track time within the game (FPS)
quit = False
pygame.key.set_repeat(1)
while not quit: # main program loop
    for event in pygame.event.get(): # check if there were any events
        if event.type == pygame.QUIT: # check if user clicked the upper
            quit = True # right quit button
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RIGHT:
                x_pos = x_pos + x_speed
            if event.key == pygame.K_LEFT:
                x_pos = x_pos - x_speed
            if x_pos <= 0:
                x_pos = x_pos + x_speed
            if x_pos + x_size >= 500:
                x_pos = x_pos - x_speed
            if event.key == pygame.K_KP_MINUS:
                x_speed = x_speed - 1
            if event.key == pygame.K_KP_PLUS:
                x_speed = x_speed + 1
            if x_speed >= 8:
                x_speed = x_speed - 1
            if x_speed <= 4:
                x_speed = x_speed + 1



    window.fill(black)
    InitSky(500)
    DrawSky(500)

    caught_egg = False
    if caught_egg == False:
        DrawEgg()
    if y_egg - egg_radius <= WINDOW_HEIGHT and caught_egg == False:
        y_egg = y_egg + 3
    else:
        y_egg = 0
        x_egg = InitEgg()
    if x_egg + egg_radius > x_pos and x_egg  + egg_radius < x_pos + x_size and y_egg - egg_radius == y_pos:
        caught_egg = True
        y_egg = 0
        x_egg = InitEgg()
        print(y_egg)
    pygame.draw.rect(window, magenta, (x_pos, y_pos, x_size, y_size))
    pygame.display.update() # refresh your display
    clock.tick(60) # wait a certain amount of time that
# ensures a frame rate of 60 fps

pygame.quit() # shutdown module

1 个答案:

答案 0 :(得分:0)

因为这种情况:number of offsets = 7 offsets = 0x6ffff360034 offset 0 = 20 offset 1 = 27a40 offset 2 = 127e20 offset 3 = 128ae0 offset 4 = 47e850 offset 5 = 57ec30 offset 6 = 580230 <__main__.Data object at 0x6ffffd5f400> 7 <__main__.LP_c_int object at 0x6ffffd5f488> 0x20 0x27a40 0x127e20 0x128ae0 0x47e850 0x57ec30 0x580230 。你从第一次20(y_egg - egg_radius == y_pos)开始,所以当你点击665时,这个条件会触发。但是,您将5 + egg_radius重置为0而不是20,这样您就不会再次对准确切的条件了。

你可以用y_egg替换两个y_egg = 0重置,但更好的解决方法是检测y维中的蛋几何是否包含在x中,就像x一样:

y_egg = 5 + egg_radius

实际上你的x逻辑只是检查右边缘是否在桶内,但我会留下来让你修复。