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
解决方案
就是因为这个条件:y_egg - egg_radius == y_pos
。您第一次从 20 开始 ( 5 + egg_radius
) 所以当您达到 665 时触发该条件。但是随后您将重置y_egg
为 0 而不是 20,因此您再也不会在确切的条件下对齐。
您可以将两个y_egg = 0
重置替换为y_egg = 5 + egg_radius
,但更好的解决方法是检测鸡蛋几何形状是否包含在 y 维度的桶中,就像您对 x 所做的那样:
if x_egg + egg_radius > x_pos and \
x_egg + egg_radius < x_pos + x_size and \
y_egg - egg_radius >= y_pos and \
y_egg + egg_radius <= y_pos + y_size:
caught_egg = True
y_egg = 0
x_egg = InitEgg()
实际上,您的 x 逻辑仅检查右边缘是否在桶内,但我会留给您解决。
推荐阅读
- c# - Postgres c# 监听超时
- c++ - 如何减少花费的时间
- javascript - Angular 选择器组件生命周期 - ngoninit/ngondestroy/等。如果嵌套在另一个之下,则会触发。这种行为可以避免吗?
- sql-server - 添加类型的类似hashmap的列
实体 - python - Pandas Dataframe - 在多列上装箱并在另一列上获取统计信息
- sql - 在SQL中的string_agg之后计算每一行中的每个值
- javascript - 将其从 jquery 转换为 javascript 后脚本无法正常工作
- mysql - 如何在我的节点应用程序中使用远程数据库
- python - 如何从 HashMap 中检索数据?
- javascript - Angular 9 - 你必须传入 NgModule 或 NgModuleFactory 才能被引导