所以我试图显示一个图像,等待 1 秒,然后显示另一个图像。我正在制作一款匹配游戏,因此在我的代码中我试图说,如果两个图像不匹配,我想将图像更改为预设的通用图像。所以我的代码看起来像这样:
if True:
self.content = self.image
time.sleep(1)
self.content = Tile.cover
Run Code Online (Sandbox Code Playgroud)
self.content是所显示内容的变量,self.image是显示的图像,然后Tile.cover是覆盖另一图像的通用图像。然而,每当我这样做时,代码都会跳过第一行,只是将图像设置为Tile.cover,为什么?
我正在制作乒乓球游戏。当任何一个分数达到 10 时,它应该在屏幕上放一些文字,并说右边的玩家赢了或左边的玩家赢了。但是,在我的程序中,它不起作用。当它必须显示左右玩家获胜的文本时,它不会显示。但它适用于其他一切。这是代码:
# Importing libraries
import pygame
import random
import time
# Initializing PyGame
pygame.init()
# Setting a window name
pygame.display.set_caption("Ping Pong")
# Creating a font
pygame.font.init()
font = pygame.font.SysFont(None, 30)
pong_font = pygame.font.SysFont("comicsansms", 75)
# Set the height and width of the screen
window_width = 700
window_height = 500
size = [window_width, window_height]
game_win = pygame.display.set_mode(size)
game_win2 = pygame.display.set_mode(size)
# Creating a messaging system
def message(sentence, color, x, y, font_type, display):
sentence = font_type.render(sentence, True, color)
display.blit(sentence, [x, …Run Code Online (Sandbox Code Playgroud) 我正在使用pygame进行街机游戏,我试图每隔几秒就有一次精灵变换位置.
我已经尝试使用time.sleep(1)并将帧速率更改为.5(clock.tick(.5)).
两者都只是在时间间隔过去之后才能使对象改变位置,但是它们也使得鼠标跟随我的鼠标更新坐标的速率相同.
我一直在研究,并且似乎找不到另一种方法来使精灵移动而不会让我的程序刷新更慢或每次运行时"睡眠".
import pygame
pygame.init()
red = 255,0,0
blue = 0,0,255
black = 0,0,0
screenWidth = 800
screenHeight = 600
gameDisplay = pygame.display.set_mode((screenWidth,screenHeight)) ## screen width and height
pygame.display.set_caption('JUST SOME BLOCKS') ## set my title of the window
clock = pygame.time.Clock()
class player(): ## has all of my attributes for player 1
def __init__(self,x,y,width,height):
self.x = x
self.y = y
self.height = height
self.width = width
self.vel = 5
self.left = False
self.right = False
self.up = False
self.down = False
class …Run Code Online (Sandbox Code Playgroud)