在這個程序中,玩家使用鍵盤上的箭頭按鍵在屏幕上移動一個黑色方塊,綠色的方塊代表食物,他也出現在屏幕上,,并且黑色方塊碰到他們時會吃掉它們。玩家可以在窗口的任何地方點擊以創建新的食物方塊,按下ESC鍵會退出程序,按下X鍵會將玩家移到隨機位置。
注意需要import pygame,所以運行前要先pip install pygame,不然會出現一個錯誤:
Traceback (most recent call last):
File "C:\Users\Administrator\Desktop\Python\Python的kidscode.cn示例\碰撞檢測.py", line 1, in
import pygame, sys, random
ModuleNotFoundError: No module named 'pygame'
上程序:
import pygame, sys, random from pygame.locals import * # Set up pygame. pygame.init() mainClock = pygame.time.Clock() # Set up the window. WINDOWWIDTH = 400 WINDOWHEIGHT = 400 windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32) pygame.display.set_caption('Input') # Set up the colors. BLACK = (0, 0, 0) GREEN = (0, 255, 0) WHITE = (255, 255, 255) # Set up the player and food data structure. foodCounter = 0 NEWFOOD = 40 FOODSIZE = 20 player = pygame.Rect(300, 100, 50, 50) foods = [] for i in range(20): foods.append(pygame.Rect(random.randint(0, WINDOWWIDTH - FOODSIZE), random.randint(0, WINDOWHEIGHT - FOODSIZE), FOODSIZE, FOODSIZE)) # Set up movement variables. moveLeft = False moveRight = False moveUp = False moveDown = False MOVESPEED = 6 # Run the game loop. while True: # Check for events. for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() if event.type == KEYDOWN: # Change the keyboard variables. if event.key == K_LEFT or event.key == K_a: moveRight = False moveLeft = True if event.key == K_RIGHT or event.key == K_d: moveLeft = False moveRight = True if event.key == K_UP or event.key == K_w: moveDown = False moveUp = True if event.key == K_DOWN or event.key == K_s: moveUp = False moveDown = True if event.type == KEYUP: if event.key == K_ESCAPE: pygame.quit() sys.exit() if event.key == K_LEFT or event.key == K_a: moveLeft = False if event.key == K_RIGHT or event.key == K_d: moveRight = False if event.key == K_UP or event.key == K_w: moveUp = False if event.key == K_DOWN or event.key == K_s: moveDown = False if event.key == K_x: player.top = random.randint(0, WINDOWHEIGHT - player.height) player.left = random.randint(0, WINDOWWIDTH - player.width) if event.type == MOUSEBUTTONUP: foods.append(pygame.Rect(event.pos[0], event.pos[1], FOODSIZE, FOODSIZE)) foodCounter += 1 if foodCounter >= NEWFOOD: # Add new food. foodCounter = 0 foods.append(pygame.Rect(random.randint(0, WINDOWWIDTH - FOODSIZE), random.randint(0, WINDOWHEIGHT - FOODSIZE), FOODSIZE, FOODSIZE)) # Draw the white background onto the surface. windowSurface.fill(WHITE) # Move the player. if moveDown and player.bottom < WINDOWHEIGHT: player.top += MOVESPEED if moveUp and player.top > 0: player.top -= MOVESPEED if moveLeft and player.left > 0: player.left -= MOVESPEED if moveRight and player.right < WINDOWWIDTH: player.right += MOVESPEED # Draw the player onto the surface. pygame.draw.rect(windowSurface, BLACK, player) # Check if the player has intersected with any food squares. for food in foods[:]: if player.colliderect(food): foods.remove(food) # Draw the food. for i in range(len(foods)): pygame.draw.rect(windowSurface, GREEN, foods[i]) # Draw the window onto the screen. pygame.display.update() mainClock.tick(40 )
別的版本我沒有試過,應該不行
本站作者已申明原創,禁止轉載!
文章內容屬作者個人觀點,不代表本站立場,如有侵權立刪。