视频系统未初始化,但我同时使用了pygame.init()和sys.exit()


import pygame
import sys
pygame.init()
window = pygame.display.set_mode([600,600])
playing = True
blue = True
x = 30 # Initial
y = 30 # Position
while playing:
for event in pygame.event.get():
if event.type == pygame.quit():
playing = False
#The above 4 lines are basically a exit function for the program that you can use in pretty much any pygame program to allow the user to exit the program
# The below is anything else you want to happen constantly while the program is running
if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:  # This program says that if a key is pressed down and that event is K_SPACE (K stands for keyboard and
#space is basically the key on the keyboard) then the following underneath happens
blue = not blue # makes blue false, you can also use blue == False which would be a much better way but i'm keeping this this way in case it helps with another
#program in the future
pressed = pygame.key.get_pressed() # this gets what key has been pressed so key.get_pressed() get's the key that has been pressed
if pressed[pygame.K_UP]: y += 3
if pressed[pygame.K_DOWN]: y -= 3
if pressed[pygame.K_LEFT]: x -= 3
if pressed[pygame.K_RIGHT]: x += 3

#these function change the y and x values according to the keys pressed
if blue:
colour = (0,128,255) #This changes the colour of the rectangle depending on whether or not blue is true or false as dictated by the space key in the code before the up down mechanism
else:
colour = (255,100,0)
pygame.draw.rect(window,colour, pygame.Rect(x,y),60,60) # this makes it draw a rectangle in the window, of the colour specified and makes the rectangle start at the x and y coordinates and makes the rectangle unable to get any bigger than 60/60 pixels
pygame.display.flip()

sys.exit()

我不知道我做错了什么,任何帮助都将不胜感激-谢谢!

ps。我正在学习pygame,所以所有的笔记都是为了我的学习过程,忽略它们。

您所犯的错误是意外地调用pygame.quit(),而不是在事件循环中重新定义pygame.QUIT协调者。基本上改变这一点:;CCD_ 3";对此:if event.type == pygame.QUIT

最新更新