如何解决一段时间后,Pygame 徽标又变回来了
我是 pygame 的菜鸟,我遇到了问题。一段时间后,Pygame 徽标会发生变化。我有两个图像。请帮忙!第一张图片是窗口应该是什么样子的,第二张图片是我 20 秒后的窗口。我该怎么办?我从来没有遇到过这个问题,我尝试了很多东西,但我做不到。
import pygame
import random
# initialize idk pygame.init()
# size Width = 800 Height = 600 white = (255,255,255) icon = pygame.image.load('sunflower.png') player_img =
pygame.image.load('cat.png') enemy1_img = pygame.image.load('dog.png')
FPS = 120 Vel = 5
# enemy random coordinates enemy1_X = random.randint(64,736) enemy1_Y = random.randint(50,150)
# screen def screen_f(p_rect,e_rect):
screen = pygame.display.set_mode((800,600))
pygame.display.set_caption('dog destroyer')
icon = pygame.image.load('sunflower.png')
pygame.display.set_icon(icon)
backgroud = pygame.image.load('backg.png')
screen.blit(backgroud,(0,0))
screen.blit(player_img,(p_rect))
screen.blit(enemy1_img,(e_rect))
# player def player_movement(key_pressed,p_rect):
if key_pressed[pygame.K_RIGHT]:
p_rect.x += Vel
if key_pressed[pygame.K_LEFT]:
p_rect.x -= Vel
if key_pressed[pygame.K_UP]:
p_rect.y -= Vel
if key_pressed[pygame.K_DOWN]:
p_rect.y += Vel
def borders(p_rect):
if p_rect.x < 0:
p_rect.x = 0
elif p_rect.x > 735:
p_rect.x = 735
elif p_rect.y < 0:
p_rect.y = 0
elif p_rect.y > 535:
p_rect.y = 535
# main def main():
Vel_e = 4 # enemy velocity
player_rect = pygame.Rect(368,500,64,64)
enemy_rect = pygame.Rect(enemy1_X,enemy1_Y,64)
clock = pygame.time.Clock()
run = True
while run:
clock.tick(FPS)
key_pressed = pygame.key.get_pressed()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
enemy_rect.x += Vel_e
if enemy_rect.x >= 736:
Vel_e = -4
enemy_rect.y += 50
if enemy_rect.x < 0:
Vel_e = 4
enemy_rect.y += 50
borders(player_rect)
player_movement(key_pressed,player_rect)
screen_f(player_rect,enemy_rect)
pygame.display.update()
if __name__ == "__main__":
main()
解决方法
您的代码不清晰,但我想我知道如何修复它。
您通常希望在文件的开头设置屏幕和其他重要内容,而不是在必须调用的 def 中。
试试这样:
import pygame
pygame.init()
width = ...
height = ...
fps = ...
#set any colors
#load all you pictures
screen = pygame.display.set_mode((width,height))
# you would also want to set your caption and icon here so:
pygame.display.set_caption('your caption')
pygame.display.set_icon(icon)
#all your defs
#and when its all set you go to a main loop.
#(at the bottom of your file)
main()
# and quit for if the user quits and run gets set to False
pygame.quit()
quit()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。