- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在用 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())
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
示例代码
编辑: 我补充说:间隙处于随机位置,管道在移动,当球接触管道时,它会在控制台/终端中打印“游戏结束”,当管道接触屏幕左侧时,它会打印在控制台/终端打印“WIN”
import pygame
import random
# --- constants --- (UPPER_CASE_NAMES)
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
FPS = 60
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 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, (50, 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,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()
关于python - 如何调整pygame中的图像大小以到达屏幕的顶部/底部?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59149195/
Surface.blit在 1.8 中有一个新参数:混合。定义了以下值: BLEND_ADD BLEND_SUB BLEND_MULT BLEND_MIN BLEND_MAX BLEND_RGBA_A
import sys import pygame import pygame.locals as pgl class Test: def __init__(self): pyg
我对 PyGame 比较陌生。我正在尝试制作一个简单的程序来显示表示鼠标在屏幕上的位置的字符串。 import pygame, sys from pygame.locals import * pyga
我有总是在后台运行的音乐和一些在触发时会播放声音的事件。音乐效果很好。 pygame.mixer.music.load(os.path.join(SOUND_FOLDER, 'WateryGrave.
我有这些代码 FONT = pygame.font.Font("font/calibri.ttf", 50) FONT.size = 25 但是编译器说 AttributeError: 'pygame
有我正在导入的图像: look_1 = pygame.image.load('data\\png\\look1.png').convert_alpha() 我试图减少它的大小是这样的: pygame.
我正在为我的 pygame 制作一个帮助屏幕,每当我运行它时,我都会收到此错误消息: > self.surface.blit(self.helpscreen) TypeError: argument
在 pyGame 中应用程序,我想渲染 SVG 中描述的无分辨率 GUI 小部件。 我怎样才能做到这一点? (我喜欢 OCEMP GUI 工具包,但它的渲染似乎依赖于位图) 最佳答案 这是一个完整的例
有没有办法将多首歌曲加载到 Pygame 中?我不是在谈论这样的音效; crash_sound = pygame.mixer.Sound("crash.ogg") #and pygame.mixer.
我还有一个问题。当我尝试运行我的代码时,pygame 启动然后立即停止。 这是我的代码: import pygame import os import time import random pygam
我正在使用 pymunk 和 pygame 开发一个项目。我正在使用 PivotJoint 约束将我的 body 连接在一起。如果可能的话,我想让关节不可见 - 有什么办法可以做到这一点吗?现在关节在
我使用 fedora 20、Python 2.7 和 virtualenv 1.10.1。我想在 virtualenv 中安装 pygame,我得到了 You are installing a pot
尝试将文本添加到矩形中并使用箭头键在屏幕上移动矩形。我想让文字不会超出边缘。到目前为止,我已经在没有将其放入 Rect 中的情况下工作了,但我想让 Rect 函数工作。现在文本只是反弹回来,我不知道要
我是第一次玩 pygame(总的来说我是 python 的新手),想知道是否有人可以帮助我... 我正在制作一款小型射击游戏,希望能够为坏人创建一个类。我的想法是类应该继承自 pygame.Surfa
我在 Windows 10 机器上运行了 python 3.9.1。我通过 pip 在我的机器上安装了 pygame 2.0.1 (python -m pip install https://gith
错误: File "/home/alien/cncell/core/animator.py", line 413, in create_animation_from_data pygame
这是代码。 5000 个弹跳旋转的红色方块。 (16x16 png) 在 pygame 版本上,我获得 30 fps,但使用 pyglet 获得 10 fps。对于这种事情,OpenGl 不应该更快吗
我以为 pygame.font.Font 是用来加载 .ttf 字体的,如果没有 .ttf 文件在同一个目录下就无法加载字体,但我看过一个视频,有人在没有 .ttf 文件的情况下加载字体。 ttf 字
我正在尝试使用 Travis CI 设置一个项目。项目也使用 pygame。我曾多次尝试设置它 - 但它似乎失败了。 我得到的最接近的是以下内容: .travis.yml : language: py
这个问题已经有答案了: Python error "ImportError: No module named" (38 个回答) 已关闭 3 年前。 我正在使用 Mac 并输入 pip install
我是一名优秀的程序员,十分优秀!