Python 板


LINE

小弟利用Pygame寫了個簡單的pong遊戲 全部用函數寫成 有簡單的AI 純分享 若有前輩有精善意見 尚望不吝賜教 謝謝! Nopaste http://rafb.net/p/pFvj9k18.html import pygame from pygame.locals import * from sys import exit from random import randint size = (600, 800) title = "Pong Game Test" black = (0, 0, 0) gray = (128, 128, 128) white = (255, 255, 255) yellow = (255, 255, 0) aqua = (0, 255, 255) prompt = {1:"1. Play", 2:"2. Simulation", 3:"3. Exit"} def ballcontrol(point, radius, dx, dy, seconds, bat_score, player_score): x, y = point if x > 0 + radius: x += dx * seconds if x < 0 + radius: dx *= -1 x = 0 + radius if x < size[0] - radius: x += dx * seconds if x > size[0] - radius: dx *= -1 x = size[0] - radius if y > 0 + radius: y += dy * seconds if y < 0 + radius: dy *= -1 y = 0 + radius bat_score += 1 if y < size[1] - radius: y += dy * seconds if y > size[1] - radius: dy *= -1 y = size[1] - radius player_score += 1 return x, y, dx, dy, bat_score, player_score def rebound(point, dx, dy, seconds): x, y = point if randint(0, 1): dx *= -1.0 else: dx *= 1.0 dy *= -1 x += dx * (seconds + 0.1) y += dy * (seconds + 0.1) return x, y, dx, dy def initial(): if randint(0, 1): sx = 1 else: sx = -1 if randint(0, 1): sy = 1 else: sy = -1 return sx, sy def speed(): return float(randint(50, 100)) def batcontrol(key, point, speed, side, seconds): x, y = point if key[K_LEFT]: x -= speed * seconds if x < 0: x = 0 elif key[K_RIGHT]: x += speed * seconds if x + side[0] > size[0]: x = size[0] - side[0] return x, y def player1control(player_point, ball_point, speed, side, seconds): player_x, player_y = player_point ball_x, ball_y = ball_point if ball_y < 400: if player_x < ball_x + side[0]: player_x += speed * seconds if player_x < 0: player_x = 0 elif player_x + side[0] > size[0]: player_x = size[0] - side[0] if player_x > ball_x: player_x -= speed * seconds if player_x < 0: player_x = 0 elif player_x + side[0] > size[0]: player_x = size[0] - side[0] return player_x, player_y def player2control(player_point, ball_point, speed, side, seconds): player_x, player_y = player_point ball_x, ball_y = ball_point if ball_y > 400: if player_x < ball_x + side[0]: player_x += speed * seconds if player_x < 0: player_x = 0 elif player_x + side[0] > size[0]: player_x = size[0] - side[0] if player_x > ball_x: player_x -= speed * seconds if player_x < 0: player_x = 0 elif player_x + side[0] > size[0]: player_x = size[0] - side[0] return player_x, player_y def userplay(screen): ball_point = (300, 400) ball_x, ball_y = ball_point radius = 10. side = (80, 12) bat_point = (260, 740) player_point = (260, 60) bat_speed = 150 sign_x, sign_y = initial() ball_dx = sign_x * speed() ball_dy = sign_y * speed() score_point = (700, 500) bat_score = 0 player_score = 0 font = pygame.font.SysFont("arial", 32) clock = pygame.time.Clock() while True: for event in pygame.event.get(): if event.type == QUIT: exit() screen.fill(black) seconds = clock.tick(30) / 1000.0 ball = pygame.draw.circle(screen, yellow, ball_point, radius) bat = pygame.draw.rect(screen, white, Rect(bat_point, side)) player = pygame.draw.rect(screen, gray, Rect(player_point, side)) player_x, player_y = player_point bat_text = font.render(str(bat_score), True, white) player_text = font.render(str(player_score), True, gray) screen.blit(bat_text, (size[0]-bat_text.get_width()-20, size[1]/2+80)) screen.blit(player_text, (size[0]-player_text.get_width()-20, \ size[1]/2-80)) # ball control if bat.colliderect(ball): ball_x, ball_y, ball_dx, ball_dy = \ rebound(ball_point, ball_dx, ball_dy, seconds) elif player.colliderect(ball): ball_x, ball_y, ball_dx, ball_dy = \ rebound(ball_point, ball_dx, ball_dy, seconds) else: ball_x, ball_y, ball_dx, ball_dy, bat_score, player_score = \ ballcontrol(ball_point, radius, ball_dx, ball_dy, \ seconds, bat_score, player_score) ball_point = (ball_x, ball_y) # user's bat pressed_key = pygame.key.get_pressed() bat_point = batcontrol(pressed_key, bat_point, bat_speed, \ side, seconds) # artificial intelligence player_point = player1control(player_point, ball_point, bat_speed, \ side, seconds) if pygame.key.get_pressed()[K_ESCAPE]: menu(screen, prompt) pygame.display.update() def simulateplay(screen): ball_point = (300, 400) ball_x, ball_y = ball_point radius = 10. side = (80, 12) bat_point = (260, 740) player_point = (260, 60) bat_speed = 150 sign_x, sign_y = initial() ball_dx = sign_x * speed() ball_dy = sign_y * speed() score_point = (700, 500) bat_score = 0 player_score = 0 font = pygame.font.SysFont("arial", 32) clock = pygame.time.Clock() while True: for event in pygame.event.get(): if event.type == QUIT: exit() screen.fill(black) seconds = clock.tick(30) / 1000.0 ball = pygame.draw.circle(screen, yellow, ball_point, radius) bat = pygame.draw.rect(screen, white, Rect(bat_point, side)) player = pygame.draw.rect(screen, gray, Rect(player_point, side)) player_x, player_y = player_point bat_text = font.render(str(bat_score), True, white) player_text = font.render(str(player_score), True, gray) screen.blit(bat_text, (size[0]-bat_text.get_width()-20, size[1]/2+80)) screen.blit(player_text, (size[0]-player_text.get_width()-20, \ size[1]/2-80)) # ball control if bat.colliderect(ball): ball_x, ball_y, ball_dx, ball_dy = \ rebound(ball_point, ball_dx, ball_dy, seconds) elif player.colliderect(ball): ball_x, ball_y, ball_dx, ball_dy = \ rebound(ball_point, ball_dx, ball_dy, seconds) else: ball_x, ball_y, ball_dx, ball_dy, bat_score, player_score = \ ballcontrol(ball_point, radius, ball_dx, ball_dy, \ seconds, bat_score, player_score) ball_point = (ball_x, ball_y) # artificial intelligence player_point = player1control(player_point, ball_point, bat_speed, \ side, seconds) bat_point = player2control(bat_point, ball_point, bat_speed, \ side, seconds) if pygame.key.get_pressed()[K_ESCAPE]: menu(screen, prompt) pygame.display.update() def menu(screen, prompt): font = pygame.font.SysFont("arial", 40) while True: for event in pygame.event.get(): if event.type == QUIT: exit() text1 = font.render(prompt[1], True, white) text1_1 = font.render(prompt[1], True, aqua) text2 = font.render(prompt[2], True, white) text2_1 = font.render(prompt[2], True, aqua) text3 = font.render(prompt[3], True, white) text3_1 = font.render(prompt[3], True, aqua) x, y = pygame.mouse.get_pos() screen.fill(black) if 200 <= x <= 200 + text1.get_width() and \ 200 <= y <= 200 + text1.get_height(): screen.blit(text1_1, (200, 200)) screen.blit(text2, (200, 240)) screen.blit(text3, (200, 280)) if pygame.mouse.get_pressed()[0]: userplay(screen) elif 200 <= x <= 200 + text2.get_width() and \ 240 <= y <= 240 + text1.get_height(): screen.blit(text1, (200, 200)) screen.blit(text2_1, (200, 240)) screen.blit(text3, (200, 280)) if pygame.mouse.get_pressed()[0]: simulateplay(screen) elif 200 <= x <= 200 + text3.get_width() and \ 280 <= y <= 280 + text1.get_height(): screen.blit(text1, (200, 200)) screen.blit(text2, (200, 240)) screen.blit(text3_1, (200, 280)) if pygame.mouse.get_pressed()[0]: exit() else: screen.blit(text1, (200, 200)) screen.blit(text2, (200, 240)) screen.blit(text3, (200, 280)) pygame.display.update() def run(): pygame.init() screen = pygame.display.set_mode(size, 0, 32) pygame.display.set_caption(title) menu(screen, prompt) if __name__ == "__main__": run() --



※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 118.169.132.17
1F:→ bizkit:有沒有考慮用nopaste XD 10/04 21:30
※ 編輯: monomorium 來自: 118.169.132.17 (10/04 22:00)







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:BuyTogether站內搜尋

TOP