1. 程式人生 > >課後作業-團隊編程項目進度

課後作業-團隊編程項目進度

int == -1 itl span key 碼雲 class pla

碼雲:https://gitee.com/songyx/codes/4lagynxvtpoqh6u1dke5852

技術分享

技術分享

技術分享

技術分享

-*- coding: cp936 -*-
#1-導入庫
import pygame
from random import randint
#tkMessageBox 用來彈出對話框
import tkMessageBox
from pygame.locals import *
#2-初始化遊戲
pygame.init()
width,height=480,480
#顯示 樣式 創建屏幕保存到變量中
screen=pygame.display.set_mode((width,height))
#標題
pygame.display.set_caption("jingziqi")
#插入圖片
background=pygame.image.load("bjt.png")
#定義
empty = 0
black=(0,0,0)
red=(255,0,0)
blue=(0,0,255)
white=(255,255,255)
#創建繪制棋盤的函數
def draw_game():
#導入背景圖
screen.blit(background,(0,0))
#畫線
#用法:pygame.draw.line(顯示,顏色,開始位置,結束位置,寬度)
pygame.draw.line(screen, black, (160, 0), (160, 480), 5)
pygame.draw.line(screen, black, (320, 0), (320, 480), 5)
pygame.draw.line(screen, black, (0, 160), (480, 160), 5)
pygame.draw.line(screen, black, (0, 320), (480, 320), 5)
#遍歷列表中的元素及他們的下標 row橫col豎 row col是下標
for row, line in enumerate(state):
for col, val in enumerate(line):
if val == -1:
#畫x
upper_left = (col * 160 + 5, row * 160 + 5)
lower_right = (col * 160 + 155, row * 160 + 155)
pygame.draw.line(screen, red, upper_left, lower_right, 5)

upper_right = (col * 160 + 155, row * 160 + 5)
lower_left = (col * 160 + 5, row * 160 + 155)
pygame.draw.line(screen, red, upper_right, lower_left, 5)
elif val == 1:
#創建一個矩形.在矩形裏畫圓
rect = (col * 160 + 5, row * 160 + 5, 150, 150)
pygame.draw.ellipse(screen, blue, rect, 5)
else:
assert val == empty
continue
pygame.display.flip()
def draw_O():
#隨機函數
while True:
row = randint(0,2)
col = randint(0,2)
#當空格為空的時候畫圓
if state[row][col] == 0:
state[row][col] = 1
break
draw_game()
pygame.display.flip()
def is_won():
for val in range(3):
# 檢查匹配的行三個圖形是否都相同且不等於空

if state[0][val] == state[1][val] == state[2][val] != empty:
return state[0][val]

# 檢查匹配的列三個圖形是否都相同不等於空

if state[val][0] == state[val][1] == state[val][2] != empty:
return state[val][0]

#判斷 \ 中三個圖形是否都相同
if state[0][0] == state[1][1] == state[2][2] != empty:
return state[1][1]
#判斷 / 中三個圖形是否都相同
if state[0][2] == state[1][1] == state[2][0] != empty:
return state[1][1]
#初始化棋盤
def begin():
global state
state = [[empty] * 3,[empty] * 3,[empty] * 3]
draw_game()
#首先初始化 pygame.display.flip()
begin()
#主循環
while True:
event=pygame.event.wait()
#初始化
pos = None
temp = 0
#接收到退出事件後退出程序
if event.type == pygame.QUIT:
pygame.quit()
exit(0)
#加入了按鍵功能
elif event.type == KEYDOWN:
if event.key == K_a:
begin()
draw_game()
pygame.display.flip()
elif event.key == K_s:
pygame.event.post(pygame.event.Event(QUIT))
#接受鼠標點擊事件
elif event.type == MOUSEBUTTONDOWN and event.button == 1:
#event.pos[0]代表x軸坐標 event.pos[1]代表y軸坐標
pos = (event.pos[1]/160, event.pos[0]/160)
row,col=pos
#if pygame.mouse.get_rel()==(0,0):
# continue
#加一個條件讓它只能在空的時候畫x
if state[row][col]==0:
state[row][col] = -1
else:
continue
print pos
draw_game()
draw_O()
#判斷屬性接受返回值
if is_won() == -1:
tkMessageBox.showinfo(title=‘win‘,message=‘win‘)
pygame.quit()
exit(0)
elif is_won() == 1:
tkMessageBox.showinfo(title=‘lose‘,message=‘lose‘)
pygame.quit()
exit(0)

 -*- coding: cp936 -*-
