def run_game(): pygame.init() ai_settings = Settings() screen = pygame.display.set_mode( (ai_settings.screen_width, ai_settings.screen_height)) pygame.display.set_caption("Flying Bird") bird = Bird(ai_settings, screen) mountains = Group() button = Button(ai_settings, screen, "PLAY") # 创建计分板 sb = Scoreboard(ai_settings, screen) while True: gf.check_events(bird, button, ai_settings, mountains) if ai_settings.state: bird.update_bird() mountains.update(ai_settings) gf.update_mountains(bird, mountains, ai_settings) gf.built_mountains(mountains, ai_settings, screen) gf.update_screen(ai_settings, screen, bird, mountains, button, sb)
def run_game(): # init the game and create a screen obj pygame.init() ai_settings = Settings() screen = pygame.display.set_mode( (ai_settings.screen_width, ai_settings.screen_height)) pygame.display.set_caption("Alien Invasion") # create a play button play_button = Button(ai_settings, screen, "Play<P>") # create a instance of GameStats stats = GameStats(ai_settings) sb = Scoreboard(ai_settings, screen, stats) ship = Ship(ai_settings, screen) # create a group store the bullet bullets = Group() aliens = Group() # create the group of aliens gf.create_fleet(ai_settings, screen, aliens, ship) # start the main func while True: gf.check_events(ship, ai_settings, bullets, screen, stats, play_button, aliens, sb) if stats.game_active: ship.update() gf.update_bullets(bullets, aliens, ai_settings, ship, screen, sb, stats) gf.update_aliens(ai_settings, aliens, ship, stats, screen, bullets, sb) gf.update_screen(ai_settings, screen, ship, aliens, bullets, play_button, stats, sb)
def __init__(self, name='first'): """ :param scoreboard: :param paddle: :param name: Can be 'first' or 'second' """ self.score = Scoreboard(player=name) self.paddle = Paddle(player=name)
def run_game(): """Initialize a game, display.""" pygame.init() game_settings = Settings() screen = pygame.display.set_mode( (game_settings.screen_width, game_settings.screen_height)) pygame.display.set_caption(game_settings.game_name) # Make the Play button. play_button = Button(game_settings, screen, "Play") # Make describtion. description = Description(game_settings, screen, "Project by Andrii Dykyi") # Create an instance to store game statistics. stats = GameStats(game_settings) score_board = Scoreboard(game_settings, screen, stats) background_image = pygame.image.load( game_settings.background_image).convert() # Create rocket. rocket = Rocket(game_settings, screen) # Make a group to store bullets in. bullets = Group() # Make a meteorite. meteorites = Group() # Make a star. stars = Group() # Make an explosion. explosions = Group() while True: game_functions.check_event(game_settings, screen, stats, play_button, rocket, meteorites, stars, bullets, score_board, explosions) if stats.game_active: rocket.update() game_functions.update_bullets(bullets, screen, meteorites) game_functions.update_meteorites(game_settings, stats, screen, rocket, meteorites, score_board, explosions) game_functions.update_explosions(explosions) game_functions.update_stars(game_settings, stats, screen, rocket, stars, score_board) game_functions.update_screen(background_image, game_settings, screen, stats, score_board, rocket, stars, meteorites, bullets, play_button, explosions, description)
def run(): pygame.init() st = Setting() screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN) screen.get_rect().width = st.screen_w screen.get_rect().height = st.screen_h pygame.display.set_caption("Math Battle") bg2 = Bg2(screen) ques = Question(screen, st) yes = YesNo(screen, 'yes') no = YesNo(screen, 'no') play = Play(screen) stat = Stats(st) time = Timeline(screen, stat) score = Scoreboard(st, screen, stat) gettype(ques, stat) while True: check_event(ques, stat, score, yes, no, play, time) screen.fill(st.bg) bg2.bg2draw() score.show_score() activenot(ques, yes, no, play, time, stat) pygame.display.flip()
def run_game(): """初始化游戏并创建一个屏幕对象""" pygame.init() ai_setting = Settings() screen = pygame.display.set_mode( (ai_setting.screen_width, ai_setting.screen_height)) #设立一个像素为1200,800的显示窗口 pygame.display.set_caption("alien_game") #set_mode的实参为一个元组 ship = Ship(screen, ai_setting) #创建飞船 bullets = Group() #创建一个子弹元组 #alien = Alien(screen,ai_setting) #创建一个外星人 aliens = Group() #创建一个外星人编组 #用于构建一个群 gf.create_fleet(ai_setting, screen, ship, aliens) #用于绘制按钮 play_button = Button(ai_setting, screen, "play") #用于统计游戏信息 stats = Gamestats(ai_setting) #计分牌 sb = Scoreboard(ai_setting, stats, screen) #开始 游戏主循环 while True: #用于管理事件(要处在游戏启动之外) gf.chck_events(ai_setting, screen, ship, aliens, bullets, stats, play_button, sb) if stats.game_active: #用于移动飞船 ship.updata() #用于发射子弹 gf.updata_bullets(bullets, aliens, ai_setting, screen, ship, stats, sb) #用于外星人移动 gf.update_aliens(ai_setting, screen, stats, ship, bullets, aliens, sb) #用于显示图像 gf.update_screen(screen, ai_setting, ship, aliens, bullets, stats, play_button, sb)
def init_game(): global screen, score, ball, wall, paddle screen = Screen() screen.bgcolor("black") screen.setup(width=800, height=600) screen.title("Breakout") screen.tracer(0) wall = BlockBuilder() wall.create_wall(SCREEN_SIZE) paddle = Paddle(STARTING_POSITION,SCREEN_SIZE) ball = Ball(SCREEN_SIZE) score = Scoreboard() screen.listen() screen.update() screen.onkeypress(paddle.go_left, "Left") screen.onkeypress(paddle.go_right, "Right")
def main(): #---------- #init初始化所有模块,并检查 pygame.init() #---------- #从配置文件中拿参数 bj_settings = Settings() #---------- #设置屏幕大小,flags:扩展选项 screen = pygame.display.set_mode( (bj_settings.screen_width, bj_settings.screen_height), pygame.RESIZABLE, 32) #设置游戏主题 pygame.display.set_caption('Alien Invasion') #设置窗口图标 icon = pygame.image.load('image/ship.bmp') pygame.display.set_icon(icon) status = GameStatus(bj_settings) sc = Scoreboard(bj_settings, screen, status) #----------- #创建一个船的对象,调用船的位置方法 ship = Ship(bj_settings, screen) # alien = Alien(screen) #----------- bullets = Group() aliens = Group() game_functions.create_aliens(screen, bj_settings, aliens, ship) play_button = Button(bj_settings, screen, 'PLAY') while True: game_functions.check_events(screen, bj_settings, ship, bullets, status, play_button, aliens, sc) game_functions.update_screen(status, screen, bj_settings, ship, aliens, bullets, play_button, sc) if status.game_active: ship.continue_update() # bullets.update() # game_functions.update_bullets(bullets) game_functions.update_screen(status, screen, bj_settings, ship, aliens, bullets, play_button, sc)
def run_game(): pygame.init() setting = Setting() screen = pygame.display.set_mode([setting.width,setting.height]) ship = Ship(screen, setting) pygame.display.set_caption("Alien Games") play_button = Button(setting, screen, "Play") stats = GameStats(setting) sb = Scoreboard(setting, screen, stats) bullets = Group() aliens = Group() gf.create_fleet(setting, screen, aliens, ship) while True: gf.check_event(setting, screen, stats, play_button, sb, ship, aliens, bullets) if stats.game_active: ship.update() gf.update_bullet(setting, screen, stats, sb, ship, bullets, aliens) gf.update_alien(setting, stats, screen, ship, aliens, bullets) gf.update_screen(screen,setting, sb, ship, stats, bullets, aliens, play_button)
# fix animation glitches with tracer screen.tracer(0) # Create left and right paddles left_paddle = Paddle(starting_pos[0]) right_paddle = Paddle(starting_pos[1]) # Initial ball direction changes, x_change, y_change x_change = random.choice([10, -10]) y_change = random.choice([10, -10]) # Create the ball ball = Ball(x_change, y_change) # Generate ScoreBoard score = Scoreboard() # bind keys to paddles movement: screen.onkey(left_paddle.move_up, "q") screen.onkey(left_paddle.move_down, "a") screen.onkey(right_paddle.move_up, "Up") screen.onkey(right_paddle.move_down, "Down") screen.textinput( "Start Game", "q - Left handle move up\na -\ Left handle move down\nUp arrow - Right handle move up\n\ Down arrow - Right handle move down\n\n\ Press Enter to start the game.", )
import time from turtle import Screen from player import Player from car_manager import CarManager from score import Scoreboard screen = Screen() screen.setup(width=600, height=600) screen.tracer(0) player = Player() car_manager = CarManager() scoreboard = Scoreboard() screen.listen() screen.onkey(player.go_up, "Up") game_is_on = True while game_is_on: time.sleep(0.1) screen.update() car_manager.create_car() car_manager.move_cars() # Detect collision with car for car in car_manager.all_cars: if car.distance(player) < 20: game_is_on = False scoreboard.game_over()
from my_turtle import MyTurtle from car import Car from score import Scoreboard WIDTH = 1000 HEIGHT = 600 screen = Screen() screen.setup(width=WIDTH, height=HEIGHT) #screen.bgcolor("black") screen.title("Cross The Road") screen.tracer(0) myturtle = MyTurtle() my_car = Car() my_score = Scoreboard() screen.listen() screen.onkey(myturtle.go, "Up") is_game_on = True while is_game_on: time.sleep(0.1) screen.update() my_car.create_car() my_car.move() for car in my_car.all_cars: if car.distance(myturtle) < 29: is_game_on = False
screen.setup(width=WIDTH, height=HEIGHT) screen.bgcolor("black") screen.title("PING PONG") screen.tracer(0) h = 0 while h < HEIGHT / 2: Line((0, h)) Line((0, -h)) h += 30 r_paddle = Paddle((470, 0)) l_paddle = Paddle((-470, 0)) my_ball = Ball() my_score = Scoreboard() screen.listen() screen.onkey(r_paddle.go_up, "Up") screen.onkey(r_paddle.go_down, "Down") screen.onkey(l_paddle.go_up, "w") screen.onkey(l_paddle.go_down, "a") is_game_on = True while is_game_on: screen.update() time.sleep(0.01) my_ball.move() if my_ball.ycor() > 280 or my_ball.ycor() < -280: my_ball.bounce_y()
from turtle import Screen from ball import Ball from paddle import Paddle from block import Blocks from score import Scoreboard screen = Screen() screen.screensize(canvheight=500, canvwidth=600) screen.bgcolor("black") screen.title("break-out") # object call ball = Ball() paddle = Paddle() blocks = Blocks() scoreboard = Scoreboard() # events screen.listen() screen.onkey(paddle.move_left, "a") screen.onkey(paddle.move_right, "d") # game game = True while game: ball.move() # wall clash if ball.xcor() > 290 or ball.xcor() < -290: ball.x_bounce() if ball.ycor() > 250: ball.y_bounce()
from snake import Snake from food import Food from score import Scoreboard from help_functions import * import time # prepare screen screen = setup_window() # create scoreboard scoreboard = Scoreboard() # create snake snake = Snake() # create food food = Food() # listen to events screen.listen() # run game game_is_on = True while game_is_on: check_keys(screen, snake) snake.move() check_food_collision(snake, food, scoreboard) game_is_on = check_wall_collision(snake, scoreboard) game_is_on = check_body_collision(snake, scoreboard) screen.update() time.sleep(0.1)
from turtle import Screen import time from snake import Snake from food import Food from score import Scoreboard screen = Screen() screen.setup(width=600, height=600) screen.bgcolor("black") screen.title("Snake Game") screen.tracer(0) snake = Snake() food = Food() scoreboard = Scoreboard() screen.listen() screen.onkey(snake.up, "Up") screen.onkey(snake.down, "Down") screen.onkey(snake.left, "Left") screen.onkey(snake.right, "Right") game_in_on = True while game_in_on: screen.update() time.sleep(0.1) snake.move() # Detect collision with food. if snake.head.distance(food) < 15: food.refresh()
from turtle import Screen from snake import Snake from food import Food from score import Scoreboard import time screen = Screen() screen.setup(width=600, height=600) screen.bgcolor("black") screen.title("Snake Game") screen.tracer(0) snake = Snake() food = Food() scoreboard = Scoreboard() screen.listen() screen.onkey(snake.up, "Up") screen.onkey(snake.down, "Down") screen.onkey(snake.right, "Right") screen.onkey(snake.left, "Left") game_is_on = True while game_is_on: screen.update() time.sleep(0.1) snake.move() # detecting collision with food if snake.head.distance(food) < 30: food.refresh()
from ball import Ball import time from score import Scoreboard # set screen screen = Screen() screen.setup(800, 600) screen.bgcolor('black') screen.title('Pong') screen.tracer(0) # pull from classes r_paddle = Paddle((350, 0)) l_paddle = Paddle((-350, 0)) ball = Ball() score = Scoreboard() screen.listen() screen.onkey(r_paddle.go_up, "Up") screen.onkey(r_paddle.go_down, "Down") screen.onkey(l_paddle.go_up, "w") screen.onkey(l_paddle.go_down, "s") game_is_on = True while game_is_on: time.sleep(ball.move_speed) screen.update() ball.move_ball() # wall collision if ball.ycor() > 280 or ball.ycor() < -280:
import time from snake import Snake from food import Food from score import Scoreboard segments = [] scn = Screen() scn.setup(width=600, height=600) scn.bgcolor("black") scn.title("Nokia Snake Game") scn.tracer(0) snake = Snake() food = Food() score = Scoreboard() scn.listen() scn.onkey(snake.up, "Up") scn.onkey(snake.down, "Down") scn.onkey(snake.left, "Left") scn.onkey(snake.right, "Right") game_on = True while game_on: scn.update() time.sleep(0.1) snake.move() # detect if food consumed if snake.head.distance(food) < 15:
import time screen = Screen() screen.setup(width=800, height=600) screen.bgcolor("#8c0000") screen.title("Carey's Pong Game") screen.tracer(0) #paddles paddle_b = Paddle((350, 0)) paddle_a = Paddle((-350, 0)) ball = Ball() #scoring score = Scoreboard() #move paddles screen.listen() screen.onkey(paddle_b.go_up, "Up") screen.onkey(paddle_b.go_down, "Down") screen.onkey(paddle_a.go_up, "w") screen.onkey(paddle_a.go_down, "s") game_on = True while game_on: time.sleep(ball.move_speed) screen.update() ball.move()