赞
踩
坏境是vscode 1.43.0 python版本是3.7.6
import pygame
from pygame.locals import *
import sys
def hello_world():
pygame.init()
pygame.display.set_mode((640,480))
pygame.display.set_caption("hello world")
while True:
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
if __name__=="__main__":
hello_world()
运行后
代码
import pygame,sys,time,random from pygame.locals import * pygame.init() fpsClock=pygame.time.Clock() playSurface=pygame.display.set_mode((640,480)) pygame.display.set_caption("贪吃蛇游戏") redColor=pygame.Color(255,0,0) blackColor=pygame.Color(0,0,0) whiteColor=pygame.Color(255,255,255) greyColor=pygame.Color(150,150,150) snakePosition=[100,100] snakeSegments=[[100,100],[80,100],[60,100]] raspberryPosition=[300,300] raspberrySpawned=1 direction='right' changeDirection=direction def gameOver(): gameOverFont=pygame.font.Font('simfang.ttf',72) gameOverSurf=gameOverFont.render('Game Over',True,greyColor) gameOverRect=gameOverSurf.get_rect() gameOverRect.midtop=(320,10) playSurface.blit(gameOverSurf,gameOverRect) pygame.display.flip() time.sleep(5) pygame.quit() sys.exit() while True: for event in pygame.event.get(): if event.type==QUIT: pygame.quit() sys.exit() elif event.type==KEYDOWN: if event.key==K_RIGHT or event.key==ord('d'): changeDirection='right' if event.key==K_LEFT or event.key==ord('a'): changeDirection='left' if event.key==K_UP or event.key==ord('w'): changeDirection='up' if event.key==K_DOLLAR or event.key==ord('s'): changeDirection='down'
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。