91av视频/亚洲h视频/操亚洲美女/外国一级黄色毛片 - 国产三级三级三级三级

資源簡介

pygame實現貪吃蛇

資源截圖

代碼片段和文件信息

import?pygame
from?pygame?import?locals
import?random

#?初始化pygame,為使用硬件做準備
pygame.init()
FPSCLOCK?=?pygame.time.Clock()??#?pygame時鐘,控制游戲速度(幀數)
#?創建一個窗口
screen?=?pygame.display.set_mode((660?480)?0?32)
#?背景
background?=?pygame.image.load(‘img/bg.png‘)
right?=?pygame.image.load(‘img/right.png‘)
food?=?pygame.image.load(‘img/apple.png‘)
body?=?pygame.image.load(‘img/body.png‘)
left?=?pygame.image.load(‘img/left.png‘)
up?=?pygame.image.load(‘img/up.png‘)
down?=?pygame.image.load(‘img/down.png‘)
basic_font?=?pygame.font.Font(‘img/neuropol.ttf‘?18)??#?字體

#?貪吃蛇的初始坐標
x?y?=?240?120
position?=?[(180?90)?(180?120)?(210?120)?(x?y)]
apple_x?=?360???#?蘋果橫坐標
apple_y?=?300???#?蘋果縱坐標

#?貪吃蛇的朝向
setheading?=?“right“
snake_head?=?right
#?分數初始值
score?=?0

while?True:
????for?event?in?pygame.event.get():
????????if?event.type?==?locals.QUIT:
????????????#?接收到退出事件后退出程序
????????????exit()
????????elif?event.type?==?locals.KEYDOWN:
????????????if?event.key?==?locals.K_RIGHT?and?setheading?!=?“left“:
????????????????setheading?=?‘right‘
????????????????snake_head?=?right
????????????elif?event.key?==?locals.K_LEFT?and?setheading?!=?“right“:
????????????????setheading?=?‘left‘
????????????????snake_head?=?left
????????????elif?event.key?==?locals.K_UP?and?setheading?!=?“down“:
????????????????setheading?=?‘up‘
????????????????snake_head?=?up
????????????elif?event.key?==?locals.K_DOWN?and?setheading?!=?“up“:
????????????????setheading?=?‘down‘
????????????????snake_head?=?down

????#?設置貪吃蛇的頭部坐標
????if?setheading?==?“right“:
????????x?+=?30???#
????elif?setheading?==?“left“:
????????x?-=?30
????elif?setheading?==?“up“:
????????y?-=?30
????else:
????????y?+=?30
????position.append((x?y))
????if?x?==?apple_x?and?y?==?apple_y:
????????#?隨機生成一個格子的左上角坐標作為蘋果(食物)的坐標
????????num1?=?random.randint(1?22)
????????num2?=?random.randint(1?16)
????????apple_x?=?30?*?num1?-?30??#?蘋果的x坐標
????????apple_y?=?30?*?num2?-?30??#?蘋果的y坐標
????????score?+=?10
????else:
????????position.pop(0)
????if?x??630?or?y??450:
????????exit()
????#?將背景圖畫上去
????screen.blit(background?(0?0))
????#?將貪吃蛇的頭畫上去
????screen.blit(snake_head?position[-1])??#
????#?將貪吃蛇的身體畫上去
????for?i?in?range(len(position)-1):
????????screen.blit(body?position[i])
????#?將果實畫上去
????screen.blit(food?(apple_x?apple_y))
????#?繪制分數
????scoreSurf?=?basic_font.render(‘Score:?‘+str(score)?True?(0?0?0))
????scoreRect?=?scoreSurf.get_rect()
????scoreRect.topleft?=?(660?-?120?10)
????screen.blit(scoreSurf?scoreRect)
????#?刷新畫面
????pygame.display.update()
????FPSCLOCK.tick(3)

?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2020-11-28?20:04??snake\
?????目錄???????????0??2020-11-28?20:04??snake\img\
?????文件????????2150??2020-11-28?20:01??snake\img\apple.png
?????文件???????23029??2020-07-13?15:03??snake\img\bg.png
?????文件????????2019??2020-11-28?20:02??snake\img\body.png
?????文件????????3009??2020-11-28?20:02??snake\img\down.png
?????文件????????3026??2020-11-28?20:02??snake\img\left.png
?????文件??????149752??2018-06-20?09:23??snake\img\neuropol.ttf
?????文件????????3032??2020-11-28?20:02??snake\img\right.png
?????文件????????3020??2020-11-28?20:03??snake\img\up.png
?????文件????????2978??2021-01-21?14:35??snake\snake_pygame.py

評論

共有 條評論