我正在翻拍pygame中的flappy bird,但以星球大战为主题。我已经完成了艺术和游戏的一般格式,但现在我需要调整的细节。我一直在切换数字,试图让光剑完全到达屏幕的顶部和底部,因为目前有些间隙不是预期的空间通过。
import pygame
from random import randint
from pygame.locals import *
#Define Colors - RGB
black = (0,0,0)
white = (255,255,255)
green = (0,255,0)
red = (255,0,0)
pygame.init()
#Screen Size
size = 700,500
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Flappy Bird in Python")
done = False
clock = pygame.time.Clock()
def ball(x,y):
#Radius of 20 px
ballImg1 = pygame.image.load('PlayerFrame1.png')
ballImg1 = pygame.transform.scale(ballImg1,(50,50))
screen.blit(ballImg1, (x,y))
ballImg2 = pygame.image.load('PlayerFrame2.png')
ballImg2 = pygame.transform.scale(ballImg2,(50,50))
screen.blit(ballImg2, (x,y))
def gameover():
font = pygame.font.SysFont(None,55)
text = font.render("Game Over! Try Again?",True,red)
screen.blit(text, [150,250])
def obstacle(xloc,yloc,xsize,ysize):
pipe = pygame.image.load('blade.png')
pipe1 = pygame.transform.scale(pipe,(xsize,ysize))
pipe2 = pygame.transform.scale(pipe,(xsize,500))
screen.blit(pipe1,[xloc,yloc,xsize,ysize])
screen.blit(pipe2,[xloc,int(yloc+ysize+space),xsize,ysize+500]))
#If the ball is between 2 points on the screen, increment score
def Score(score):
font = pygame.font.SysFont(None,55)
text = font.render("Score: "+str(score),True,white)
screen.blit(text, [0,0])
x = 350
y = 250
x_speed = 0
y_speed = 0
ground = 477
xloc = 700
yloc = 0
xsize = 70
ysize = randint(0,350)
#Size of space between 2 blocks
space = 150
obspeed = 2
score = 0
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
y_speed = -5
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP:
y_speed = 4
screen.fill(black)
obstacle(xloc,yloc,xsize,ysize)
ball(x,y)
Score(score)
y += y_speed
xloc -= obspeed
if y > ground:
gameover()
y_speed = 0
obspeed = 0
if x+20 > xloc and y-20 < ysize and x-15 < xsize+xloc:
gameover()
y_speed = 0
obspeed = 0
if x+20 > xloc and y+20 < ysize and x-15 < xsize+xloc:
gameover()
y_speed = 0
obspeed = 0
if xloc < -80:
xloc = 700
ysize = randint(0,350)
if x > xloc and x < xloc+3:
score = score + 1
pygame.display.flip()
clock.tick(60)
pygame.quit()
您应该只加载一次图像并重新缩放到窗口高度-并将其大小和位置设置为
pygame.Rect()
(使用get_rect())
)然后可以为每个管道创建
pygame.Rect()
如果间隙的大小为200,则顶部应为300像素
然后确定管道的位置
你会把它当作
从右向左移动
示例代码
编辑:我补充道:缝隙在随机位置,管道在移动,当球碰到管道时,它在控制台/终端上打印“游戏结束”,当管道碰到屏幕左侧时,它在控制台/终端上打印“赢”
如果我理解您的变量名,可以在函数的第四行中尝试:
相关问题 更多 >
编程相关推荐