import random class Game_2(object): def __init__(self): self.guess_list = ["石头","剪刀","布"] #将赢的组合规则先通过元组进行设定 self.win_combination = [["布","石头"],["石头","剪刀"],["剪刀","布"]] #组合/ˌkɑːmbɪˈneɪʃ(ə)n/ def play(self): while True: computer = random.choice(self.games_list) #计算机随机生成一个手势 people = input("请输入:石头,剪刀,布\n").strip() #定义玩家输入一个手势 if people not in self.guess_list: print("请给出一个正确的手势~~~") continue #如果输入错误则继续输入,/kənˈtɪnjuː/ elif computer == people: print("平手,再玩一次") elif [computer,people] in self.win_combination: print("计算机获胜,游戏继续,直到玩家获胜才能推出!") else: print("玩家获胜!") break #测试代码 if __name__ == '__main__': game = Game_2() game.paly()
时间: 2023-02-27 15:17:15 浏览: 60
这是一个使用 Python 语言定义的游戏类。该类继承自 "object" 类,名为 Game_2。它有一个初始化函数 __init__,它创建了一个列表 self.guess_list,其中包含三个元素:"石头","剪刀",和"布"。
相关问题
import pygame import random import sys class Settings: def __init__(self): self.picture_num = 4 self.screen_width = 408 self.screen_length = 809 self.picture_length = 100 self.screen_bgcol = (96, 127, 255) self.picture_speed = 5 self.picture_bian = 1 self.picture_distance = 102
这是一个使用 pygame 库开发的游戏的设置类。其中包括了游戏的窗口大小、背景颜色、图片数量、图片大小、图片移动速度等等参数。通过实例化该类,可以方便地对游戏进行设置和调整。例如,可以通过修改 picture_speed 参数来控制图片的移动速度,通过修改 screen_bgcol 参数来改变游戏窗口的背景颜色。
import sys import pygame from yaya import YAYA class Sky: def __init__(self): self.screen=pygame.display.set_mode((0,0),pygame.FULLSCREEN) self.bg_color=(0,23,56) self.yaya=YAYA(self) def run_game(self): while True: for event in pygame.event.get(): if event.type==pygame.QUIT: sys.exit() self.screen.fill(self.bg_color) self.yaya.blitme() self.display.flip() if __name__=='__main__': blny=Sky() blny.run_game()
在你的代码中,你有一个`Sky`类,以及一个`run_game`方法。在`run_game`方法中,你调用了`self.display.flip()`。然而,`display`属性实际上不存在于`Sky`类中。相反,你应该使用`pygame.display.flip()`来刷新屏幕。
因此,你的代码应该修改为:
```
import sys
import pygame
from yaya import YAYA
class Sky:
def __init__(self):
self.screen=pygame.display.set_mode((0,0),pygame.FULLSCREEN)
self.bg_color=(0,23,56)
self.yaya=YAYA(self)
def run_game(self):
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
sys.exit()
self.screen.fill(self.bg_color)
self.yaya.blitme()
pygame.display.flip()
if __name__=='__main__':
blny=Sky()
blny.run_game()
```
这应该可以解决你遇到的问题。