我的游戏屏幕有问题。
我已经创建了一些屏幕,但是我的“说明”屏幕和“重新启动”屏幕有问题。
在我的主菜单中有3个按钮,分别指向“指令”屏幕、“播放”和“退出”屏幕。
“播放”和“退出”屏幕都能正常工作。
在我的“说明”屏幕中,我有一些文本,只有“播放”按钮。
当我按下主菜单中的“播放”键,我就死了,它会导致“重新启动”屏幕,在那里我又有了“播放”按钮,所以我可以播放。
但是如果我从我的主菜单转到我的“指令”屏幕,然后转到“播放”按钮,我可以再次玩游戏,但是当我死的时候,它会把我带到我的“指令”屏幕,但是我希望它会导致“重启”屏幕。
代码如下:
import pygame
import sys
pygame.init()
pygame.display.set_caption("My Game")
screen_width, screen_height = 1200, 600
screen = pygame.display.set_mode((screen_width, screen_height))
clock = pygame.time.Clock()
BLUE = pygame.Color('dodgerblue3')
ORANGE = pygame.Color('sienna3')
BLACK = (0, 0, 0)
WHITE = (255,255,255)
RED = (255, 0, 0)
GREEN = (13, 255, 0)
DARK_GREEN = (0, 225, 0)
BRIGHT_GREEN = (0, 255, 0)
YELLOW = (255, 255, 0)
font = pygame.font.Font(None, 25)
frame_rate = 60
image1 = pygame.image.load("/Users/xy/Desktop/menupy2.png")
class Walls(pygame.Rect):
def __init__(self, x, y, w, h):
super().__init__(x, y, w, h)
class LeftRedRect(pygame.Rect):
def __init__(self, x, y, w, h, vel):
# Calling the __init__ method of the parent class
super().__init__(x, y, w, h)
self.vel = vel
def update(self):
self.x += self.vel # Moving
if self.right > 600 or self.left < 320: # If it's not in this area
self.vel = -self.vel # Inverting the direction
class RightRedRect(pygame.Rect):
def __init__(self, x, y, w, h, vel):
super().__init__(x, y, w, h)
self.vel = vel
def update(self):
self.x += self.vel
if self.right > 1180 or self.left < 620:
self.vel = -self.vel
class UpAndDownRedRect(pygame.Rect):
def __init__(self, x, y, w, h, vel):
super().__init__(x, y, w, h)
self.vel = vel
def update(self):
self.y += self.vel
if self.top < 20 or self.bottom > 535:
self.vel = -self.vel
def quit_game():
pygame.quit()
sys.exit()
def message_display(text):
largeText = pygame.font.Font(None, 115)
screen.blit(largeText.render(text, True, BLUE), (370, 250))
pygame.display.update()
pygame.time.wait(1000)
def text_objects(text, font):
textSurface = font.render(text, True, BLACK)
return textSurface, textSurface.get_rect()
def button(msg, x, y, w, h, ic, ac, action = None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if x + w > mouse[0] > x and y + h > mouse[1] > y:
pygame.draw.rect(screen, ac, (x, y, w, h))
if click[0] == 1 and action is not None:
action()
else:
pygame.draw.rect(screen, ic, (x, y, w, h))
smallText = pygame.font.Font("freesansbold.ttf",35)
textSurf, textRect = text_objects(msg, smallText)
textRect.center = ((x+(w/2)), (y+(h/2)))
screen.blit(textSurf, textRect)
def restart():
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit_game()
screen.fill(WHITE)
largeText = pygame.font.Font(None, 115)
screen.blit(largeText.render("You lost", True, BLUE), (445, 75))
button("Restart", 525, 250, 150, 60, DARK_GREEN, BRIGHT_GREEN, menu)
button("Quit", 525, 350, 150, 60, DARK_GREEN, BRIGHT_GREEN, quit_game)
pygame.display.flip()
clock.tick(60)
def victory_screen():
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit_game()
screen.fill(WHITE)
largeText = pygame.font.Font(None, 115)
screen.blit(largeText.render("Congratulations!", True, BLUE), (270, 80))
largeText = pygame.font.Font(None, 60)
screen.blit(largeText.render("You beat the game!", True, BLUE), (410, 180))
button("Restart", 525, 300, 150, 60, DARK_GREEN, BRIGHT_GREEN, menu)
button("Quit", 525, 400, 150, 60, DARK_GREEN, BRIGHT_GREEN, quit_game)
pygame.display.update()
pygame.display.flip()
clock.tick(frame_rate)
def instructions_screen():
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit_game()
screen.fill(WHITE)
largeText = pygame.font.Font(None, 115)
smallText = pygame.font.Font(None, 60)
screen.blit(largeText.render("Instructions", True, BLUE), (362, 50))
screen.blit(smallText.render("Goal of the game: Reach the yellow rectangle", True, BLACK), (148, 150))
screen.blit(smallText.render("How to move: Upper arrow - up", True, BLACK), (148, 210))
screen.blit(smallText.render("Lower arrow - down", True, BLACK), (429, 250))
screen.blit(smallText.render("Left arrow - left", True, BLACK), (429, 290))
screen.blit(smallText.render("Right arrow - right", True, BLACK), (429, 330))
button("Play", 525, 430, 150, 60, DARK_GREEN, BRIGHT_GREEN, menu)
pygame.display.flip()
clock.tick(60)
def front_page():
next_scene = None
def start_game():
nonlocal next_scene
next_scene = menu
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit_game()
if next_scene is not None:
return next_scene
screen.fill(WHITE)
screen.blit(image1, (0,0))
largeText = pygame.font.Font(None, 115)
screen.blit(largeText.render("My Game", True, BLUE), (430, 50))
button("Play", 525, 200, 150, 60, DARK_GREEN, BRIGHT_GREEN, start_game)
button("Quit", 525, 400, 150, 60, DARK_GREEN, BRIGHT_GREEN, quit_game)
button("Info", 525, 300, 150, 60, DARK_GREEN, BRIGHT_GREEN, instructions_screen)
pygame.display.flip()
clock.tick(frame_rate)
def menu():
vel = 4
vel_left = 5
vel_right = -5
vel_up = 7
player = pygame.Rect(40, 45, 30, 30)
finish_line = pygame.Rect(620, 535, 560, 45)
walls = [
Walls(0, 0, 1200, 20), Walls(0, 0, 20, 600),
Walls(0, 580, 1200, 20), Walls(1180, 0, 20, 600),
Walls(300, 0, 20, 530), Walls(20, 100, 230, 20),
Walls(70, 200, 230, 20), Walls(20, 300, 230, 20),
Walls(70, 400, 230, 20), Walls(600, 100, 20, 500)
]
leftredrects = [
LeftRedRect(320, 120, 30, 30, vel_left),
LeftRedRect(320, 240, 30, 30, vel_left),
LeftRedRect(320, 360, 30, 30, vel_left),
LeftRedRect(570, 180, 30, 30, vel_right),
LeftRedRect(570, 300, 30, 30, vel_right),
LeftRedRect(570, 420, 30, 30, vel_right)
]
rightredrects = [
RightRedRect(1140, 120, 30, 30, vel_left),
RightRedRect(1140, 240, 30, 30, vel_left),
RightRedRect(1140, 360, 30, 30, vel_left),
RightRedRect(620, 180, 30, 30, vel_right),
RightRedRect(620, 300, 30, 30, vel_right),
RightRedRect(620, 420, 30, 30, vel_right),
]
upanddownredrects = [
UpAndDownRedRect(620, 20, 30, 30, vel_up),
UpAndDownRedRect(752, 505, 30, 30, vel_up),
UpAndDownRedRect(885, 20, 30, 30, vel_up),
UpAndDownRedRect(1016, 505, 30, 30, vel_up),
UpAndDownRedRect(1150, 20, 30, 30, vel_up)
]
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit_game()
keys = pygame.key.get_pressed()
# Player coordinates
if keys[pygame.K_LEFT] and player.x > 0:
player.x -= vel
if keys[pygame.K_RIGHT] and player.x < 1200 - player.width:
player.x += vel
if keys[pygame.K_UP] and player.y > 0:
player.y -= vel
if keys[pygame.K_DOWN] and player.y < 600 - player.height:
player.y += vel
# Game logic
for wall in walls:
# Check if the player rectangle collides with a wall rectangle
if player.colliderect(wall):
print("Game over")
message_display("Game Over")
return restart
for rect in rightredrects:
rect.update() # Movement and bounds checking
if player.colliderect(rect):
print("Game over")
message_display("Game Over")
return restart
for rect in leftredrects:
rect.update()
if player.colliderect(rect):
print("Game over")
message_display("Game Over")
return restart
for rect in upanddownredrects:
rect.update()
if player.colliderect(rect):
print("Game over")
message_display("Game Over")
return restart
if player.colliderect(finish_line):
print("You beat the game")
victory_screen()
# Drawing everything
screen.fill(WHITE)
pygame.draw.rect(screen, YELLOW, finish_line)
for wall in walls:
pygame.draw.rect(screen, BLACK, wall)
for rect in rightredrects:
pygame.draw.rect(screen, RED, rect)
for rect in leftredrects:
pygame.draw.rect(screen, RED, rect)
for rect in upanddownredrects:
pygame.draw.rect(screen, RED, rect)
pygame.draw.rect(screen, GREEN, player)
pygame.display.flip()
clock.tick(60)
def main():
scene = front_page # Set the current scene.
while scene is not None:
# Execute the current scene function. When it's done
# it returns either the next scene or None which we
# assign to the scene variable.
scene = scene()
main()
pygame.quit()
您必须在另一个回调函数的帮助下,从首页
函数返回说明屏幕
函数。
def front_page():
next_scene = None
def start_game():
nonlocal next_scene
next_scene = menu
def show_instructions_screen():
nonlocal next_scene
next_scene = instructions_screen
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit_game()
if next_scene is not None:
return next_scene
screen.fill(WHITE)
screen.blit(image1, (0,0))
largeText = pygame.font.Font(None, 115)
screen.blit(largeText.render("My Game", True, BLUE), (430, 50))
button("Play", 525, 200, 150, 60, DARK_GREEN, BRIGHT_GREEN, start_game)
button("Quit", 525, 400, 150, 60, DARK_GREEN, BRIGHT_GREEN, quit_game)
button("Info", 525, 300, 150, 60, DARK_GREEN, BRIGHT_GREEN, show_instructions_screen)
pygame.display.flip()
clock.tick(frame_rate)
你应该在其他函数中做同样的事情。定义回调函数,并返回next_scene
,如果它是而不是No
。通过将下一个场景函数返回到main
函数来切换场景,而不是直接调用它。
我显示记分板的代码总是闪烁,因为我将背景设置为白色。但我想让这一切停止。那么,有没有一种方法可以让pygame在不更新显示的情况下完成所有这些绘图和填充,并且当绘图完成时,它会显示新的结果,这样我就不必看到每一步,从而不必闪烁?
我运行以下代码: 当我运行代码时,pygame窗口打开,但它是一个空白(黑色)屏幕。我还收到以下错误消息:Traceback(最近一次呼叫last): 文件"C:/用户/Draco/OneDrive/文档/编程/graphics.py",第13行,screen.blit(img(0,0))TypeError:'pyplay.Surface'对象不可调用 我试图打开的图像保存为JPG文件。图像保存在
我有一个问题,找到一种方法来在正确的时间烧毁游戏结束屏幕。我做了一个平台上pyplay和我想游戏在屏幕上出现后,我的角色死亡的动画已经发生,当敌人有他的攻击动画。现在它绕过任何动画,一旦精灵之间的冲突检测为真,它就说游戏结束。有什么想法吗? 谢谢
我的有问题,当我在手机上启动应用程序时,它会显示约0.5秒的白色屏幕。扩展了,在中,我声明为启动器,肖像模式为。 代码: XML:
我试图得到一个树莓派3B显示来自3个摄像头的实时流,使用屏幕来获得omxPlayer的多个实例,以运行在我想要的位置上。这完全符合预期。 现在的问题是,我想要一个静态图像在屏幕上的最后一个空间。为此,我想使用pyplay,这样我以后可以在这个领域有更多的乐趣,也许不仅仅是一个图像。 现在的问题是如何定位图像: https://imgur.com/mUQ38vV(图像大小与视频提要相同,我原以为它位
我一直在我的5.2英寸设备(LG G2)上开发和测试我的应用程序。 我刚刚在一个更大的5.5英寸设备(OnePlus 3T)上启动了该应用程序,它看起来不太好,如下所示: 这是主要活动: 这是子项的布局xml: 我在这里做错了什么? 编辑: 我搜索了一点,找到了一个解决方案,使用以下代码动态设置GridView的最小高度: 此外,我在适配器中添加了以下代码行: 在我的例子中,我必须将屏幕高度除以2