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灯, 水草

请输入看板名称,例如:WOW站内搜寻

TOP