作者:xieyuhua | 来源:互联网 | 2023-06-24 08:28
我正在pygame中重新制作飘扬的小鸟,但主题是《星球大战》。我已经完成了游戏的美术和常规格式设置,但是现在我需要调整细节。我一直在切换数字,以尝试使光剑完全到达屏幕的顶部和底部,因为此刻有时存在间隙,这些间隙不是可以通过的预定空间。
import pygame
from random import randint
from pygame.locals import *
#Define Colors - RGB
black = (0,0)
white = (255,255,255)
green = (0,0)
red = (255,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))
screen.blit(ballImg2,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,500))
screen.blit(pipe1,[xloc,ysize])
screen.blit(pipe2,int(yloc+ysize+space),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),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,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 gameover()
y_speed = 0
obspeed = 0
if x+20 > xloc and y+20 gameover()
y_speed = 0
obspeed = 0
if xloc <-80:
xloc = 700
ysize = randint(0,350)
if x > xloc and x score = score + 1
pygame.display.flip()
clock.tick(60)
pygame.quit()
如果我能理解您的变量名,也许在功能障碍()的第四行中尝试:
screen.blit(pipe,[xloc,int(yloc+space),xsize,ysize+500])
,
您应该只加载一次图像,然后重新缩放至窗口高度-并将其大小和位置设为pygame.Rect()
(使用get_rect())
image = pygame.image.load("images.png").convert()
image = pygame.transform.scale(image,(50,SCREEN_HEIGHT))
image_rect = image.get_rect()
然后您可以为每个管道创建pygame.Rect()
pipe1_rect = image_rect.copy()
pipe2_rect = image_rect.copy()
如果间隙的大小为200,顶部应该为300像素
gap_size = 200
gap_top = 300
那么管道的位置将是
pipe1_rect.bottom = gap_top
pipe2_rect.top = pipe1_rect.bottom + gap_size
,然后将其显示为
screen.blit(image,pipe1_rect)
screen.blit(image,pipe2_rect)
您将其从右移到左
pipe1_rect.x -= 1
pipe2_rect.x -= 1
示例代码
编辑:我添加了:间隙处于随机位置,管道正在移动,当球接触管道时,它将在控制台/终端上打印“ Game Over”,当管道接触屏幕左侧时,在控制台/终端上显示“ WIN”
import pygame
import random
# --- constants --- (UPPER_CASE_NAMES)
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
FPS = 60
BLACK = (0,0)
WHITE = (255,255,255)
GREEN = (0,0)
# --- classes --- (CamelCaseNames)
# empty
# --- main ---
pygame.init()
screen = pygame.display.set_mode( (SCREEN_WIDTH,SCREEN_HEIGHT) )
screen_rect = screen.get_rect()
image = pygame.image.load("Obrazy/images/paddle.png").convert()
image = pygame.transform.scale(image,SCREEN_HEIGHT))
image_rect = image.get_rect()
pipe1_rect = image_rect.copy()
pipe2_rect = image_rect.copy()
pipe1_rect.right = screen_rect.right # move to right of screen
pipe2_rect.right = screen_rect.right # move to right of screen
gap_size = 200
gap_top = 300
#pipe1_rect.bottom = gap_top
pipe1_rect.bottom = random.randint(50,SCREEN_HEIGHT-gap_size-50)
pipe2_rect.top = pipe1_rect.bottom + gap_size
ball_rect = pygame.Rect((0,100,100))
ball_rect.center = screen_rect.center
ball_speed = 5
# --- mainloop ---
clock = pygame.time.Clock()
running = True
while running:
# --- events ---
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYUP:
if event.key == pygame.K_ESCAPE:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
ball_speed = -3
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP:
ball_speed = 4
# --- changes/moves/updates ---
ball_rect.y += ball_speed
pipe1_rect.x -= 1
pipe2_rect.x -= 1
if pipe1_rect.colliderect(ball_rect) or pipe2_rect.colliderect(ball_rect):
print('Game Over')
pipe1_rect.right = screen_rect.right
pipe2_rect.right = screen_rect.right
pipe1_rect.bottom = random.randint(50,SCREEN_HEIGHT-gap_size-50)
pipe2_rect.top = pipe1_rect.bottom + gap_size
if pipe1_rect.left == 0:
print("WIN")
pipe1_rect.right = screen_rect.right
pipe2_rect.right = screen_rect.right
pipe1_rect.bottom = random.randint(50,SCREEN_HEIGHT-gap_size-50)
pipe2_rect.top = pipe1_rect.bottom + gap_size
# --- draws ---
screen.fill(BLACK)
screen.blit(image,pipe1_rect)
screen.blit(image,pipe2_rect)
pygame.draw.rect(screen,GREEN,ball_rect)
pygame.display.flip()
# --- FPS ---
ms = clock.tick(FPS)
#pygame.display.set_caption('{}ms'.format(ms)) # 40ms for 25FPS,16ms for 60FPS
fps = clock.get_fps()
pygame.display.set_caption('FPS: {}'.format(fps))
# --- end ---
pygame.quit()