我已经99%完成了我的第一个游戏的编程。我真的很高兴它的工作方式,只有一件事我想改变,但我不知道如何
这是我的代码:
"""Program based on
https://www.youtube.com/watch?v=-8n91btt5d8&list=PLNAPMEWWbHeegjsyN7_pSwXbYPrbyiYhh&index=2&t=1849s"""
import pygame
import sys
import time
import random
pygame.init()
width = 1400
height = 800
green = (0, 51, 0)
black = (0, 0, 0)
yellow = (255, 255, 0)
white = (255, 255, 255)
player_location = [width - 1350, height - 200]
player_size = [100, 200]
obsticle_size = [101, 101]
obsticle_location = [width - 100, height - 100]
obsticle_list = [obsticle_location]
screen = pygame.display.set_mode((width, height))
speed = 10
score = 0
clock = pygame.time.Clock()
FPS = 60
myFont = pygame.font.SysFont("monospace", 35)
run = True
def set_speed(score, speed): #speed up game as score gets higher
if score < 5:
speed = 10
elif score < 10:
speed = 15
elif score < 20:
speed = 20
elif score < 35:
speed = 25
elif score < 50:
speed = 30
elif score < 75:
speed = 35
elif score < 100:
speed = 40
else:
speed = 50
return speed
def spawn_obsticle(obsticle_list): #spawn obsticle on floor, max 3 on screen
delay = random.random()
if len(obsticle_list) < 3 and delay < 0.0085:
x_pos = width - 100
y_pos = height - 100
obsticle_list.append([x_pos, y_pos])
def spawn_obsticle_2(obsticle_list): #spawn obsticle in air, max 3 on screen
delay = random.random()
if len(obsticle_list) < 3 and delay < 0.0085:
x_pos = width - 100
y_pos = height - 300
obsticle_list.append([x_pos, y_pos])
def draw_obsticle(obsticle_list): #create obsticle
for obsticle_location in obsticle_list:
pygame.draw.rect(screen, white, (obsticle_location[0], obsticle_location[1], obsticle_size[0], obsticle_size[1]))
def update_obsticle_positions(obsticle_list, score): #making obsticles move from right to left
for idx, obsticle_location in enumerate(obsticle_list):
if obsticle_location[0] >= 0 and obsticle_location[0] < width:
obsticle_location[0] -= speed
else:
obsticle_list.pop(idx) #drop obsticles off screen, score +1 everytime this happenes
score += 1
return score
def collision_check(obsticle_list, player_location): #detect collision between player and obsticle, part 1
for obsticle_location in obsticle_list:
if detect_collision(obsticle_location, player_location):
return True
return False
def detect_collision(player_location, obsticle_location): #detect collision between player and obsticle, part 2
p_x = player_location[0]
p_y = player_location[1]
p_width = player_size[0]
p_height = player_size[1]
o_x = obsticle_location[0]
o_y = obsticle_location[1]
o_width = obsticle_size[0]
o_height = obsticle_size[1]
if (o_x >= p_x and o_x < (p_x + p_width)) or (p_x >= o_x and p_x < (o_x + o_width)):
if (o_y >= p_y and o_y < (p_y + p_height)) or (p_y >= o_y and p_y < (o_y + o_height)):
return True
return False
while run:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
sys.exit
if event.type == pygame.KEYDOWN:
x = player_location[0] #x location of player
y = player_location[1] #y location of player
p_width = player_size[0] #starting player width
p_height = player_size[1] #starting player height
if event.key == pygame.K_UP: #jump up by changing y location
y -= 200
player_location = [x,y]
if event.type == pygame.KEYUP:
if event.key == pygame.K_SPACE or event.key == pygame.K_UP: #back to starting size and location when releasing key
player_size = [100, 200]
player_location = [50, 600]
keys = pygame.key.get_pressed() #stooping by switching width and heigt of player
if keys[pygame.K_SPACE]:
p_width = player_size[1]
p_height = player_size[0]
y = 700
player_size = [p_width,p_height]
player_location = [x,y]
screen.fill(black)
spawn_obsticle(obsticle_list)
spawn_obsticle_2(obsticle_list)
score = update_obsticle_positions(obsticle_list, score)
speed = set_speed(score, speed)
text = "Score:" + str(score) #making score appear on screen
label = myFont.render(text, 1, yellow)
screen.blit(label, (600, 250))
if collision_check(obsticle_list, player_location): #ending game when there is contact between player and obsticle
run = False
draw_obsticle(obsticle_list)
pygame.draw.rect(screen, green, (player_location[0], player_location[1], player_size[0], player_size[1]))
pygame.display.update()
游戏如果很简单:你是绿色方块,你必须避开白色方块。每避开一个街区,你就得一分。分数越高,障碍物移动越快
我的问题是,有时一个飞块和一个在地板上的块会(几乎)正好在彼此的上面繁殖,使得它不可能躲闪。(第一次运行时可能不会发生,但如果运行几次,肯定会遇到)
我怎样才能使它们总是在彼此之间保持一定距离的情况下产卵呢
我就是想不出来,这会结束我的第一场比赛。提前谢谢 (对不起,有些地方英语不好。英语不是我的母语)
增加每次产卵之间的延迟。将您的spawn_obsticle函数更改为:
这两种功能都可以实现。我建议将两个障碍函数放在一个函数中,并将更改减半,这样两个障碍函数就不会相互重叠,从而形成一个不可能的障碍,例如:
相关问题 更多 >
编程相关推荐