#1-導入庫
import pygame
from random import randint
#tkMessageBox 用來彈出對話框
import tkMessageBox
from pygame.locals import *
#2-初始化遊戲
pygame.init()
width,height=480,480
#顯示 樣式  創建屏幕保存到變量中
screen=pygame.display.set_mode((width,height))
#標題
pygame.display.set_caption("jingziqi")
#插入圖片
background=pygame.image.load("bjt.png") #定義 empty = 0 black=(0,0,0) red=(255,0,0) blue=(0,0,255) white=(255,255,255) #創建繪制棋盤的函數 def draw_game(): #導入背景圖 screen.blit(background,(0,0)) #畫線 #用法:pygame.draw.line(顯示,顏色,開始位置,結束位置,寬度) pygame.draw.line(screen, black, (160, 0), (160, 480), 5) pygame.draw.line(screen, black, (320, 0), (320, 480), 5) pygame.draw.line(screen, black, (0, 160), (480, 160), 5) pygame.draw.line(screen, black, (0, 320), (480, 320), 5) #遍歷列表中的元素及他們的下標 row橫col豎 row col是下標 for row, line in enumerate(state): for col, val in enumerate(line): if val == -1: #畫x upper_left = (col * 160 + 5, row * 160 + 5) lower_right = (col * 160 + 155, row * 160 + 155) pygame.draw.line(screen, red, upper_left, lower_right, 5) upper_right = (col * 160 + 155, row * 160 + 5) lower_left = (col * 160 + 5, row * 160 + 155) pygame.draw.line(screen, red, upper_right, lower_left, 5) elif val == 1: #創建一個矩形.在矩形裏畫圓 rect = (col * 160 + 5, row * 160 + 5, 150, 150) pygame.draw.ellipse(screen, blue, rect, 5) else: assert val == empty continue pygame.display.flip() def draw_O(): #隨機函數 while True: row = randint(0,2) col = randint(0,2) #當空格為空的時候畫圓 if state[row][col] == 0: state[row][col] = 1 break draw_game() pygame.display.flip() def is_won(): for val in range(3): # 檢查匹配的行三個圖形是否都相同且不等於空 if state[0][val] == state[1][val] == state[2][val] != empty: return state[0][val] # 檢查匹配的列三個圖形是否都相同不等於空 if state[val][0] == state[val][1] == state[val][2] != empty: return state[val][0] #判斷 \ 中三個圖形是否都相同 if state[0][0] == state[1][1] == state[2][2] != empty: return state[1][1] #判斷 / 中三個圖形是否都相同 if state[0][2] == state[1][1] == state[2][0] != empty: return state[1][1] #初始化棋盤 def begin(): global state state = [[empty] * 3,[empty] * 3,[empty] * 3] draw_game() #首先初始化 pygame.display.flip() begin() #主循環 while True: event=pygame.event.wait() #初始化 pos = None temp = 0 #接收到退出事件後退出程序 if event.type == pygame.QUIT: pygame.quit() exit(0) #加入了按鍵功能 elif event.type == KEYDOWN: if event.key == K_a: begin() draw_game() pygame.display.flip() elif event.key == K_s: pygame.event.post(pygame.event.Event(QUIT)) #接受鼠標點擊事件 elif event.type == MOUSEBUTTONDOWN and event.button == 1: #event.pos[0]代表x軸坐標 event.pos[1]代表y軸坐標 pos = (event.pos[1]/160, event.pos[0]/160) row,col=pos #if pygame.mouse.get_rel()==(0,0): # continue #加一個條件讓它只能在空的時候畫x if state[row][col]==0: state[row][col] = -1 else: continue print pos draw_game() draw_O() #判斷屬性接受返回值 if is_won() == -1: tkMessageBox.showinfo(title=win,message=win) pygame.quit() exit(0) elif is_won() == 1: tkMessageBox.showinfo(title=lose,message=lose) pygame.quit() exit(0)

技術分享

課後作業-團隊編程項目進度