用户:giorgioXokQ查看:0 回复:1 评论:0 创建时间:2020-04-15T15:30:34
展示:
介绍:石头剪刀布!
作品源代码:
import pygame
import sys
import random
import time
from pygame.locals import *
class Mora():
def __init__(self):
self.images = {
0:pygame.image.load("shitou.png"),
1:pygame.image.load("jiandao.png"),
2:pygame.image.load("bu.png")
} #0 石头 | 1 剪刀 | 2 布
for each in self.images:
self.images[each] = pygame.transform.scale(self.images[each], (100, 100))
def bijiao(a, b): # a 我方 | b 敌方
if a == 0:
if b == 0:
return 2 #平局
elif b == 1:
return 0 #赢
elif b == 2:
return 1 #输
if a == 1:
if b == 0:
return 1 # 输
elif b == 1:
return 2 #平局
elif b == 2:
return 0 # 赢
if a == 2:
if b == 0:
return 0 #赢
elif b == 1:
return 1 #输
elif b == 2:
return 2 #平局
#主函数
def main():
pygame.init()
bg = [480, 360]
screen = pygame.display.set_mode(bg)
pygame.display.set_caption("石头剪刀布")
clock = pygame.time.Clock()
#猜拳
one = Mora()
one_munber = 0
two = Mora()
two_go = True
two_random = 0
#-----------------
myfont = pygame.font.Font(None,60)
textimages = {
0:myfont.render("you Win",True,(0, 0, 0)),
1:myfont.render("you Lose",True,(0, 0, 0)),
2:myfont.render("Tie",True,(0, 0, 0)),
3:myfont.render("select->press Z to stop",True,(0, 0, 0))}
textmun = 3
#-----------------
while True:
if not two_go:
time.sleep(2)
two_go = True
textmun = 3
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN: #操作
if event.key == K_z: #确定
two_go = False
if event.key == K_UP: #更改手势
one_munber += 1
if one_munber >= 3:
one_munber = 0
if event.key == K_DOWN: #更改手势
one_munber -= 1
if one_munber < 0:
one_munber = 2
if two_go:
two_random = random.randint(0, 2)
else:
textmun = bijiao(one_munber, two_random)
#绘制
screen.fill((255,255,255))
screen.blit(one.images[one_munber], [25, (bg[0] - 100) // 2])
screen.blit(two.images[two_random], [bg[1] - 25, (bg[0] - 100) // 2])
screen.blit(textimages[textmun], (0, 0))
#刷新屏幕
clock.tick(60)
pygame.display.flip()
if __name__ == "__main__":
main()