阻止对象在pygame中的故障
我正在尝试使用Pygame这样的游戏,我目前有两个矩形,但是一个故障,我不知道为什么,我评论了 win.fill(((0,0,0,0))
,它停止了一个在毛刺中的矩形中,但另一个不是我所拥有的:
# import pygame module in this program
import pygame
# activate the pygame library .
# initiate pygame and give permission
# to use pygame's functionality.
pygame.init()
# create the display surface object
# of specific dimension..e(500, 500).
win = pygame.display.set_mode((1280, 720))
# set the pygame window name
pygame.display.set_caption("Pong")
# object1 current co-ordinates
x = 15
y = 280
# object2 current co-ordinates
x1 = 1245
y1 = 280
# dimensions of the object1
width = 20
height = 130
# dimensions of the object2
width1 = 20
height1 = 130
# velocity / speed of movement
vel = 10
# Indicates pygame is running
run = True
# infinite loop
while run:
# creates time delay of 10ms
pygame.time.delay(0)
# iterate over the list of Event objects
# that was returned by pygame.event.get() method.
for event in pygame.event.get():
# if event object type is QUIT
# then quitting the pygame
# and program both.
if event.type == pygame.QUIT:
# it will make exit the while loop
run = False
# stores keys pressed
keys = pygame.key.get_pressed()
# if left arrow key is pressed
if keys[pygame.K_w] and y > 0:
# decrement in y co-ordinate
y -= vel
# if left arrow key is pressed
if keys[pygame.K_s] and y < 720-height:
# increment in y co-ordinate
y += vel
# completely fill the surface object
# with black colour
win.fill((0, 0, 0))
# drawing object on screen which is rectangle here
pygame.draw.rect(win, (0, 0, 255), (x, y, width, height))
# it refreshes the window
pygame.display.update()
keys2 = pygame.key.get_pressed()
# if left arrow key is pressed
if keys2[pygame.K_UP] and y1 > 0:
# decrement in y co-ordinate
y1 -= vel
# if left arrow key is pressed
if keys2[pygame.K_DOWN] and y1 < 720-height:
# increment in y co-ordinate
y1 += vel
# completely fill the surface object
# with black colour
win.fill((0, 0, 0))
# drawing object on screen which is rectangle here
pygame.draw.rect(win, (255, 255, 255), (x1, y1, width1, height1))
# it refreshes the window
pygame.display.update()
# closes the pygame window
pygame.quit()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
调用
pygame.display.update()
在应用程序循环结束时仅一次。多个调用pygame.display.update()
或pygame.display.flip()
引起闪烁。参见为什么pygame动画是闪烁的。也只清除显示一次。实际上,您实际上是在 对象。如果您借鉴了与PyGame显示器关联的 surface ,则在显示屏中不立即可见。更改将可见,当显示显示使用 pygame.display.update() 或
pygame.display.flip()
。如果清除显示屏,则所有先前绘制的元素都变得不可见。因此,在框架开始时清除显示器一次,然后在框架末端进行一次更新。
典型的pygame应用程序循环必须:
pygame.time.clock.tick
pygame.event.pump()
或pygame.event.get()
。BLIT
所有对象)Call
pygame.display.update()
only once at the end of the application loop. Multiple calls topygame.display.update()
orpygame.display.flip()
cause flickering. See Why is the PyGame animation is flickering. Also clear the display only once per frame.You are actually drawing on a
Surface
object. If you draw on the Surface associated to the PyGame display, this is not immediately visible in the display. The changes become visible, when the display is updated with eitherpygame.display.update()
orpygame.display.flip()
.If you clear the display, all previously drawn elements become invisible. Therefore, clear the display once at the beginning of the frame and update it once at the end of the frame.
The typical PyGame application loop has to:
pygame.time.Clock.tick
pygame.event.pump()
orpygame.event.get()
.blit
all the objects)pygame.display.update()
orpygame.display.flip()